[go: up one dir, main page]

0% found this document useful (0 votes)
0 views31 pages

Lecture-4

The document outlines key programming concepts in C++, including constants, expressions, type casting, and input streams. It explains how to define constants, evaluate expressions, and perform type conversions, along with practical examples. Additionally, it provides a problem statement for calculating the average age of students and includes a sample C++ code solution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views31 pages

Lecture-4

The document outlines key programming concepts in C++, including constants, expressions, type casting, and input streams. It explains how to define constants, evaluate expressions, and perform type conversions, along with practical examples. Additionally, it provides a problem statement for calculating the average age of students and includes a sample C++ code solution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Programming Fundamental

Instructor Name: Mohabbat Ali


Lecture-4
Today’s Lecture

 Constants

 Expression and its types

 Type Casting (Conversion) & its Forms

 Type Promotion (Integer Promotion)

 Escape Sequence

 Quadratic Equation

 Use of Operators in Expressions

 Input Stream and its member functions

 Problem Statement (Analysis & Solution)


2
Constants in C++

What is a Constant?
 Constants refer to fixed values that the program may not alter and they are
called literals.
 Constants can be of any of the basic data types and can be divided into
Integer Numerals, Floating-Point Numerals, Characters, Strings and
Boolean Values.
 Constants are treated just like regular variables except that their values
cannot be modified after their definition.

3
Constants in C++

Defining Constants
 There are two simple ways in C++ to define constants:

1. Using #define preprocessor.


2. Using const keyword.
 It is Good Programming practice to define constants in CAPITALS.

4
Constants in C++

The #define Preprocessor


 Following is the form to use #define preprocessor to define a constant:
Using #define
#define identifier value
 Any occurrence of identifier in the code is interpreted as replacement,
where replacement is any sequence of characters.
 Replacement performed by the preprocessor, before the program is
compiled, thus cause sort of blind replacement.
 Validity of the types or syntax involved is not checked in any way.
 do not require semicolons (;) at the end; If a semicolon is included in
the line, it is part of the replacement sequence and included in all
replacements.

5
Constants in C++

Named Constants
 Concept: Literals may be given names that symbolically represents them in
a program.
 A named constant is like a variable, but its content is read-only, and
cannot be changed while the program is running.
 use const prefix to declare constants with a specific type as follows:

const type variable = value;


 Sometimes, it is convenient to give a name to constant value
 A const object is subject to the scoping rules for variables, whereas a
constant created using #define is not.

6
Expression

What is an Expression?
 Expressions are sequences of operators and operands that specifies a
computation.
 Expressions are used for one or more of these purposes:
 Computing a value from the operands
 Designating objects or functions.

7
Expression

Types of Expression
 Expressions may be of one of following types
 Integral Expression: A mathematical expression involving one or more
integral (all operands are integer) and produces result in integer form.
 Floating Expression: A mathematical expression that all operands of
float type. The result will be float also
 Mixed Expression: An expression that has operands of different data
types is called a mixed expression

8
Expression

Evaluating Expression
 When evaluating an operator in a mixed expression:
 If both operands have same data type then result will be of operand
type
 If the operator has different types of operands then the result would of
higher data type used in expression
 For example one is an integer, one is float and the third is a double,
then the result would be a double number.

9
Type Casting in C++

What is Type Casting?


 Type casting is a way to convert a variable from one data type to another
data type.
 For example, if you want to store a long value into a simple integer then
you can type cast long to int.
 You can convert values from one type to another explicitly using the cast
operator as follows:
(type_name) expression
int sum = 17, count =
5; double mean;
mean = (double)
sum / count;

10
Type Casting in C++

Type Casting - Types


 Type casting is of two types
 Implicit Type Casting:
conversion performed by the compiler automatically
 Explicit Type Casting
conversion performed via an unary type-casting operator in
form of: the
(new-type) operand //functional notation
new-type (operand) //c-like case notation
Example
double x =
10.3; int y;
y = int (x);
y = (int) x;

11
Explicit Type casting

 Try to determine the output of the following expression


 cout<<int(7.9) ; 7
 cout<<int(3.3) ;
3
 cout<<double(25) ; 25.0
 cout<<double(15)/2 ;
7.5
 cout<<double(15/2) ; 7.0
 cout<<int(7.8+double(15)/2) ;
15
 cout<<int(7.8+double(15/2)) ; 14

12
Escape Sequence

What is Escape Sequence


 An escape sequence is a sequence of characters that does not represent
itself when used inside a character or string literal, but is translated into
another character or a sequence of characters that may be difficult or
impossible to represent directly.
 Syntax is as follow

cout<<“My name on next line\n Atif ”;

13
Quadratic Equation

What is Quadratic Equation


 a quadratic equation is any equation having the form

 The Standard Form of a Quadratic Equation looks like this:

 In C++ the above equation would be represented as

a*x*x + b*x + c

14
Discriminant
b2 -
4ac
2a
= b*b - 4*a*c /2
*a

Incorrect
answer

Solution
Input Stream in C++

cin Object
 Concept : the cin object can be used to read data typed at the
keyboard.

16
Input Stream in C++

cin in c++
 Concept : the cin object can be used to read data typed at the keyboard.
 cin is used together with the extraction operator, which is written as >>.
This operator is then followed by the variable where the extracted data is
stored.
#include <iostream>
using namespace std;
int main () {
int i;
cout << "Please enter
an integer value: ";
cin >> i;
cout << "The value
you entered is " << i;
}
17
cin and the Extraction Operator >>

 Suppose payRate is double variable


 cin >> payRate;
 The extraction operator >> is binary and thus takes two

operands.
The left-side operand must be an input stream variable, such
as cin. And right hand side is a variable to store the data.
cin >> payRate >> hoursWorked;
Equivalent to
cin >> payRate;
cin >>
hoursWorked;
White space or carriage return are equivalent to segregate
the two inputs
cin and the Extraction Operator >>
cin and the Extraction Operator >>
cin and the Extraction Operator >>
cin and the Extraction Operator >>
Input Stream in C++

cin without >> operator


 The >> operator may be used when you simply want to read the next non-
blankspace characters entered by the user into a character or character
array.
 Any printable characters that follow the first space will be ignored and
will not be stored in the variable
 if, for example, you wish to obtain a whole sentence from the user that
includes spaces. In that case, you would be better served by using the cin
member functions get or getline.

23
Input Stream in C++

Using cin.get
 The unformatted get member function works like the >> operator with one
exceptions
1. The get function includes white-space characters, whereas the
extractor excludes white space .
 A variation of the get function specifies a buffer address and the maximum
number of characters to read. This is useful for limiting the number of
characters sent to a specific variable, as in example on next slide:

24
Input Stream in C++

cin.get Example
using namespace std;
#include <iostream>

main() {
char
line[25];
cout << " Type Line and enter\n>";
cin.get( line, 25 );
cout << ' ' << line;
}

In this example, you can type up to 24 characters and a terminating character.


Any remaining characters can be extracted later.

25
Input Stream in C++

Using cin.getline
 The getline member function is similar to the get function.
 Both functions allow a third argument that specifies the terminating
character for input.
 The default value is the newline character.
 Both functions reserve one character for the required terminating
character.
 However, get takes the input stream character by character while getiline
take the input steram line by line.

26
Input Stream in C++

cin.getline Example
using namespace std;
#include <iostream>

void main() {
char line[100];
cout<<"Type Line terminated by ‘t’ \n>";
cin.getline( line, 100,’t’ );
cout << line;
}

In this example, if user enter Pakistan, the output will be Pakis. It will
display characters up to ‘t’

27
Use of Operator

Problem Statements
 Calculate the average age of a class of five students. Prompt the user
to enter the age of each student.
Problem Analysis
We have to calculate the average age of five students so we will take the ages
of five students from the user. Five variables are required to store the ages,
one variable for each student’s age. We will take the ages of students in
whole numbers (in years only, like 10, 12, 15 etc), so we will use the variables
of data type int.

28
Solution:C++ code

#include <iostream>
using namespace std;
int main() {
int student1, student2, student3 , student4 , student5;
double avg;
cout << "Enter the age of student 1: ";
cin >> student1;
cout << "Enter the age of student 2: ";
cin >> student2;
cout << "Enter the age of student 3: ";
cin >> student3;
cout << "Enter the age of student 4: ";
cin >> student4;
cout << "Enter the age of student 5: ";
cin >> student5;

avg = (student1 + student2 + student3+ student4+ student5) / 5.0;


cout << "The average age of five students is " << avg << endl;
} 29
Home task

• Please solve chapter-2 programming exercise questions 1-10.

30
35

You might also like