Documente Academic
Documente Profesional
Documente Cultură
1
void
The void type has no values
and no operations. In other
words, both the set of values
and the set of operations are
empty. Although this might
seem unusual, we will see
later that it is a very useful
data type.
Integer
An integer type is a number without a
fractional part. It is also known as an
integral number. C++ supports three different
sizes of the integer data type: short int, int
and long int.
Short int
int
long int
2
Integer
The type also defines the size of the field in which data can
be stored. In C++, even though the size is machine
dependent, most PCs use the integer sizes shown below.
Size Size
Name Description (Bytes) (bits) Range
signed: -32768 to 32767
Short int Short 2 16 unsigned: 0 to 65535
Integer
signed: - 2147483648 to 2147483647
int Integer 4 32 unsigned: 0 to 4294967295
signed: –9,223,372,036,854,775,808
Long int Long Integer 8 64 to
9,223,372,036,854,775,807
unsigned: 0 to
18,446,744,073,709,551,615
5
Floating Point
A floating-point type is a number with a
fractional part, such as 43.32 . The C++
language supports three different sizes of
floating-point: float, double and long double.
float
double
long double
3
Floating Point
Although the physical size of floating-point types is
machine dependent, many computers support the
sizes shown below.
float 4 32
double 8 64
long double 10 80
Boolean
The Boolean data type:-Boolean variable are set to
either true or false. Expressions that have a true or
false value are called Boolean expression.
True=1;
False= 0;
4
Character data
A variable or a constant of char type can hold an ASCII
character .
When initializing a constant or a variable of char type, or
when changing the value of a variable of char type, the value
is enclosed in single quotation marks.
Examples:
const char star = '*';
char letter one = '1';
Declarations
Constants and variables must be declared before they can
be used.
A constant declaration specifies the type, the name and
the value of the constant.
A variable declaration specifies the type, the name and
possibly the initial value of the variable.
When you declare a constant or a variable, the compiler:
1. Reserves a memory location in which to store the value
of the constant or variable.
2. Associates the name of the constant or variable with
the memory location. (You will use this name for
referring to the constant or variable.)
10
5
Constant declarations
Constants are used to store values that never change during
the program execution.
Syntax:
const <type> <identifier> = <expression>;
Examples:
const int size = 7;
11
Variable declarations
Variables are used to store values that can be changed
during the program execution.
A variable is best thought of as a container for a value.
3445 y -3.14
Syntax:
< type > < identifier >;
< type > < identifier > = < expression >;
Examples:
int sum;
int total = 3445;
char answer = 'y';
double temperature = -3.14;
12
6
Variable declarations
A variable has a type and it can contain only values of
that type. For example, a variable of the type int can only
hold integer values.
Variables are not automatically initialized. For example,
after declaration
int sum;
the value of the variable sum can be anything.
Thus, it is good practice to initialize variables when they
are declared.
int sum =10;
13
Examples
#include<iostream.h>
int main()
{
int x;
x= 6;
cout<<x<<"\n";
x=7.5 ;
cout<<x<<"\n";
return 0;
}
14
7
Examples
#include<iostream.h>
int main()
{
char val=‘D’;
val=68;
cout<<val<<"\n";
val=70;
cout<<val<<"\n";
cout<<"\n";
return 0;
}
15
Examples
#include<iostream.h>
int main()
{
bool x;
x=true;
cout <<"x="<<x<<"\n";
x=false;
cout<<"x="<< x<<"\n";
return 0;
}
16
8
Rules for Division
C++ treats integers differently from decimal numbers.
100 is an int type.
100.0 , 100.0000, and 100. are double type.
The general rule for division of int and double types
is:
double/double = double (normal)
double/int = double (normal)
int/double = double (normal)
int/int = int (note: the decimal part is discarded)
17
18
9
Assignment Conversions
A decimal number assigned to an int type variable is
truncated.
An integer assigned to a double type variable is
converted to a decimal number.
Example 1:
double yy = 2.7;
int i = 15;
int j = 10;
i = yy; // i is now 2
yy = j; // yy is now 10.0
19
Operators
Relational Operators:
Assume :
A= 10
B= 20
20
10
Operators
Logical Operators:
Assume :
A= 1
B= 0
Or:
A=True
B=False
Operators
Bitwise Operators:
Bitwise operator works on bits and perform bit-by-bit
operation.
Assume if A = 60;
and B = 13; now in binary format they
will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
11
Operators
Bitwise Operators:
The truth tables for &, |, and ^ are as follows:
Operators
Assignment Operators:
12
Operators
Assignment Operators:
Operators
Relational Operators:
There are relational operators supported by C++ language… Assume variable A=10 and B=20, then:
13
Operators
CONDITIONAL OPERATORS: (? :(
27
operators
Ex:-
int i=10,j=20,x=8,y=9;
int k;
K= (x==y)? i:j;
K= 20
----------------------------------------
EX:-
int n=5,m=4;
int g;
g=(n>m)? n: m;
g= 5
28
14
operators
Comma operator ( , )
29
operators
Comma operator
Ex:-
x= (y=3,y+2);
x= 5 ; y=3;
-----------------
X=(y=1,y=y+3)
X=4 ;
y=4;
--------
Find a where b=5
a=(b=b+5,20/b);
a= 2, b= 10;
----------------- 30
15
operators
Ex:- find m where n=1 H.W:-
m=(n+1,n+2)
n= char x= 'f ';
m= int y= ((x+5) != ‘k’);
y=0 or y= 1
---------------------------------
31
operators
Algebraic expression C++Expression
a= 3bx+4 a=3*b*x+4
b=3x+2/4a-1 b= (3*x+2)/(4*a-1)
32
16
operators
Ex:-
If
a=2, b=2
Then find x
where :
x= ((a<<(a*2)+b) | (b>>!0));
33
Syntax:-
cin>> var1>>var2>>…..>>var n;
17
Cin & Cout
What is the deference between the following
int x; double y;
1.cin >>x>>y;
2. x=6 and y=7.7
Note:- you must be careful when the user enter a variables from
different data types.
35
36
18
Cin & Cout
Ex1:- Write a program in c++ to compute the area of circle
(rectangle or triangle), then print the result.
Ex2:- Write a program in c++ that reads two integer numbers and
then give and print the result in
1. Multiplication
2. Addition
3. Subtraction
4. Division
-----------------------------------------------------
H.W:- Write a c++ program to identify and read two numbers,
then print the minimum one between them using (?:)
operators.
37
38
19
Write a c++ program to read five integer numbers, then find and
print the average of them.
#include<iostream.h>
int main()
{
int v1,v2,v3,v4,v5;
double ave;
cout<<"Enter five marks as an integers : \n";
cin>>v1>>v2>>v3>>v4>>v5;
ave=(v1+v2+v3+v4+v5)/5.0;
cout<< endl <<"The average is = " << ave <<
endl;
return 0;
39
}
Write a C++ program to read five integer numbers, then find the
average of them, print the average, and then print if the student
successful or unsuccessful.
#include<iostream.h>
int main()
{
int v1,v2,v3,v4,v5;
double ave;
cout<<"Enter five marks as an integers : \n";
cin>>v1>>v2>>v3>>v4>>v5;
ave=(v1+v2+v3+v4+v5)/5.0;
cout<< endl <<"The average is = " << ave << endl;
(ave>=50) ? cout<<"The student is successful" : cout<<"The student is unsuccessful";
return 0;
} 40
20
Write a C++ program to input the ASCII code and to print the character
related to that code.
#include<iostream.h>
int main()
{
int y= 102;
char x= y;
cout<<x<<endl;
cout<< y<< endl ;
return 0;
41
Write a C++ program to input the character and to print the ASCII code
related to that character .
#include<iostream.h>
int main()
{
char x= 'f';
int y= x;
cout<<x<< endl;
cout<< y<< endl;
return 0;
}
42
21