Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views49 pages

C++ (OOPs)

Uploaded by

sandeepkaringu2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views49 pages

C++ (OOPs)

Uploaded by

sandeepkaringu2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

C++ (OOPs)

In 1976, Bjarne Stroustrup developed C++ (oops) in BELL laboratories, New Jersey, USA.
First name it as C with classes after that it change to C++ (CPP), because it is increment of C.
Tokens:
 Keywords
 Characters (Character set)
 Constants
 Variable
 Operators
Key words:
int if continue auto protected
float else default register friend
char while void static virtual
short do return external template
long for struct typedef operator
double switch union class
signed break enum public
unsigned case sizeof private

Identifiers: Name of the classes, functions, variables, objects, labels in program


Operators In C++:
An operator is a symbol that takes one or more arguments and operates on them to produce a
result.
<< Insertion operator new Memory allocation operator
>> Extraction operator delete Memory release operator
:: Scope resolution operator endl Line feed operator
::* Pointer-to-member declaratory setw Field width operator
->* Pointer-to-member operator
.* Pointer-to-member operator
(or)
Operator is a symbol that tells the computer to perform mathematical or logical
manipulations.
Eg: a%b a-(a/b)*b
Constants: It is constant value or constant declaration.
Eg: const int size=10; 10 100
const float pi=3.14; 123 1.23

Variables: A variable is an identifier used to store a data value.

Data Types: Every variable is a data type that specify the size and type of values that can be
stored.
Built-in type : int, char, float, double, void
User-Defined type : struct, union, class, enum (enumeration)
Derived type : array, function, pointer
Declaration of Variables Names of storage locations
* A variable must be declare before it is used in the program. Variables are separated by
commas.
type v1,v2,v3, .. . . . . vn;
Ex: int number; float per;
char c1,c2; double y;

/* Simple program1 */
#include<iostream.h>
#include<conio.h>
void main( )
{ clrscr( ); Output:-
cout<<”welcome”; Welcome
getch( ); //printf(“welcome”);
}
/* Simple program2 */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x=10;
clrscr( ); Output:-
cout<<”x :”<<x; x:10
getch( ); // cout<<x; 10
}
/* Simple program3 */
#include<iostream.h>
#include<conio.h> Output:-
void main( ) 1.23
{ float x=1.23;
clrscr( ); cout<<x;
getch( );
}
/* Simple program4 */
#include<iostream.h>
#include<conio.h>
void main( )
{ char x=’A’;
clrscr( ); Output:-
cout<<”x:”<<x; x:A
getch( ); //x=a;
}
/* Simple program5 */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x=10,y=3,z;
clrscr( ); z=x+y; Output:-
cout<<x<<”+”<<y<<”=”<<z; 10+3=13
getch( ); // cout<< z; 13
} // printf(“%d+%d=%d”,x,y,z);
/* Simple program read integer */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x; clrscr( ); Output:-
cout<<”enter no :”; enter no :10
cin>>x; // scanf(“%d”,&x); x: 10
cout<<”x :”<<x; getch( );
}
/* Write a program to add two integers */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x,y,z; clrscr( ); Output:-
cout<<”enter two nos :”; cin>>x>>y; enter two nos :10 5
z=x+y; cout<<z; getch( ); 15
}
/* Write a program to find area of the circle */
#include<iostream.h>
#include<conio.h>
void main( )
{ int r; clrscr( ); Output:-
cout<<”enter r :”; cin>>r; enter r:1
float a=3.14*r*r; cout<<a; 3.14
getch( );
}
/* Write a program to swap two numbers */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x,y; clrscr( ); Output:-
cout<<”enter two nos :”; cin>>x>>y; enter two nos :10 50
cout<<”x:”<<x<<”y:”<<y<<endl; x:10 y:50
x=x+y; x:50 y:10
y=x-y;
x=x-y;
cout<<”x:”<<x<<”y:”<<y<<endl; getch( );
}
Setw: (Manipulators)
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{ int x=1,y=12,z=123,m=1234,n=12345; Output:-
clrscr( ); 12345
cout<<setw(6)<<n<<endl; 1234
cout<<setw(6)<<m<<endl; 123
cout<<setw(6)<<z<<”\n”; 12
cout<<setw(6)<<y<<endl; 1
cout<<setw(6)<<x<<endl; 12
getch( ); 1 123
} 12
123
1234
Reference Variables:
data-type & reference-name = variable-name
Ex: -
float x = 10; float &y = x;
/*Example program for reference variable */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x=10; int &y = x; // y is a reference variable of x
cout<<”x :”<<x<<”y :”<<y<<endl; O/P:-
x = x + 10; x: 10 y :10
cout<<”x :”<<x<<”y :”<<y<<endl; x: 20 y :20
y =y-5; x: 15 y :15
cout<<”x :”<<x<<”y :”<<y<<endl;
getch( );
}

/* Write a program to add, sub, multi, divide & find remainder of the two nos. */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x,y,a,b,c,e; float d; Output:
clrscr( ); x y enter two nos: 10 3
cout<<“enter two nos:”; cin>>x>>y; a : 13 b : 7 c : 30
a = x + y; b = x – y; 10 3 d : 3.333333 e : 1
c = x * y; a b c d e
d = x / (float) y; //d=x/y;
e = x % y;
cout<<“a :”<<a<<”b :”<<b<<”c :”<<c<<endl;
cout<<“d :”<<d<<”e :”<<e;
getch( );
}

cout<<a<<” ”<<b 13 7 30
LOOPS
Def: -
One or more that one statement(s) are repeated or executed through a condition.

Branching When a program breaks the sequential flow and jumps to another
part of the code, it is called branching

Conditional Branching When the branching is based on a particular condition, is known as


conditional branching

Un-Conditional Branching If branching takes place without any decision, is known as


Un-conditional branching

Types of statements: -

I. Conditional statements
1. if 2. switch
II. Iterative statements
1. While 2. do-while 3. for
III. Un-conditional statement
1. goto
If: -
Def: -
Check the condition, when condition is true execute if following statement(s).
Syntax: -
if(cond) if(cond)
statement1; (or) {
B.S.
}
/* Write a program to check the given no is positive. (using if) */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x; Output:
clrscr( ); x enter any no : 10
cout<<“enter any no :”; cin>>x; 10 10 is +ve
if(x>0)
cout<<x<<”is +ve”;
getch( );
}
/* Write a program to check the given no is even. (using if) */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x; x Output:
clrscr( ); 20 enter any no : 20
cout<<“enter any no :”; cin>>x; 20 is even
if(x% 2 = = 0)
cout<<x<<“is even”;
getch( );
}
If-else: -
Def: -
Check the condition, when condition is true execute if following statement(s). When
condition is false else following statement(s) will be executed.
Syntax: -
if(cond) if(cond)
statement1; (or) {
else
statement2; B.S.
}
else
{
B.S.

}
/* Write a program to check the given no is positive or negative. (using if-else) */
#include<iostream.h>
#include<conio.h>
void main( ) x
{ int x; clrscr( ); 12 Output:
cout<<“enter any no :”; cin>>x; enter any no : 12
if(x>0) 12 is +ve
cout<<x<<”is +ve”;
else enter any no : -2
cout<<x<<”is -ve”; -2 is -ve
getch( );
}
/* Write a program to check the given no is even or odd. (using if-else) */
#include<iostream.h>
#include<conio.h>
void main( ) x
{ int x; 10 Output:
clrscr( );
cout<<“enter any no :”; cin>>x; enter any no : 10
if(x% 2 = = 0) 10 is even
cout<<x<< “ is even”; enter any no : 15
else 15 is odd
cout<<x<< “ is odd”;
getch( );
}

/* Write a program to print greatest of two nos. (using if-else) */


#include<iostream.h>
#include<conio.h>
void main( ) x y
{ int x,y; clrscr( ); 20 10
cout<<“enter two nos:”; cin>>x>>y; Output:
if ( x > y) enter two nos: 20 10
cout<<x; 20
else
cout<<y; enter two nos: 10 50
getch( ); 50
}
/* Write a program to check the given two nos are equal or not equal. (using if-else) */
#include<iostream.h>
#include<conio.h>
void main( ) x y
{ int x,y;
10 10
clrscr( );
cout<<“enter two nos:”; cin>>x>>y; Output:
if ( x = = y ) enter two nos: 10 10
cout<<“equal”; equal
else
cout<<“not equal”; enter two nos: 10 5
getch( ); not equal
}

else-if ladder: -
If-else is following with another if-else.

Syntax: -
if(cond)
statement1;
else if(cond)
statement2;
else if(cond)
statement3;
else if(cond)
:

/* Write a program to print greatest of three nos. (using else-if ladder) */


#include<iostream.h>
#include<conio.h>
void main( )
{ int x,y,z; x y z
clrscr( );
3 2 1
cout<<“enter three nos:”; cin>>x>>y>>z;
if ( x > y && x > z ) Output:
cout<<x; enter three nos: 3 2 1
else if(y > z) 3
cout<<y; enter three nos: 2 3 1
else 3
cout<<z;
getch( );
}

Nested if: -
If-else is following with another if-else.
Syntax: -
if(cond)
{
if(cond)
statement1;
}
/* Write a program to find total, percentage & grade of the student when three subjects are given.*/
#include<iostream.h>
#include<conio.h>
void main( )
{ int m1,m2,m3,tot; float per; Output:
clrscr( ); enter three sub marks: 80 80 80
cout<<“enter three sub marks :”; cin>>m1>>m2>>m3; m1 : 80
tot = m1 + m2 + m3; per = tot / 3.0; m2 : 80
cout<< “m1 :”<<m1<<” m2 :”<<m2<<” m3 :”<<m3<<endl; m3 : 80
cout<<“ tot :”<<tot<<” per :”<<per<<endl; tot : 240
if( m1 >= 40 && m2 >= 40 && m3 >= 40 ) per : 80.000000
{ if( per >= 75) distinction
cout<<“distinction”; m1 m2 m3
else if( per >= 60) 80 80 80
cout<<“1st class”;
else if( per >= 50) tot per
cout<<“2nd class”;
else
cout<<“3rd class”;
}
else
cout<<“fail”;
getch( );
}
While: -
Def: -
Check the condition, when condition is true execute the loop until the condition is false.
Syntax: -
while(cond) while(cond)
st1; (or) {
B.S.
}
/* Write a program to print 1,2,3,……… 10 */
#include<iostream.h> Output:
#include<conio.h> 1
void main( ) 2
{ int i=1; clrscr( ); 3
while(i<=10) i :
{ cout<<i<<endl; i=i+1; 1 10
} cout<<i++<<endl;
getch( );
}

/* Write a program to print 2,4,6,8,10 */


#include<iostream.h> Output:
#include<conio.h> 2
void main( ) 4
{ int i=2; clrscr( ); i 6
while(i<=10) 2 8
{ cout<<i<<endl; i= i+2; 10
}
getch( );
}
/* Write a program to print 1,3,5,7,9 */
#include<iostream.h>
#include<conio.h>
void main( )
{ int i=1; i Output:
clrscr( ); 1
while(i<=10) 1
{ cout<<i<<endl; i=i+2; 3
} 5
getch( ); 7
} 9
/* Write a program to print 10, 9, 8, …….. 1 */
#include<iostream.h>
#include<conio.h>
void main( )
{ int i=10; i Output:
clrscr( ); 10
while(i>=1) 10
{ cout<<i<<endl; i--; 9
} 8
getch( ); :
} 1
/* Write a program to print 1+2+3+……… +10 */
#include<iostream.h>
#include<conio.h>
void main( ) i sum
{ int i=1,sum=0; 1 0
clrscr( );
while(i<=10)
{ sum = sum + i; Output:
i++; sum :55
}
cout<<“sum :”<<sum;
getch( );
}
/* Write a program to print reverse of the given number. */
#include<iostream.h>
#include<conio.h>
void main( ) num rem sum
{ int num, rem, sum=0; 123 0
clrscr( );
cout<<“enter the number :”; cin>>num;
while(num>0) Output:
{ rem = num % 10; enter the number : 123
sum = (sum * 10)+ rem; reverse of the given number is : 321
num = num / 10; 123 0*10 + 3
} 12 3*10+2
cout<<“reverse of the given number is :”<<sum; 1 32*10+1
getch( ); 0 321
}
/* Write a program to print sum of the digits of the given numbers. */
#include<iostream.h>
#include<conio.h>
void main( ) num rem sum
{ int num, rem, sum=0; 123 0
clrscr( );
cout<<“enter the number :”; cin>>num;
while(num>0) Output:
{ rem = num % 10; enter the number : 123
sum = sum + rem; sum of the digits of the given number is :6
num = num / 10; : 234
} :9
cout<<“sum of the digits of the given number is :”<<sum;
getch( );
}
For: -
Syntax: -
for(Exp1;Exp2;Exp3) (or) for(Exp1;Exp2;Exp3)
statement1; {
B.S.

}
Exp1: - (Initialization)
Ex: - i=0; j=10; k=1; …….. etc.
Exp2: - (Condition)
Ex: - i<=10; j>=1; k<=100; …….. etc.
Exp3: - (Increment / Decrement)
Ex: - i++; j--; k++; …….. etc.
/* Write a program to print 1, 2, 3, ……… 10 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int i; i 1
clrscr( ); 2
1
for(i=1;i<=10;i++) :
cout<<i<<endl; 10
getch( );
}
/* Write a program to print 2, 4, 6, ……… 10 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int i; clrscr( ); i 2
for(i=2;i<=10;i=i+2) 4
2
cout<<i<<endl; :
getch( ); 10
}
/* Write a program to print 1+ 2 + 3+ ………+10 */
#include<iostream.h>
#include<conio.h> Output:
void main( ) i sum 55
{ int i,sum=0; 1 0
clrscr( );
for(i=1;i<=10;i++)
sum = sum + i;
cout<<sum; getch( );
}

/* Write a program to print 1, 2, 3, ……… n */


#include<iostream.h>
#include<conio.h>
void main( ) i n Output:
{ int i,n; 1 5 enter n : 5
clrscr( ); 1
cout<<“enter n :”; cin>>n; 2
for(i=1;i<=n;i++) :
cout<<i<<endl; 5
getch( );
}

/* Write a program to print 1+ 2 + 3+ ………+n */


#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int i,n,sum=0; clrscr( ); i sum enter n : 10
cout<<“enter n :”; cin>>n; 1 0 55
for(i=1;i<=n;i++) n
sum = sum + i; 5
cout<<sum; getch( );
}

do-while: -
Def: -Execute the loop, after execution of loop check the condition when condition is true execute
the loop until the condition is false.
syntax: -
do
{
B.S.

}while(cond);

/* Write a program to print 1, 2, 3, ….. 10 (using do-while) */


#include<iostream.h>
#include<conio.h>
void main( )
{ int i=1; clrscr( ); i
do 1 Output:
{ cout<<i<<endl; 1
i++; 2
} while(i<=10); :
getch( ); 10
}
/* Write a program to print sum of the given n numbers. */
#include<iostream.h>
#include<conio.h>
void main( )
{ int i=1,x,n,sum=0; i sum Output:
clrscr( ); 1 0 enter n : 5
cout<<“enter n :”; cin>>n; enter any no : 100
do n x enter any no : 66
{ cout<< “enter any no :” 5 100 enter any no : 74
cin>>x; enter any no : 87
sum = sum + x; i++; enter any no : 92
} while(i<=n); sum :
cout<<“sum :”<<sum; getch( );
}

switch: -
Select one statement from number of statements.
Syntax: -
switch (op)
{
case ‘char’ (or) const : statement 1; break;
case ‘char’ (or) const : statement 2; break;
case ‘char’ (or) const : statement 3; break;
:
default : statement n;
}
/* Simple program for switch. */
#include<iostream.h>
#include<conio.h>
void main( )
{ int n; clrscr( ); n
cout<<“enter no :”; cin>>n; 3 Output:
switch(n) enter no : 3
{ case 1 : cout<<“ one ”; break; three
case 2 : cout<<“ two ”; break;
case 3 : cout<<“ three ”; break;
case 4 : cout<<“ four ”; break;
default : cout<<“ wrong option ”;
}
getch( );
}

/* Write a program for add, subtract, multiple, divide and find remainder of the two numbers when
operator is given. */
#include<iostream.h> OUTPUT:
#include<conio.h> enter character [+ - * / %]: *
void main( ) enter two nos : 10 3
{ char op; 30
int x,y,a,b,c,e; float d;
clrscr( );
cout<<“enter character [+ - * / %]:”; cin>>op;
cout<<“enter two nos :”; cin>>x>>y;
switch(op)
{ case ’+’ : a = x+y; cout<<a; break;
case ’-’ : b = x-y; cout<<b; break;
case ’*’ : c = x*y; cout<<c; break;
case ’/’ : d = x/(float) y; cout<<d break;
case ’%’ : e= x%y; cout<<e; break;
default : cout<<“invalid option”;
}
getch( );
}

/* Write a program to print corresponding day for the given number (1 for Sunday) */
#include<iostream.h>
#include<conio.h>
enum day
{ sun, mon, tue, wed, thu, fri, sat
};
void main( )
{ int n;
clrscr( );
cout<<"enter n :"; cin>>n; output: -
switch(n) enter n : 1
{ case sun : cout<<"sun"; break; mon
case mon : cout<<"mon"; break;
case tue : cout<<"tue"; break;
case wed : cout<<"wed"; break;
case thu : cout<<"thu"; break;
case fri : cout<<"fri"; break;
case sat : cout<<"sat"; break;
default : cout<<"wrong option";
}
getch( );
}
ARRAYS

Def: -
Collection of similar or related data elements.

Declaration: -
i). Data type Variable[size]={values}; ii). Data type Variable[size];
int x[5]={10,20,30,40,50}; int x[3];
0 1 2 3 4 0 1 2
10 20 30 40 50
float a[3] = {1.1, 1.2, 1.3}; float a[100] ;
0 1 2 0 1 2 3 99
1.1 1.2 1.3
char s[10]={‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’}; char b[5];
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
a b c d e f g h i j

/* Simple program about array. */


#include<iostream.h>
#include<conio.h> 0 1 2 3 4
10 20 30 40 50
void main( )
{ int x[5]={10,20,30,40,50};
clrscr( ); OUTPUT:
cout<<”x[0] :”<<x[0]<<endl; x[0] : 10
cout<<”x[1] :”<<x[1]<<endl; x[1] : 20
cout<<”x[2] :”<<x[2]<<endl; x[2] : 30
cout<<”x[3] :”<<x[3]<<endl; x[3] : 40
cout<<”x[4] :”<<x[4]<<endl; x[4] : 50
getch( );
}
/* Write a program read five numbers store in array and print those five numbers. */
#include<iostream.h>
#include<conio.h> 0 1 2 3 4
10 20 30 40 50
void main( )
{ int x[5],i;
clrscr( ); Output:
cout<< “enter 5 eles :”; enter 5 eles : 10 20 30 40 50
for(i=0;i<=4;i++) eles :10 20 30 40 50
cin>>x[i]; //cin>>x[0]>>x[1]>>x[2]>>x[3]>>x[4];
cout<<“\n eles :”; // cout<<x[0]<< “ ”<<x[1]<< “ ”<<x[2]<< “ ”<<x[3]<< “ ”<<x[4];
for(i=0;i<=4;i++)
cout<<x[i]<<”\t”;
getch( );
}
/* Write a program to sort the given elements. */
#include<iostream.h>
#include<conio.h> 0 1 2 3 4
10 5 3 1 6
void main( ) x
{ int x[5],i,j,t;
clrscr( );
cout<<“enter 5 eles : ”; OUTPUT:
for(i=0;i<=4;i++) enter 5 eles : 10 5 3 1 6
cin>>x[i];

cout<<“before sorting eles : \n”; before sorting eles :


for(i=0;i<=4;i++) 10 5 3 1 6
cout<<x[i]<<”\t”; after sorting eles :
for(i=0;i<=3;i++) 1 3 5 6 10
{ for(j=i+1;j<=4;j++) i j 0 1 2 3 4
{ if(x[i]>x[j]) 0 1<=4 10 5 3 1 6
{ t=x[i]; 1 2<=4 5 10 3 1 6
x[i]=x[j]; 2 3<=4 3 10 5 1 6
x[j]=t; 3 4<=4 1 10 5 3 6
} 1 5 10 3 6
} 1 3 10 5 6
} 1 3 5 10 6
cout<<“\n after sorting eles :\n”; 1 3 5 6 10
for(i=0;i<=4;i++)
cout<<x[i];
getch( );
}

2D Array: -
Declaration: -
Data Type variable[size][size];
Ex: - int x[60][8];
float a[4][2];
char std[100][20];

5. Write a program to add two matrices. (3*3) with (3*3)


void main( )
{ int x[3][3],y[3][3],z[3][3],i,j; clrscr( ); Output:
cout<<“ enter 9 eles in x : ”; enter 9 eles in x : 1 2 3 4 5 6 7 8 9
for(i=0;i<=2;i++) i j enter 9 eles in y : 1 1 1 1 1 1 1 1 1
for(j=0;j<=2;j++) 0 0 eles in x :
cin>>x[i][j]; 0 1 0 1 2
cout<<“enter 9 eles in y :”; 0 2 1 2 3 0 00 01 02
for(i=0;i<=2;i++) 4 5 6 1 10 11 12
for(j=0;j<=2;j++) 7 8 9 2 20 21 22
cin>>y[i][j];
for(i=0;i<=2;i++) eles in y :
for(j=0;j<=2;j++) 1 1 1
z[i][j]= x[i][j] + y[i][j]; 1 1 1
cout<<“ \n eles in x : \n”; 1 1 1
for(i=0;i<=2;i++) eles in z :
{ for(j=0;j<=2;j++) 2 3 4
cout<<x[i][j]<< “\t”; 5 6 7
cout<<“\n”; 8 9 10
}
cout<<“ \n eles in y : \n”; 11 12 13
for(i=0;i<=2;i++) 21 22 23
{ for(j=0;j<=2;j++) 31 32 33
cout<<y[i][j]<< “\t”;
cout<<“\n”;
}
cout<<“ \n eles in z : \n”;
for(i=0;i<=2;i++)
{ for(j=0;j<=2;j++)
cout<<z[i][j]<< “\t”;
cout<<“\n”;
}
getch( );
}

6. Write a program to print row and column sum of the given matrix. (3*3)
void main( )
{ int x[3][3],rsum,csum,i,j,tsum=0;
clrscr( ); Output:
cout<<“ enter 9 eles in x : ”; enter 9 eles in x : 1 2 3 4 5 6 7 8 9
for(i=0;i<=2;i++) 1 2 3 =6
for(j=0;j<=2;j++) 4 5 6 =15
cin>>x[i][j]; 7 8 9 =24 0 1 2
12 15 18 =90 0 00 01 02
for(i=0;i<=2;i++) 1 10 11 12
{ rsum=0; 2 20 21 22
for(j=0;j<=2;j++)
{ cout<<x[i][j]<<”\t”;
rsum=rsum+x[i][j];
}
cout<<”=”<<rsum<<”\n”;
tsum=tsum+rsum;
}

for(i=0;i<=2;i++)
{ csum=0;
for(j=0;j<=2;j++)
{ csum=csum+x[j][i];
}
cout<<csum<<”\t”;
tsum=tsum+csum;
}
cout<<”=”<<tsum;
getch( );
}
FUNCTIONS

Def: - Sub program.

Function is sub program. It having own name and block of statements. When it is called from main it
will be executed.

main

function

function

function

Types of functions: -

1. Passing by arguments & Return type


2. Passing by arguments & Non-return type
3. Passing by reference & Return type
4. Passing by reference & Non-return type

1. Simple program on function.

void main( ) function declaration Output:


{
void function( ); we are in main
clrscr( ); we are in function
cout<<“\n we are in main”; we are in main
function( ); calling of function
cout<<“\n we are in main”;
getch( );
}

void function( ) called function (or) definition of function


{
cout<<“\n we are in function”;
}
2. Write a program to print area of the circle. (Using function)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void area( ); enter r : 1
clrscr( ); area of the circle : 3.140000
area( ); getch( );
}
void area( )
{ int r; float a; r a
cout<<“enter r :”; cin>>r; 1
a= 3.14 * r * r;
cout<< “area of the circle :”<<a;
}

3. Write a program to swapping or interchanging of two numbers. (Using function)


#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void swap( ); enter two nos : 10 20
clrscr( ); x : 10 y : 20
swap( ) x : 20 y : 10
getch( );
}
void swap( )
{ int x,y; x y
cout<<“enter two nos :”; cin>>x>>y; 10 20
cout<<”x :”<<x<<” y:”<<y<<endl;
x=x+y;
y=x-y;
x=x-y;
cout<<”x :”<<x<<” y:”<<y<<endl;
}

4. Write a program to print reverse of the given number. (Using function)


#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void reverse( ); enter no : 123
clrscr( ); reverse of the given no :321
reverse( ); getch( );
}
void reverse( )
{ int num,rem,sum=0; num rem sum
cout<<“enter no :”; cin>>num; 123 0
while(num>0)
{ rem = num % 10;
sum = (sum * 10) + rem;
num = num / 10;
}
cout<<“reverse of the given no :”<<sum;
}
5. Write a program to print area of the circle. (Using passing by argument & non-return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void area( int r ); enter r : 1
int r; r area of the circle : 3.140000
clrscr( );
1
cout<<“enter r :”;
cin>>r;
area( r ); actual parameter
getch( );
}
void area( int r ) formal parameter r a
{ 1
float a; local variable
a= 3.14 * r * r;
cout<<“area of the circle :”<<a;
}

6. Write a program to swapping or interchanging of two numbers. (Using passing by argument &
non-return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void swap( int x, int y ); enter two nos : 10 20
int a,b;
clrscr( ); a : 10 b : 20
cout<<“enter two nos :”; a : 20 b : 10
cin>>a>>b;
cout<<“ a :”<<a<<” b :”<<b<<endl; a b
swap( a,b); 10 20
getch( );
}
void swap( int x, int y )
{ x=x+y; x y
y=x-y; 10 20
x=x-y;
cout<<“ a :”<<x<<” b :”<<y<<endl;
}

7. Write a program to print reverse of the given number. (Using passing by argument &
non-return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void reverse( int num ); enter no : 123
int n; reverse of the given no :321
clrscr( );
cout<<“enter no :”; cin>>n;
reverse( n ); n
getch( ); 123
}
void reverse( int num )
{ int rem,sum=0; num rem sum
while(num>0) 123 0
{ rem = num % 10;
sum = (sum * 10) + rem;
num = num / 10;
}
cout<<“reverse of the given no :”<<sum;
}

8. Write a program to print reverse of the given number. (Using passing by argument & return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ int reverse( int num ); enter no : 123
int n,t; reverse of the given no :321
clrscr( );
cout<<“enter no :”; cin>>n; n
t=reverse( n ); 123
cout<<“reverse of the given no :”<<t;
getch( );
}
int reverse( int num )
{ int rem,sum=0; num rem sum
while(num>0) 123 0
{ rem = num % 10; 0 321
sum = (sum * 10) + rem;
num = num / 10;
}
return(sum);
}

9. Write a program to print area of the circle. (Using passing by argument & return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ float area( int r ); enter r : 1
int r; float a; area of the circle : 3.140000
clrscr( ); r a
cout<<“enter r :”; cin>>r;
1
a = area( r );
cout<<“area of the circle :”<<a;
getch( );
}
float area( int r ) r a
{ float a= 3.14 * r * r; 1
return( a );
}
10. Write a program add, subtract, multiply, divide and find reminder of two numbers when
operator is given ( + - * / %).
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ void add(int x, int y); void sub(int x, int y); enter op [+ - * / %]: +
void mul(int x, int y); void div(int x, int y); ente two nos: 10 3
void rem(int x, int y); 13
int a,b; char op; clrscr( );
cout<<“enter op [+ - * / %] :”; cin>>op;
cout<<“enter two nos:”; cin>>a>>b;
switch( op )
{ case ‘+’ : add(a,b); break;
case ‘-’ : sub(a,b); break;
case ‘*’ : mul(a,b); break;
case ‘/’ : div(a,b); break;
case ‘%’ : rem(a,b); break;
default : cout<<“ enter correct option”;
}
getch( );
}
void add(int x, int y)
{ int z; z = x + y; cout<<z; }
void sub(int x, int y)
{ int z; z = x - y; cout<<z; }
void mul(int x, int y)
{ int z; z = x * y; cout<<z; }
void div(int x, int y)
{ float z; z = x /(float) y; cout<<z; }
void rem(int x, int y)
{ int z; z = x % y; cout<<z; }

inline function:
Write a program to add and multiple two numbers using inline function.
#include<iostream.h>
#include<conio.h>
inline int add(int x, int y) Output:
{
int z; 30
z=x+y; 20
return z;
}
inline int mul(int x, int y)
{
int z=x*y;
return z;
}
void main( )
{
clrscr( ); //x=add(10,2); y=mul(12,2);
cout<<add(10,20)<<endl; //cout<<x;
cout<<mul(10,2)<<endl;
getch( );
}

Reference Variable

Write a program to swapping or interchanging of two numbers. (Using passing by reference)


#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void swap( int &x, int &y ); enter two nos : 10 20
int a,b;
clrscr( ); a : 10 b : 20
cout<<“enter two nos :”; a : 20 b : 10
cin>>a>>b;
cout<<“ a :”<<a<<” b :”<<b<<endl; a b
swap( a,b); 10 20
cout<<“ a :”<<a<<” b :”<<b<<endl;
getch( );
}
void swap( int &x, int &y )
{ x=x+y; x y
y=x-y; 10 20
x=x-y;
}

Function overload:
Def: Name of the functions are same or equal but perform different task.

Note: At least one argument must be different.

1. Write a program to swap two numbers and two characters using function overload.
#include<iostream.h>
#include<conio.h>
void swap( int x, int y )
{ x=x+y; x y
y=x-y; x=x-y; 10 20
cout<<x<<” ”<<y<<endl;
}
void swap( char x, char y )
{ char t; x a y b
t=x;
x=y; y=t;
cout<<x<<” ”<<y<<endl; t
}

void main( ) Output :


{ char m,n; enter two characters : a b
int a,b; enter two nos : 10 20
clrscr( ); 10 20
cout<<“enter two characters :”; cin>>m>>n; a b
cout<<“enter two nos :”; cin>>a>>b; 20 10
cout<<a<<” ”<<b<<endl; b a
cout<<m<<” ”<<n<<endl;
swap( a,b); swap( m,n); a 10 b 20
getch( );
} m n

2. Write a program to area of the circle and area of the rectangle using function overload.
#include<iostream.h>
#include<conio.h>
void area(int r)
{ float a=3.14*r*r;
cout<<a<<endl;
}
void area(int l,int b) Output:
{ int a=l*b; 3.14
cout<<a<<endl; 200
}
void main( )
{ clrscr( );
area(1); area(10,20);
getch( );
}
STRINGS

Def: - Collection of characters.

Declaration : -
i) D.T. v[size]=”string”; ii) D.T. v[size];
ex:- ex:-
char x[5]=”gopi”; char x[10];
0 1 2 3 4 0 1 2 3 4 5 6 7 8 9
g o p i ‘\0’
char y[10]=”abc”; char a[20];
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 ----- 19
a b c ‘\0’

‘\0’  NULL Character

1. Simple program on strings.


#include<iostream.h>
#include<conio.h>
void main( )
{ char x[10] = “gopi”; 0 1 2 3 4 5 6 7 8 9
clrscr( ); x g o p i ‘\0’
cout<<“string :”<<x;
getch( ); Output: string : gopi
}

2. Simple program on strings.


#include<iostream.h>
#include<conio.h> 0 1 2 3 4 5 6 7 8 9 ----- 19
void main( ) x
g o p i ‘\0’
{ char x[20];
clrscr( );
cout<<“enter string :”; Output:
cin>>x; // cin.getline(x,20); enter string : gopi nlg
cout<<“string :”<<x; string : gopi
getch( ); // gets(x);
}

String functions: -

1. strlen( )
2. strcpy( )
3. strcmp( )
4. strcat( ) <string.h>

3. Write a program to find string length. [Using string function strlen( )]


#include<iostream.h>
#include<conio.h>
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12
void main( ) x
g o p i ‘\0’
{ char x[20]; int len;
clrscr( ); Output: len
cout<< “enter string :”; cin>>x; enter string : gopi
len = strlen(x); string : gopi
cout<<“ string :”<<x<<endl; length : 4
cout<<”length :”<<len; getch( );
}

4. Write a program to copy string one variable to other. [Using string function strcpy( )]
#include<iostream.h>
#include<conio.h>
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12
void main( ) x
g o p i ‘\0’
{ char x[20],y[20];
clrscr( ); 0 1 2 3 4 5 6 7 8 9 10 11 12
cout<< “enter string :”; cin>>x; y
strcpy(y,x);
cout<<“x :”<<x<<” y:”<<y; Output:
getch( ); enter string : gopi
} x : gopi y : gopi

5. Write a program to compare to strings. [Using string function strcmp( )]


#include<iostream.h>
#include<conio.h>
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12
void main( ) x
g o p i ‘\0’
{ char x[20],y[20]; int a;
clrscr( ); 0 1 2 3 4 5 6 7 8 9 10 11 12
cout<< “enter two strings:”; y
g o p i ‘\0’
cin>>x>>y;
a = strcmp(x,y);
if( a = = 0) Output:
cout<<“ equal ”; enter two string : gopi gopi
else equal
cout<<“ not equal ”;
getch( );
}

6. Write a program to combine two strings. [Using string function strcat( )]


#include<iostream.h>
#include<conio.h>
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12
void main( ) x
g o p i ‘\0’
{ char x[20],y[20];
clrscr( ); 0 1 2 3 4 5 6 7 8 9 10 11 12
cout<< “enter two strings:”; y
k r i s h n a ‘\0’
cin>>x>>y;
cout<<“x :”<<x<<” y:”<<y<<endl; Output:
strcat(x,” ”); strcat(x,y); enter two string : gopi krishna
cout<<“x :”<<x<<” y:”<<y; x : gopi y : krishna
getch( ); x : gopikrishna y : krishna
}
7. Write a program to check the given string is palindrome or not.
#include<iostream.h>
#include<conio.h>
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12
void main( ) x
m a d a m ‘\0’
{ char x[20],y[20];
clrscr( ); 0 1 2 3 4 5 6 7 8 9 10 11 12
cout<<“enter string:”; cin>>x; y
strcpy(y,x);
strrev(y); Output:
if( strcmp(x,y) = = 0) enter string : madam
cout<<x<<” is palindrome ”; madam is palindrome
else
cout<<x<<” is not palindrome ”;
getch( );
}

strrev(x); x=dcba
strupr(x);
strlwr(x);
CLASS

Def:
A class is a way of binding the data and associated member functions in a single unit.
Object:
Instance of a class is known as object (Instance is a mechanism of allocating sufficient
amount of memory space for data members of a class)
Class variable is known as an object (or) blue print of a class is known as an object or value
of a class is known as a object or real world entities of a class.
Data Abstraction:
It is a mechanism of retrieving the essential details without dealing with background details.
Encapsulation:
It is the process of wrapping up on data and associated methods in a single unit. It is basically
used for achieving data/information hiding i.e. security.
Declaration:
class class_name
{ protected:
instance variables
member functions
private:
instance variables
member functions
public:
instance variables
member functions
};
class_name obj_list;

1. Write a program to print student details using class.


#include<iostream.h>
#include<conio.h>
class student rno name per
{ private:
int rno; s
char name[20];
float per;
public: Output:
void read( ) enter rno : 1
{ cout<<“enter rno :”; cin>>rno; enter name : gopi
cout<<“enter name :”; cin>>name; enter per : 70
cout<<“enter per :”; cin>>per; rno :1
} name : gopi
void display( ) per : 70.00
{ cout<<“rno :”<<rno<<endl;
cout<<“name :”<<name<<endl;
cout<<“per :”<<per<<endl;
}
};

void main( )
{ student s;
clrscr( );
s.read( ); s.display( );
getch( );
}
2. Write a program to print book details using class.
#include<iostream.h>
#include<conio.h>
class book name author price
{ private:
char name[20]; b b
char author[20];
float price;
public: Output:
void read( ) enter name : c
{ cout<<“enter name :”; cin>>name; enter author : swamy
cout<<“enter author :”; cin>>author; enter price : 300
cout<<“enter price :”; cin>>price; name : 1
} author : gopi
void display( ) price : 300.00
{ cout<<“name :”<<name<<endl;
cout<<“author :”<<author<<endl;
cout<<“price :”<<price<<endl;
}
};
void main( )
{ book b; clrscr( );
b.read( ); b.display( );
getch( );
}

3. Write a program to print employee details using class.


#include<iostream.h>
#include<conio.h>
class employee name design sal
{ private:
char name[20],design[20]; e
float sal;
public: Output:
void getdetails( ) enter name : ravi
{ cout<<“enter name :”; cin>>name; enter design : jr.asst.
cout<<“enter design :”; cin>>design; enter sal : 30000
cout<<“enter sal :”; cin>>sal; name : ravi
} design : jr.asst.
void display( ); sal : 30000
};
void employee :: display( ) // void display( )
{ cout<<“name :”<<name<<endl;
cout<<“design :”<<design<<endl;
cout<<“sal :”<<sal<<endl;
}

void main( )
{ employee e;
clrscr( );
e.getdetails( ); e.display( );
getch( );
}
4. Write a program to print super market details using class.
#include<iostream.h>
#include<conio.h>
class market item qty price totamt
{ char item[20];
int qty; float price,totamt; m
public: Output:
void getitem( ) enter item : pen
{ enter qty : 5
cout<<“enter item :”; cin>>item; enter price : 10
cout<<“enter qty :”; cin>>qty;
cout<<“enter price :”; cin>>price; item : pen
} qty :5
void display( ); price : 10
}; totamt: 50
void market :: display( )
{ totamt=qty*price;
cout<<“item :”<<item<<endl;
cout<<“qty :”<<qty<<endl;
cout<<“price :”<<price<<endl;
cout<<“totamt:”<<totamt;
}
void main( )
{ market m;
clrscr( );
m.getitem( ); m.display( );
getch( );
}
5. Write a program to print student details using class and passing by argument.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{ int rno; char name[20]; rno name per
float per;
public: s
void read(int r,char n[ ],float p)
{ rno=r; strcpy(name,n); Output:
per=p; enter rno : 1
} enter name : gopi
void display( ) enter per : 70
{ cout<<“rno :”<<rno<<endl; rno :1
cout<<“name :”<<name<<endl; name : gopi
cout<<“per :”<<per<<endl; per : 70.00
}
};

void main( )
{ int r; char n[20]; float p;
student s;
clrscr( );
cout<<“enter rno :”; cin>>r;
cout<<“enter name :”; cin>>n;
cout<<“enter per :”; cin>>p;
s.read(r,n,p); s.display( );
getch( );
}

6. Write a program to print super market details using class.


#include<iostream.h>
#include<conio.h>
#include<string.h>
class market
{ char item[20]; int qty; item qty price totamt
float price,totamt;
public: m
void getitem(char it[ ],int q,float p)
{ Output:
strcpy(item,it); enter item : pen
qty=q; price=p; enter qty : 5
totamt=qty*price; enter price : 10
}
void display( )
{ item : pen
cout<<“item :”<<item<<endl; qty :5
cout<<“qty :”<<qty<<endl; price : 10
cout<<“price :”<<price<<endl; totamt: 50
cout<<“totamt:”<<totamt;
}
};
void main( )
{ market m;
char it[20]; int q; float p; clrscr( );
cout<<“enter item :”; cin>>it;
cout<<“enter qty :”; cin>>q;
cout<<“enter price :”; cin>>p;
m.getitem(it,q,p); m.display( ); getch( );
}

Nested of member functions:

/* Largest of three numbers */


#include<iostream.h>
#include<conio.h>
class number
{ int x,y,z;
public:
void input( ); void display( ); int large( );
};

void number :: input( )


{ cout<<"enter 3 numbers :"; cin>>x>>y>>z;
}
void number :: display( )
{ cout<<"large :"<<large( ); Output:
}
int number :: large( ) enter 3 numbers: 3 2 1
{ if(x>y && x>z) large: 3
return(x);
else if(y>z)
return(y);
else
return(z);
}
void main( )
{ number n; clrscr( );
n.input( ); n.display( );
getch( );
}
/* Fibonacii series */
#include<iostream.h>
#include<conio.h>
class fib Output:
{ int a,b,c;
public: enter n : 5
void setfib( ) 0 1 1 2 3
{ a=0,b=1; }
void fibo(int n);
};
void fib :: fibo(int n)
{ cout<<a<<” ”<<b<<”\t”;
for(int i=1;i<=n-2;i++)
{ c=a+b; cout<<c<<"\t";
a=b; b=c;
}
}
void main( )
{ fib f; int n; clrscr( );
cout<<"enter n :"; cin>>n;
f.setfib( ); f.fibo(n);
getch( );
}
/* Friend function */
#include<iostream.h>
#include<conio.h>
class addition Output:
{ int x,y;
public: enter two numbers: 10 5
void write(int a,int b) 15
{ x=a; y=b;
}
friend int add(addition a);
};

int add(addition a)
{ return(a.x+a.y);
}

void main( )
{ addition a;
int p,q,k;
clrscr( );
cout<<"enter two numbers :"; cin>>p>>q;
a.write(p,q);
k=add(a);
cout<<k;
getch( );
}

Array of objects
#include<iostream.h>
#include<conio.h>
class employee Output:
{ int id; char *name; float sal;
public: enter emp id :
void getdata( ); void print( ); enter emp name:
}; enter emp sal:
void employee :: getdata( ) :
{ cout<<"enter emp id :"; cin>>id; :
cout<<"enter emp name :"; cin>>name;
cout<<"enter emp sal :"; cin>>sal; emp id :
} emp name :
void employee :: print( ) emp sal :
{ cout<<"emp id :"<<id<<endl;
cout<<"emp name :"<<name<<endl;
cout<<"emp sal :"<<sal<<endl;; id name sal
}
const int size = 3; 0 101 aaa 10000
void main( ) 1 102 bbb 12000
{ employee e[size]; int i; 2 103 ccc 15000
clrscr( );
for(i=0;i<=2;i++)
e[i].getdata( );
for(i=0;i<=2;i++)
e[i].print( );
getch( );
}
Constructers and Destructors

Def: Name of the class and its function(s) names are equal. When object is created automatically
function will be invoke (called).

Types of Constructors:
i) Default Constructors ii) Parameterized Constructors

Declaration: (Default Constructors)


class class_name
{ class_name( )
{ - - - (block of statements)
- --
}
}

Declaration: (Parameterized Constructors)


class class_name
{ class_name(list of parameters)
{ - - - (block of statements)
- --
}
}
/* Sample Program */
#include<iostream.h>
#include<conio.h>
int count=0;
class constr
{ public:
constr( )
{ count++; cout<<"no .of object created :"<<count<<endl;
}
~constr( )
{ cout<<"no .of object destroyed :"<<count<<endl; count--;
}
};
void main( )
{ clrscr( );
constr a,b,c;
getch( );
}
/* Multiply two numbers using constructors. */
#include<iostream.h>
#include<conio.h>
class multiply
{ int x,y; Output:
public:
multiply(int a,int b) 14
{ x=a; y=b;
} // multiply() { x=0; y=0; }
int display( )
{ return (x*y);
}
};
void main( )
{ clrscr( );
multiply a(2,7); cout<<a.display( ); // int t=a.display( ); cout<<t;
getch( );
}

Default Arguments
/* Multiply two numbers using Default Arguments constructors. */
#include<iostream.h>
#include<conio.h>
class multiply
{ int x,y;
public: Output:
multiply(int a=0,int b=0)
{ x=a; y=b; 14
} 0
int display( )
{ return (x*y);
}
};
void main( )
{ clrscr( );
multiply m(2,7); cout<<m.display( )<<endl;
multiply n; cout<<n.display( )<<endl;
getch( );
}
/* Area of the circle using constructor */
#include<iostream.h>
#include<conio.h>
class area
{ float r,a;
public:
area( )
{ r=1; }
area(float r1); // area(float r1)
void cal( ) // {
{ a=3.14*r*r; cout<<a<<"\n"; // r=r1;
} // }
};
area :: area(float r1) Output:
{ r = r1;
} 12.56
void main( ) 3.14
{ clrscr( );
area ac1(2); ac1.cal( );
area ac2; ac2.cal( );
getch( );
}
This:
#include<iostream.h>
#include<conio.h>
class area
{ float r,a;
public:
area( )
{ r=1;
}
area(float r);
void cal( )
{
a=3.14*r*r; cout<<a<<"\n";
}
};
area :: area(float r)
{
this->r = r; // r= r;
}
void main( ) Output:
{ clrscr( );
area a(2); a.cal( ); 12.56
getch( );
}

Memory Management Operators:


new:
pointer-variable = new data-type;
Ex:- int *p; p = new int; *p=20;
float *q; q = new float; *q=1.23;

int *p=new int; *p=25;


float *q = new float; *q=7.5;

pointer-variable = new data-type(value);


Ex:- int *p=new int(25);
float *a = new float(7.5);

pointer-variable = new data-type[size];


Ex:- int *p=new int[10];
float *q = new float[5];

delete:
delete pointer-variable;
Ex:- delete p;
delete q;

delete [size]pointer-variable;
Ex:- delete [ ]p;
delete [ ]q;
/* Sample program1 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int *p;
clrscr( ); 100
p = new int; *p = 100;
cout<<"*p :"<<*p;
delete p;
getch( );
}

/* Sample program2 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int *p;
clrscr( ); 10
p = new int(10); cout<<"*p :"<<*p;
delete p;
getch( );
}

Allocating Arrays
var = new arr_type[size];
delete [ ]var;

Ex:-
x = new int[10];
delete [ ]x;

/* Sample program3 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int *p,i;
clrscr( ); 1
p = new int[10]; 2
for(i=0;i<10;i++) :
p[i] = i+1; :
for(i=0;i<10;i++) 10
cout<<p[i]<<endl;
delete [ ]p; p 0 1 2 9
getch( ); 1 2 3 10
}
Operator Overloading

Def: Function call on operator. --x;


Like +, -, ++, --, = =, ….. etc c=a-b;

/* Write a program to display negation of the given numbers using operator overloading */
#include<iostream.h>
#include<conio.h> Output:
class negitive
{ int x,y,z; x : 10 y : 20 z : 30
public: x : -10 y : -20 z : -30
void getdata(int a,int b,int c);
void display( );
void operator -( );
};
void negitive :: getdata(int a,int b,int c)
{ x = a; y = b; z = c;
}
void negitive :: display( )
{ cout<<" x : "<<x<<" y :"<<y<<" z :"<<z<<endl;
}
void negitive :: operator -( )
{ x = -x; y = -y; z = -z;
}
void main( )
{ negitive n;
clrscr( );
n.getdata(10,20,30); n.display( );
-n; n.display( );
getch( );
}

/* Write a program to check the given two students marks are equal or not equal using operator
overloading */
#include<iostream.h>
#include<conio.h> Output:
class equal
{ int m1,m2; m1 : 90 m2 : 100
public: m1 : 80 m2 : 90
equal( ) not equal
{ m1=0; m2=0; }
equal(int a,int b);
void display( );
int operator = =(equal s);
};
equal :: equal(int a,int b)
{ m1=a; m2=b;
}
void equal :: display( )
{ cout<<”m1 :”<<m1<<”m2:”<<m2<<endl;
}
int equal :: operator ==(equal s)
{ if( (m1= =s.m1) && (m2 = = s.m2) )
return (1);
else
return(0); // while(1)
} // cout<<”gopi\n”;

void main( )
{ clrscr( );
equal s1(90,100), s2(80,90);
s1.display( ); s2.display( );
if(s1= =s2)
cout<<”equal”;
else
cout<<”not equal”;
getch( );
}

/*Write a program to check the given two strings are equal or not equal using operator overloading */
#include<iostream.h>
#include<string.h>
#include<conio.h> Output:
class equal
{ char name[20]; name : gopi
public: name : krishna
equal( ) not equal
{ strcpy(name,” ”); }
equal(char s[ ])
{ strcpy(name,s);
}
void display( );
int operator = =(equal s);
};
void equal :: display( )
{ cout<<”name :”<<name<<endl;
}
int equal :: operator ==(equal s)
{ if( strcmp(name,s.name) = = 0)
return (1);
else
return(0);
}
void main( )
{ clrscr( );
equal s1(“gopi”), s2(“krishna”);
s1.display( ); s2.display( );
if(s1= =s2)
cout<<”equal”;
else
cout<<”not equal”;
getch( );
}

/* Add two complex numbers using operator overloading */


#include<iostream.h>
#include<conio.h>
class complex
{ float x,y;
public:
complex( ) { x=0;y=0; }
complex(float a,float b)
{ x = a; y = b;
}
void display( )
{ cout<<x<<" +i"<<y<<endl;
}
complex operator +(complex);
};
complex complex :: operator +(complex c)
{ complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return(temp);
}
void main( ) Output:
{
complex c1(1,3),c2(2,4),c3; 1+i3
clrscr( ); 2+i4
//c1 = complex(1,3); 3+i7
//c2 = complex(2,4);
c3 = c1 + c2;
cout<<"c1 :"; c1.display( );
cout<<"c2 :"; c2.display( );
cout<<"c3 :"; c3.display( );
getch( );
}
INHERITANCE

Def:
The process of acquiring the properties of base class to derived class is known as Inheritance.

Note: Private data members and private methods of the base class cannot be inherited at all.

Types of Inheritance:
i) Single Inheritance ii) Multiple Inheritance
iii) Multilevel Inheritance iv) Hierarchical Inheritance
v) Hybrid Inheritance

1. Single inheritance 2. Multiple Inheritance

A A B

B C

3. Hierarchical Inheritance 4. Multilevel Inheritance

A A

B C D B

C
5. Hybrid Inheritance

B C

Single Inheritance:
The process of deriving the properties of class B from class A [existing class]. In this, we
have only one base class and one derived class.

Syntax:
class Base_classname
{ instance variables;
function definition
{ ----
}
}
class Derived_classname : access-specifier Base_classname
{ instance variables;
function definition
{ ----
}
}
/* Write a program to print student details using single inheritance */
#include<iostream.h>
#include<conio.h>
class student
{ protected :
int rno; char name[20];
public :
void read1( )
{ cout<<"enter rno :"; cin>>rno;
cout<<"enter name :"; cin>>name;
}
void display1( )
{
cout<<"rno :"<<rno<<endl;
cout<<"name :"<<name<<endl;
}
};
class address : public student
{ protected :
char fname[20],hno[20],dist[20];
public :
void read2( )
{ read1( );
cout<<"enter fname :"; cin>>fname;
cout<<"enter hno :"; cin>>hno;
cout<<"enter dist :"; cin>>dist;
}
void display2( )
{ display1( );
cout<<"fname :"<<fname<<endl;
cout<<"hno :"<<hno<<endl;
cout<<"dist :"<<dist<<endl;
}
};
void main( )
{ clrscr( );
address a;
a.read2 ( ); a.display2( ); // a.read1(); a.display1();
getch( );
}

/* Sample program on Single inheritance */


#include<iostream.h>
#include<conio.h>
class A
{ int x;
public : Output:
int y; x : 10
void readxy( ) y : 20
{ x =10; y = 20; z : 200
} x : 10
int getx( ) y : 10
{ return(x); z : 100
}
};
class B : public A
{ int z;
public :
void mul ( )
{ z = y * getx( );
}
void display( )
{
cout<<"x :"<<getx( )<<endl;
cout<<"y :"<<y<<endl;
cout<<"z :"<<z<<endl;
}
};
void main( )
{ B b;
clrscr( );
b.readxy( ); b.mul( ); b.display( );
b.y=10; b.mul( ); b.display( );
getch( );
}

/* Sample program on Single inheritance */


#include<iostream.h>
#include<conio.h>
class A Output:
{ enter two nos : 1 2
int x; x:1
public : y:2
int y; z:2
void readxy( ) enter two nos : 2 3
{ cout<<"enter two nos :"; cin>>x>>y; x:2
} y:3
int getx( ) z:6
{ return(x);
}
};
class B : private A
{ int z;
public :
void mul ( )
{ readxy( );
z = y * getx( );
}
void display( )
{ cout<<"x :"<<getx( )<<endl;
cout<<"y :"<<y<<endl;
cout<<"z :"<<z<<endl;
}
};
void main( )
{ B b;
clrscr( );
b.mul( ); b.display( );
getch( );
}

Multiple Inheritance

Multiple of based classes are inherited to single derived class

/* Sample program on Multiple Inheritance */


#include<iostream.h>
#include<conio.h>
class M
{ protected : Output:
int x; x : 10
public : y:2
void get_x(int a) x * y : 20
{ x = a;
}
};
class N
{ protected :
int y;
public :
void get_y(int b)
{ y = b;
}
};
class P : public M, public N
{ public :
void display( )
{ cout<<"x :"<<x<<endl;
cout<<"y :"<<y<<endl;
cout<<"x * y :"<<x*y<<endl;
}
};
void main( )
{ P p;
clrscr( );
p.get_x(10); p.get_y(2);
p.display( );
getch();
}
Multilevel Inheritance
The process of derived class is as base class for another derived class.
/* Students details using multilevel inheritance */
#include<iostream.h>
#include<conio.h>
class student
{ protected :
int rno; char name[20];
public :
void read1( )
{ cout<<"enter rno :"; cin>>rno;
cout<<"enter name :"; cin>>name;
}
};
class marks : public student
{ protected :
int m1,m2,m3,tot;
public :
void read2( )
{ cout<<"enter 3 sub marks :";
cin>>m1>>m2>>m3; tot = m1 + m2 + m3;
}
};
class percentage : public marks
{ float per;
public :
void cal( )
{ per = tot / 3.0;
}
void display( )
{ cout<<"rno :"<<rno<<endl; cout<<"name :"<<name<<endl;
cout<<"m1 :"<<m1<<endl; cout<<"m2 :"<<m2<<endl;
cout<<"m3 :"<<m3<<endl; cout<<"tot :"<<tot<<endl;
cout<<"per :"<<per<<endl;
}
};
void main( )
{ percentage p;
clrscr( );
p.read1( ); p.read2( );
p.cal( ); p.display( );
getch( );
}

Hybrid Inheritance
In this inheritance we have only single base and more than one derived class.

/* Sample program on Hybrid Inheritance */


#include<iostream.h>
#include<conio.h>
class student
{ protected :
int rno;
char name[20];
public :
void read1( )
{ cout<<"enter rno :"; cin>>rno;
cout<<"enter name :"; cin>>name;
}

void display1( )
{ cout<<"rno :"<<rno<<endl;
cout<<"name :"<<name<<endl;
}
};
class marks : public student
{ protected :
int m1,m2,m3,tot;
public :
void read2(int x,int y,int z)
{ m1 = x; m2 = y; m3=z;
tot = m1 + m2 + m3;
}
void display2( )
{ cout<<"m1 :"<<m1<<endl;
cout<<"m2 :"<<m2<<endl;
cout<<"m3 :"<<m3<<endl;
cout<<"tot :"<<tot<<endl;
}
};
class sports Output: enter rno : 1
{ protected : enter name : gopi
char game[20]; enter game : cricket
int score; enter score : 100
public : rno : 1
void read3( ) name :gopi
{ cout<<"enter game :"; cin>>game; m1 : 90
cout<<"enter score :"; cin>>score; m2 : 90
} m3 : 90
void display3( ) tot : 270
{ cout<<"game :"<<game<<endl; game : cricket
cout<<"score :"<<score<<endl; score : 100
} total : 370
};

class result : public marks, public sports


{ float per;
public:
void display4( )
{ per = tot/3.0;
display1( ); display2( ); display3( );
cout<<"per :"<<per<<endl;
}
};
void main( )
{ result r; clrscr( );
r.read1( ); r.read2(90,90,90); r.read3( ); r.display4( );
getch( );
}
/* Virtual Function */
class Base
{ public:
void display( )
{ cout<<”we are in base class display function\n”; }
virtual void show( )
{ cout<<”we are in base class show function\n”; }
};
class Derived : public Base
{ public:
void display( )
{ cout<<”we are in derived class display function\n”; }
void show( )
{ cout<<”we are in derived class show function\n”; }
}; Output:
void main( ) we are in base class display function
{ clrscr( ); we are in base class show function
Base b, *ptr; Derived d; we are in base class display function
ptr = &b; ptr->display( ); ptr->show( ); we are in derived class show function
ptr = &d; ptr->display( ); ptr->show( );
getch( );
}

Template: Template is a variable, we can store any type of value in that variable in any time.

template <class data_type> // x=10; cout<<x<<endl;


data_type variable; // x=1.23; cout<<x<<endl;
ex:-
template <class nlg>
nlg x; // x is template
/* Write a program to swap two numbers using template */
template <class dt> Output:
void swap(dt &x,dt &y) a:10 b:20
{ dt t; m:1.2 n:3.4
t=x; p:a q:b
x=y;
y=t; a:20 b:10
} m:3.4 n:1.2
void main( ) p:b q:a
{ clrscr( );
int a=10,b=20; float m=1.2,n=3.4; char p=’a’,q=’b’;
cout<<”a:”<<a<<”b:”<<b<<endl;
cout<<”m:”<<m<<”n:”<<n<<endl;
cout<<”p:”<<p<<”q:”<<q<<endl;
swap(a,b); swap(m,n); swap(p,q);
cout<<”a:”<<a<<”b:”<<b<<endl;
cout<<”m:”<<m<<”n:”<<n<<endl;
cout<<”p:”<<p<<”q:”<<q<<endl;
gecth( );
}
/* Write a program to print student details using template */
template <class dt1, class dt2>
void function(dt1 x, dt2 y) Output:
{ 1 gopi
cout<<x<<” ”<<y<<endl; gopi 7.5
} 1 70.5
void main( )
{
clrscr( );
function(1,”gopi”);
function(“gopi”,70.5);
function(1,70.5);
getch( );
}
FILES
Def:
Collection of information or records.

/* Write a program to send student details into the file and read student details from the file */
#include<fstream.h>
#include<conio.h>
void main( )
{ clrscr( );
ofstream p1(“student”); // write mode Output:
int rno; char name[20]; float per; enter rno:1
cout<<”enter rno:”; cin>>rno; enter name:gopi
cout<<”enter name :”; cin>>name; enter per: 70.5
cout<<”enter per:”; cin>>per;
p1<<rno<<endl; p1<<name<<endl; p1<<per<<endl; rno: 1
p1.close( ); name: gopi
ifstream p2(“student”); // read mode per: 70.5
p2>>rno; p2>>name; p2>>per;
cout<<”rno :”<<rno<<endl;
cout<<”name :”<<name<<endl;
cout<<”per :”<<per;
p2.close( ); // FILE *p;
getch( ); // p=fopen(“student”,”w”); fclose(p);
}

/* Write a program to send five(5) strings into the file and read five(5) strings from the file */
#include<fstream.h>
#include<conio.h>
void main( )
{ clrscr( ); int i; char name[20]; Output:
ofstream p1; // write mode enter name: Hyd
p1.open(“names”); enter name: Nlg
clrscr( ); enter name: Adb
for(i=1;i<=5;i++) enter name: Wrl
{ cout<<”enter name:”; cin>>name; enter name: Sec
p1<<name<<endl;
} name: Hyd
p1.close( ); name: Nlg
ifstream p2; // read mode name: Adb
p2.open(“names”); name: Wrl
for(i=1;i<=5;i++) name: Sec
{ p2.getline(name,20);
cout<<”name :”<<name<<endl;
}
p2.close( );
getch( );
}

You might also like