Unit 4
Unit 4
Unit 4
UNIT – 4
• Type compatibility
• Variables
• Cast Operator
• For Ex:
int num1;
• Struct
• Union
• Enum
• class
s.total=s.cPlus+s.maths+s.sql+s.dfs;
s.per=s.total/4;
• Syntax:
union union_name //union declaration
{
data_type member1;
data_type member2;
…
};
roll_no
Byte 0 Byte 1
grade
enum char{a,b,c};
For example :
enum char{a=3,b=7,c,d};
here, value of c is 8 and d is 9.
UNIT – 4 : Tokens and Expression & Control Structure
Exercise
24
Solution
#include<iostream>
using namespace std;
int main()
enum Fruits{apple,orange,guava,pinapple};
Fruits myfruit; //optional
int i;
cout<<"Enter your choice:";
cin>>i;
}
return 0;
}
class class_name
{
data member variables;
data member methods/functions;
};
31
Solution
#include<iostream>
using namespace std;
class Pyramid
{
int i,n,j;
public:
void getdata();
void buildpyramid();
};
void pyramid::getdata()
{
cout<<"enter N:"; cin>>n;
}
• Ex:
char name[10];
int student[10];
• Ex:
int max(int n1,int n2);
39
Solution
#include<iostream> using namespace std;
int max(int n1,int n2); //declaration
int main()
{
int n1; int n2; int a;
cout<<"Enter Number1: "<<"\
n"; cin>>n1;
cout<<"Enter Number2: "<<"\
n";
cin>>n2;
a=max(n1,n2); //calling function
cout<<"max value is "<<a<<"\n";
return 0;
}
return result;
}
int main()
{
int var=34; int *p;
p=&var;
cout<<"Address stored in
variable is "<<p<<"\n";
cout<<"Value of P Variable is
"<<*p<<"\n"; return 0;
}
UNIT – 4 : Tokens and Expression & Control Structure
Symbolic Constant Type
• Constant is an identifier with associated value which can not be altered
by the program during execution.
• You must initialize a constant when you create it, you can not assign
new value later after constant is initialized.
#define pi 3.14
const max=10;
enum char{a,b,c};
• It can be used to get the size of classes, structure, union and any
other user defined data type.
• Syntax:
sizeof(data_type)
• Ex:
sizeof(x) or sizeof(int)
int main()
{
int a,x; char b;
float c=10.02; double d;
cout<<"size of a:"<<sizeof(a)*sizeof(x)<<"\n"; \*nested sizeof *\
cout<<"size of b:"<<sizeof(b)<<"\n";
cout<<"size of c:"<<sizeof(10.02)<<"\n";
cout<<"size of d:"<<sizeof(d)<<"\n";
return 0;
}
• Ex:
int a=0;
char name[]={‘h’,’e’,’l’,’l’,’o’}; char name[8]=‘computer’
For example:
int c= a+b;
float avg=sum/n;
• Ex:
int a;
int& i=a;
• Basically variable are two types ,local variable and global variable.
• The global variable are declared out side function and used
anywhere in the program.
• Syntax:
::variable_name;
#include<iostream>
using namespace std;
int m=5; //global
declaration
int main()
{
int m=15;
cout<<"Value of m is "<<m<<"\n";
cout<<"Value of global ::m is "<<::m<<"\n";
return 0;
}
UNIT – 4 : Tokens and Expression & Control Structure
Member Dereferencing Operator
The member dereferencing operator are used in class, a class
contain various type of data and function as member.
The dereference operator is also known as the indirection
operator.
2 2
int main()
{
int roll_no=2;
char name[]="Heer"; int age=19;
• Categorized in
1. Automatic conversions:
When two operands of different tpes are encountered in the
same expression, the lower-type variable is converted to the
type of higher-type variable.
2. Casts:
This is specified by the programmer, as opposed to the
automatic data conversions. This is sometimes called
coercion; the data is coerced into becoming another type
• For example, if two operands are respectively int and float type,
then int is converted into a float ad then operation is performed.
• Syntax:
(type_name) expression;
type_name (expression);
• Ex:
average=(float) sum/n;
average=float(sum)/n;
• Constant expressions
• Integral expressions
• Float expressions
• Pointer expressions
• Relational expressions
• Logical expressions
• Bitwise expressions
• Integral expression:
Integral expressions are those which produce integer results after
implementing all the automatic and implicit type conversions.
Ex: m * n – 5; //m and n are integer
5 + int(2.0);
• Float expression:
Floating expression are those which, after all conversations,
produce floating point results.
Ex: 5 + float(10);
x * y / 10; //x and y are floating point
• Relational expression:
Relational expressions yield results of type bool which takes a
value true or false.
Also known as boolean expression.
Ex: x <= y;
a+b = c+d;
If arithmetic expression are used either side of relational
operator, they will be evaluated first then the results
compared.
• Bitwise expression
Bitwise expressions are used to manipulate data at bit
level. They are basically used for testing or shifting bits.
• Compound assignment:
Compound assignment operator (+=)is a combination of
the assignment operator with a binary operator.
Ex: x=x+10 may be written as , x +=10;
• If the operand type differ than the compiler converts one of them to
match with the other using the rule that the “smaller” type is
converted to the “wider” type.
• For example, if one operand is an int and other is float , the int is
converted into float because float is wider than int.
Scope resolution
1 ::
operator
Suffix/postfix increment
++ --
decrement
Function style
type()
Type cast
type{ }
Left-to-right
2 Function call Array
()
subscripting
[]
Element selection by
.
Reference Element
selection through pointer
-> C
ala
UNIT – 4 : Tokens and Expression & Control Structure i
precedence operators description associativity
?: Ternary operator
= Direct assignment
operator
+= -= Assignment by sum
and difference
15 Right-to-left
*= /= %= Assignment by product
quotient, and
remainder
<<= >>= Assignment by left shift
and right shift
&= ^= |= Assignment by bitwise
AND XOR and OR
, comma
16 Left-to-right
syntax:
if (expression is true) if(a>b)
{ {
cout<<“a is max”;
action-1; }
} else
else {
{ cout<<“b is max”;
}
action-2;
}
while(i>5)
{
cout<<“number of class:”; i++;
}
i=0;
do
{
cout<<i<<“\n”; i++;
}
while(i<5);
for(int i=0;i<=5;i++)
{
cout<<i<<“\n”;
}
Subject Teachers: