Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

//21.

Program to show the use of inline function


#include<iostream.h>
#include<conio.h>
inline int max(int x,int y)
{
return(x>y?x:y);
}
int main()
{
clrscr();
int m=10,n=25;
int a,b;
a=max(6,8);
cout<<"\n Greatest of 6 and 8="<<a;
b=max(m,n);
cout<<"\n Greatest of m="<<m<<" and n "<<n<<" is "<<b;
getch();
return 0;
}
/* OUTPUT
Greatest of 6 and 8=8
Greatest of m=10 and n 25 is 25
*/
//22.Program to calculate factorial of number using recursion
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int factorial(int);
int x,fact;
cout<<"\n Enter a number =";
cin>>x;
fact=factorial(x);
cout<<"\n Factorial of "<<x<<"="<<fact;
getch();
return 0;
}
int factorial(int a)
{
int b;
if(a==1)
return (1);
else
b=a*factorial(a-1);
return (b);
}
/* OUTPUT
Enter a number =3

Factorial of 3=6
*/
//23.Program to demonstrate how a friend function can access private data member of a
class
#include<iostream.h>
#include<conio.h>
class demo
{
int x;
public:
demo(int xx)
{ x=xx; }
friend void display(demo); //friend function declaration
};
void display(demo dd1)
{ cout<<"private data of demo class="<<dd1.x; }
void main()
{
clrscr();
demo d1(5);
display(d1); //friend function call
getch();
}
/* OUTPUT
private data of demo class=5
*/
//24.Program to demonstrate how a friend function act as a bridge between two class
#include<iostream.h>
#include<conio.h>
class yyy;// forward declaration of class
class xxx
{
private:
int x;
public:
xxx(int xx)
{ x=xx; }
friend void f1(xxx,yyy);//friend function declaration
};
class yyy
{
private:
int y;
public :
yyy(int yy)
{ y=yy; }
friend void f1(xxx,yyy); //friend function declaration
};
void f1(xxx objx,yyy objy)
{
cout<<"Difference = "<<objx.x-objy.y;
}
void main()
{
clrscr();
xxx ob1(10);
yyy ob2(5);
f1(ob1,ob2);//friend function call
getch();
}
/* OUTPUT
Difference = 5
*/
//25.Program to illustrate how to declare the entire class as a friend of another class
#include<iostream.h>
#include<conio.h>
class xxx
{
private:
int x,y;
public:
xxx(int xx,int yy)
{ x=xx; y=yy; }
friend class yyy; //friend class
};
class yyy
{
public:
void f1(xxx objx)
{ cout<<"\n x="<<objx.x;}
void f2(xxx objy)
{ cout<<"\n y="<<objy.y;}
};
void main()
{
clrscr();
xxx ob1(10,15);
yyy ob2;
ob2.f1(ob1);
ob2.f2(ob1);
getch();
}
/* OUTPUT
x=10
y=15
*/
//26.Program of default constructor
#include<iostream.h>
#include<conio.h>
class ABC
{
int a;
public:
void get()
{
a=10;
}
void show()
{
cout<<"\n A= "<<a;
}
};
void main()
{
clrscr();
ABC obj;
obj.get();
obj.show();
getch();
}
/* OUTPUT
A= 10
*/
//27.program of parameterized constructor
#include<iostream.h>
#include<conio.h>
class info
{
int x,y;
public:
info(int a)//single parameter constructor
{
x=a;
y=10;
}
info(int a,int b)//two parameters constructor
{
x=a;
y=b;
}
void show()
{
cout<<"\n x="<<x;
cout<<"\n y="<<y;
}
};
void main()
{
info a2(5);
info a3(15,20);
clrscr();
a2.show();
a3.show();
getch();
}
/* OUTPUT
x=5
y=10
x=15
y=20 */
//28.Program of copy constructor
#include<iostream.h>
#include<conio.h>
class counter
{
int count;
public:
counter(int c)//single parameter constructor
{ count=c; }
counter(counter &ob)//copy constructor
{
cout<<"\n copy constructor invoked";
count=ob.count;
}
void show()
{ cout<<"\n cout="<<count; }
};
void main()
{
clrscr();
counter c1(10);
counter c2(c1);//call copy constructor
c1.show();
c2.show();
getch();
}
/* OUTPUT
copy constructor invoked
cout=10
cout=10
*/
//29.program of constructor overloading
#include<iostream.h>
#include<conio.h>
class rectangle
{
private:
int length,breadth;
public:
rectangle()
{
length=breadth=0;
cout<<"constructor with zero parameter called\n";
}
rectangle(int a)
{
length=breadth=a;
cout<<"constructor with one parameter called\n";
}
rectangle(int a,int b)
{
length=a; breadth=b;
cout<<"constructor with two parameter called\n";
}
int area()
{
return(length*breadth);
}
};
int main()
{
clrscr();
rectangle r1;
rectangle r2(5);
rectangle r3(7,8);
cout<<"\n Area of first rectangle="<<r1.area();
cout<<"\n Area of square="<<r2.area();
cout<<"\n Area of second rectangle="<<r3.area();
getch();
return 0;
}
/*OUTPUT
constructor with zero parameter called
constructor with one parameter called
constructor with two parameter called
Area of first rectangle=0
Area of square=25
Area of second rectangle=56
*/
//30.Program of dynamic constructor
#include<iostream.h>
#include<conio.h>
class dynamic_con
{
int *ptr;
public:
dynamic_con()
{
ptr=new int;
*ptr=100;
}
dynamic_con(int v)
{
ptr=new int;// Dynamic memory allocation
*ptr=v;
}
int display()
{
return(*ptr);
}
};
void main()
{
clrscr();
dynamic_con obj1,obj2(90);
cout<<"\n The value of obj1's ptr is:";
cout<<obj1.display();
cout<<"\n The value of obj2's pte is:";
cout<<obj2.display();
getch();
}
/* OUTPUT
The value of obj1's ptr is:100
The value of obj2's pte is:90
*/
//31.Program of destructor
#include<iostream.h>
#include<conio.h>
class counter
{
int id ;
public:
counter(int i)
{
id=i;
cout<<"\n constructor of object with id "<<id<<" runs";
}
~counter()
{
cout<<"\n object with Id "<<id<<" destroyed";
}
};
int main()
{
clrscr();
counter c1(1);
counter c2(2);
counter c3(3);
cout<<"\n End of main";
getch();
return 0;
}
/* OUTPUT
constructor of object with id 1 runs
constructor of object with id 2 runs
constructor of object with id 3 runs
End of main
object with Id 3 destroyed
object with Id 2 destroyed
object with Id 1 destroyed*/
//32.Program of function overloading
#include<iostream.h>
#include<conio.h>
void sum();
int sum(int,int);
float sum(float,float);
int sum(int,int,int);
void main()
{
int a=10,b=5,c=15;
float x=7.75,y=5.5;
clrscr();
sum();//No value passed
cout<<"\n sum of two integer passed "<<a<<" and "<<b<<" ="<<sum(a,b);
cout<<"\n sum of two floating point passed "<<x<<" and" <<y<<" ="<<sum(x,y);
cout<<"\n sum of three integer passed "<<a<<","<<b<< " and" <<c<<"="<<sum(a,b,c);
getch();
}
//________________________________________________
void sum()
{
int a=5;
float b=7.25;
cout<<"\n sum of mixed value (no parameter passed) "<<a<<" and "<<b<<" ="<<a+b;
}
//___________________________________
int sum(int p,int q)
{
return(p+q);
}
//_________________________________
int sum(int p,int q,int r)
{
return(p+q+r);
}
//___________________________________
float sum(float p, float q)
{
return(p+q);
}

/* OUTPUT
sum of mixed value (no parameter passed) 5 and 7.25 =12.25
sum of two integer passed 10 and 5 =15
sum of two floating point passed 7.75 and 5.5 =13.25
sum of three integer passed 10,5 and 15=30
*/
//33. Program of NESTED CLASSES
#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
char name[20];
public:
class date
{
private:
int dd,mm,yy;
public:
void read(); //declaration
void show();
}dob; //object of nested class
void read();
void show();
};
void student::date::read()
{
cin>>dd>>mm>>yy; //defining read() of nested class date
}
void student::date::show()
{
cout<<"\n Date of birth ="<<dd<<"/"<<mm<<"/"<<yy;
}
void student::read()
{
cout<<"\n Enter roll no and name =";
cin>>rollno>>name;
cout<<"\n Enter date of birth(dd mm yy) =";
dob.read();
}
void student::show()
{
cout<<" Rollno ="<<rollno<<"\t name = "<<name;
dob.show();
}
int main()
{
clrscr();
student s1;
s1.read();
s1.show();
getch();
return 0;
}
/* OUTPUT

Enter roll no and name =7201 Vikas


Enter date of birth(dd mm yy) =31 07 1998
Rollno =7202 Name =Vikas
Date of birth =31/7/1998
*/
//34. program of static data member
#include<iostream.h>
#include<conio.h>
class account
{
private:
int acc_no; double balance;
static double rate;//static data member declaration
public:
void read()
{
cout<<"\n Enter account no. and balance=";
cin>>acc_no>>balance;
}
void show()
{
cout<<"\n Account no="<<acc_no<<'\t';
cout<<"Interest="<<rate<<'\t';
cout<<"Balance="<<balance;
}
void qtr_rate_cal()
{
double interest=(balance*rate*0.25);
balance=balance+interest;
}
};
double account::rate=0.05; //static data member definition
int main()
{
clrscr();
account a1,a2;
cout<<"Enter customer 1 information......\n";
a1.read();
cout<<"Enter customer 2 information......\n";
a2.read();
a1.qtr_rate_cal();
a2.qtr_rate_cal();
a1.show();
a2.show();
getch();
return 0;
}
/* OUTPUT
Enter customer 1 information...

Enter account no. and balance=201 1000


Enter customer 2 information....

Enter account no. and balance=202 2000

Account no=201 Interest=0.05 Balance=1012.5


Account no=202 Interest=0.05 Balance=2025
*/
//35.program of static member function
#include<iostream.h>
#include<conio.h>
class account
{
private:
int acc_no; double balance;
static double rate;
public:
void read()
{
cout<<"Enter account number and balance->";
cin>>acc_no>>balance;
}
void show()
{
cout<<"\n Account no = "<<acc_no;
cout<<" Interest = "<<rate;
cout<<" Balance = "<<balance;
}
void qtr_rate_cal()
{
double interest=(balance*rate*0.25)/100;
balance=balance+interest;
}
static void modify_rate(double incr)
{
rate=rate+incr;//modifying rate
cout<<"\n Modified Rate of Interest ="<<rate;
}
};//end of class specification
double account::rate=0.05;
void main()
{
clrscr();
account a1,a2;
a1.read();
a2.read();
account::modify_rate(0.01); // calling static mem.fun.
a1.qtr_rate_cal();
a2.qtr_rate_cal();
a1.show();
a2.show();
getch();
}

/* OUTPUT
Enter account number and balance->43557 4367
Enter account number and balance->676288 5267

Modified Rate of Interest =0.06


Account no = -21979 Interest = 0.06 Balance = 4367.65505
Account no = 20928 Interest = 0.06 Balance = 5267.7900
*/
//36.Program to demonstrate object to pointer
#include<iostream.h>
#include<conio.h>
class data
{
int val;
public:
void get()
{
cout<<"\n Enter any integer value= ";
cin>>val;
}
void show()
{
cout<<"\n \t Integer value= "<<val;
}
};
void main()
{
data d1; //object declaration
data *ptr;//pointer object declaration
clrscr();
ptr=&d1; //object to pointer
ptr->get();
(*ptr).show();
getch();
}

/* OUTPUT
Enter any integer value= 20

Integer value= 20
*/
//37.program to demonstrate the concept of nesting of member function
#include<iostream.h>
#include<conio.h>
#define MAX_SIZE 10
class Data
{
int num[MAX_SIZE];
int n;
public:
void get_data();
int largest();
void display();
};
void Data::get_data()
{
cout<<" Enter the total number(n):"<<endl;
cin>>n;
cout<<"Enter the Number:"<<endl;
for(int i=0; i<n;i++)
{
cout<<"Enter the Number "<<i+1<<"=";
cin>>num[i];
}
}
int Data::largest()
{
int max;
max=num[0];

for(int i=1;i<n;i++)
{
if(max<num[i])
max=num[i];
}
return max;
}
void Data::display()
{
cout<<"The largest number:"<<largest()<<endl;
}
void main()
{
Data n1;
clrscr();
n1.get_data();
n1.display();
getch();
}

/* OUTPUT
Enter the total number(n):4
Enter the Number:
Enter the Number 1=7
Enter the Number 2=14
Enter the Number 3=23
Enter the Number 4=11
The largest number:23
*/
//38.Program of array of object
#include<iostream.h>
#include<conio.h>
class student
{
char name[20];
int roll;
float per;
public:
void get();
void put();
};
void student::get()
{
cin>>name>>roll>>per;
}
void student::put()
{
cout<<"\n\t Name="<<name;
cout<<"\n\t Roll no.="<<roll;
cout<<"\n\t Percentage="<<per;
}
void main()
{
student s[3];//Array of objects
clrscr();
cout<<"\n Enter student details=";
for(int i=0; i<3; i++)
{
cout<<"\n Enter "<<i+1<<"Student name,roll no and percentage=";
s[i].get();
}
cout<<"\n Student Information(Report)";
for(i=0; i<3; i++)
{
cout<<"\n\t"<<i+1<<"Student";
s[i].put();
}
getch();
}

/* OUTPUT
Enter student details=
Enter 1 Student name,roll no and percentage=Anshu 7246 80

Enter 2 Student name,roll no and percentage=Nitish 7243 75

Enter 3 Student name,roll no and percentage=Aman 7268 66

Student Information(Report)
1 Student
Name=Anshu
Roll no.=7246
Percentage=80
2 Student
Name=Nitish
Roll no.=7243
Percentage=75
3 Student
Name=Aman
Roll no.=7268
Percentage=66
*/
//39.Program to demonstrate passing an object by argument
#include<iostream.h>
#include<conio.h>
class time
{
private:
int hours,minutes;
public:
void read_time();
void add_time(time,time);
void show_time();
};
void time::read_time()
{
cout<<"\n Enter time in hours and minutes :";
cin>>hours>>minutes;
}
void time::add_time(time tt1,time tt2)
{
int min=tt1.minutes +tt2.minutes;
int hrs=min/60;
minutes=min%60;
hours=hrs+tt2.hours;
}
void time::show_time()
{
cout<<"\n Time obtained on adding= ";
cout<<hours<<":"<<minutes;
}
int main()
{
clrscr();
time t1,t2,t3;
t1.read_time();
t2.read_time();
t3.add_time(t1,t2); //t3=t1+t2
t3.show_time();
getch();
return 0;
}

/* Output
Enter time in hours and minutes : 5 30
Enter time in hours and minutes : 5 30

Time obtained on adding= 6:0 */


//40.Program of Private Inheritance
#include<iostream.h>
#include<conio.h>
class A //BASE CLASS
{
private:
int x;
public:
void input_x()
{
cout<<"\n Enter value of x:";
cin>>x;
}
int get_x()
{
return x;
}
};
class B: private A //DERIVED CLASS
{
int y,diff;
public:
void input_y()
{
input_x();
cout<<"\n Enter value of y:";
cin>>y;
}
void subtract()
{
diff=get_x()-y;
cout<<"\n Base class data:"<<get_x();
cout<<"\n Derived class data:"<<y;
cout<<"\n Difference="<<diff;
}
};
void main()
{
B b1;
clrscr();
b1.input_y();
b1.subtract();
getch();
}

/* OUTPUT
Enter value of x:15
Enter value of y:5

Base class data:15


Derived class data:5
Difference=10 */
//41.program of public Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
private:
int x;
public:
void input_x()
{
cout<<"\n Enter value of x:";
cin>>x;
}
int get_x()
{
return x;
}
};
class B: public A
{
int y, diff;
public:
void input_y()
{
cout<<"\n Enter value of y:";
cin>>y;
}
void subtract()
{
diff=get_x()-y;
cout<<"\n Base class data:"<<get_x();
cout<<"\n Derived class data:"<<y;
cout<<"\n Difference="<<diff;
}
};
void main()
{
B b1;
clrscr();
b1.input_x();
b1.input_y();
b1.subtract();
getch();
}

/*OUTPUT
Enter value of x:15
Enter value of y:5
Base class data:15
Derived class data:5
Difference=10
*/
//42.program of protected Inheritance or protected data member
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int x; //protected data member
public:
void input_x()
{
x=10;
}
};
class B: protected A //protected inheritance
{
int y,sum;
public:
void input_y()
{
input_x();
y=20;
}
void add()
{
sum=x+y;
}
void display()
{
cout<<"\n Base class data="<<x;
cout<<"\n Derived class data="<<y;
cout<<"\n Sum="<<sum;
}
};
void main()
{
B b1;
clrscr();
b1.input_y();
b1.add();
b1.display();
getch();
}
/* OUTPUT
Base class data=10
Derived class data=20
Sum=30
*/
//43.program of single inheritance
#include<iostream.h>
#include<conio.h>
class A
{
private:
int x;
public:
int get_x()
{
x=10;
return x;
}
};
class B:
private A
{
int y,sum;
public:
void get_y()
{
y=20;
}
void add()
{
sum=get_x()+y;
cout<<"\n Base class data:"<<get_x();
cout<<"\n Derived class data:"<<y;
cout<<"\n sum="<<sum;
}
};

void main()
{
B b1;
clrscr();
b1.get_y();
b1.add();
getch();
}

/* OUTPUT
Base class data:10
Derived class data:20
sum=30
*/
//44.Program of multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class A //---Super Base Class---
{
protected:
int x;
public:
void input_x()
{
cout<<"\n Enter value of x:";
cin>>x;
}
};
class B: public A //---INTERMEDIATE BASE CLASS---
{
protected:
int y;
public:
void input_y()
{
cout<<"\n Enter value of y:";
cin>>y;
}
};
class c: public B//--DERIVED CLASS--
{
int z;
public:
void add()
{
z=x+y;
}
void display()
{
cout<<"\n Super Base class data:"<<x;
cout<<"\n Intermediate Base class data:"<<y;
cout<<"\n sum (in derived class):"<<z;
}
};
void main()
{
c c1;
clrscr();
c1.input_x();
c1.input_y();
c1.add();
c1.display();
getch();
}

/* OUTPUT
Enter value of x:10

Enter value of y:20

Super Base class data:10


Intermediate Base class data:20
sum (in derived class):30
*/
//45.program of Hierarchical inheritance
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int m;
public:
void input_m()
{
m=2;
}
};
class B: public A
{
int sq;
public:
void square()
{
sq=m*m;
cout<<"\n Base data="<<m;
cout<<"\n square of no="<<sq;
}
};
class C: public A
{
int cb;
public:
void cube()
{
cb=m*m*m;
cout<<"\n Base data="<<m;
cout<<"\n cube of no="<<cb;
}
};
void main()
{
B b1;
C c1;
clrscr();
b1.input_m();
b1.square();
c1.input_m();
c1.cube();
getch();
}

/* OUTPUT
Base data=2
square of no=4
Base data=2
cube of no=8 */
//46.Program of multiple inheritance
#include<iostream.h>
#include<conio.h>
class A //1ST BASE CLASS
{
protected:
int x;
public:
void input_x()
{
cout<<"\n Enter the value of x:";
cin>>x;
}
};
class B //2ND BASE CLASS
{
protected:
int y;
public:
void input_y()
{
cout<<"\n Enter the value of y:";
cin>>y;
}
};
class C:public A,public B //DERIVED CLASS
{
int z;
public:
void add()
{
z=x+y;
}
void display()
{
cout<<"\n 1st Base class data:"<<x;
cout<<"\n 2nd Base class data:"<<y;
cout<<"\n Sum(in derived class):"<<z;
}
};
void main()
{
C c1;
clrscr();
c1.input_x();
c1.input_y();
c1.add();
c1.display();
getch();
}

/* OUTPUT
Enter the value of x:10

Enter the value of y:20

1st Base class data:10


2nd Base class data:20
Sum(in derived class):30
*/
//47.Program to resolve ambiguity in multiple inheritance
#include<iostream.h>
#include<conio.h>
class A //1st base class
{
protected:
int x;
public:
void input_x()
{
cout<<"\n Enter the value of x:";
cin>>x;
}
};
class B //2nd Base class
{
protected:
int x;
public:
void input_x()
{
cout<<"\n Enter the value of x:";
cin>>x;
}
};
class C:public A,public B //Derived class
{
int z;
public:
void add()
{
z=A::x+B::x;
}
void display()
{
cout<<"\n 1st Base class data:"<<A::x;
cout<<"\n 2nd Base class data:"<<B::x;
cout<<"\n sum (in derived class):"<<z;
}
};
void main()
{
C c1;
clrscr();
c1.A::input_x();
c1.B::input_x();
c1.add();
c1.display();
getch();
}

/* OUTPUT
Enter the value of x:10
Enter the value of x:15
1st Base class data:10
2nd Base class data:15
sum (in derived class):25
*/
//48.Program of hybrid inheritance or VIRTUAL BASE class
// A
// | |
// B C
// | |
// D
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int x;
public:
void input_x()
{
cout<<"\n Enter value of x:";
cin>>x;
}
};
class B:virtual public A // 1st virtual base class
{
protected:
int y;
public:
void input_y()
{
cout<<"\n Enter value of y:";
cin>>y;
}
};
class C:virtual public A //2nd virtual base class
{
protected:
int z;
public:
void input_z()
{
cout<<"\n Enter value of z:";
cin>>z;
}
};
class D:public B,public C //Derived class
{
int sum;
public:
void add()
{
sum=x+y+z;
}
void display()
{
cout<<"\n Super base class data:"<<x;
cout<<"\n 1st intermediate base class data:"<<y;
cout<<"\n 2nd Intermediate base class data:"<<z;
cout<<"\n Sum(In derived class):"<<sum;
}
};

void main()
{
D d1;
clrscr();
d1.input_x();
d1.input_y();
d1.input_z();
d1.add();
d1.display();
getch();
}

/* Output
Enter value of x:30

Enter value of y:40

Enter value of z:60

Super base class data:30


1st intermediate base class data:40
2nd Intermediate base class data:60
Sum(In derived class):130
*/
//49.program of composition or containment
#include<iostream.h>
#include<conio.h>
class date
{
private:
int dd,mm,yy;
public:
void read()
{
cout<<"Enter date,month and year=";
cin>>dd>>mm>>yy;
}
void show()
{
cout<<"Date="<<dd<<"/"<<mm<<"/"<<yy;
}
};
class student
{
private:
int rollno;
char name[20];
date dob;//object of class date as a data member
public:
void read()
{
cout<<"Enter roll number and name=";
cin>>rollno>>name;
dob.read();
}
void show()
{
cout<<"\n Roll no= "<<rollno<<" name= "<<name<<" ";
dob.show();
}
};
void main()
{
clrscr();
student s1;
s1.read();
s1.show();
getch();
}
/* OUTPUT
Enter roll number and name=7246 Anshu
Enter date,month and year=31 07 1998

Roll no= 7246 name= Anshu Date=31/7/1998


*/
//50.program of unary operator overloading using member function
#include<iostream.h>
#include<conio.h>
class obj_incr
{
int a;
public:
obj_incr()
{
a=0;
}
void operator++()
{
a=a+1;
}
void show()
{
cout<<"\n value:"<<a;
}
};
void main()
{
obj_incr t1,t2;
clrscr();
cout<<"\n Before increment";
cout<<"\n Ist object value";
t2.show();
++t1; //compiler convert the statement into t1.operator++()
t1++;
++t2;
cout<<"\nAfter increment";
cout<<"\n Ist object value";
t1.show();
cout<<"\n 2nd object value";
t2.show();
getch();
}
/*Output
Before increment
Ist object value
value:0
After increment
Ist object value
value:2
2nd object value
value:1
*/
//51.Program of unary operator overloading using friend function
#include<iostream.h>
#include<conio.h>
class obj_incr
{
int a;
public:
obj_incr()
{
a=0;
}
friend void operator++(obj_incr &temp)
{
temp.a=temp.a+1;
}
void show()
{
cout<<"\n value:"<<a;
}
};
void main()
{
obj_incr t1,t2;
clrscr();
cout<<"\n Before increment";
cout<<"\n Ist object value";
t1.show();
cout<<"\n 2nd object value";
t2.show();
++t1; // compiler convert the statement into operator++(t1)
t1++;
++t2;
cout<<"\nAfter increment";
cout<<"\n Ist object value";
t1.show();
cout<<"\n 2nd object value";
t2.show();
getch();
} /*Output
Before increment
Ist object value
value:0
After increment
Ist object value
value:2
2nd object value
value:1
*/
//52.program of binary operator overloading using member function
#include<iostream.h>
#include<conio.h>
class data
{
int a;
public:
data()
{
a=0;
}
data(int x)
{
a=x;
}
data operator+(data obj)
{
cout<<"\n Binary operator overloading using member function";
data temp;
temp.a=a+obj.a;
return temp;
}
void show()
{
cout<<a;
}
};
void main()
{
data t1(10),t2(20),t3;
clrscr();
cout<<"\n Ist object value:";
t1.show();
cout<<"\n 2nd object value:";
t2.show();
t3=t1+t2; // compiler convert statement into t1.operator+(t2)
cout<<"\n After addition";
cout<<"\n\t Sum of Ist object and 2nd object:";
t3.show();
getch();
}
/*Output
Before increment
Ist object value
value:0
After increment
Ist object value
value:2
2nd object value
value:1
*/
//53.Program of binary operator overloading using friend function
#include<iostream.h>
#include<conio.h>
class data
{
int a;
public:
data()
{
a=0;
}
data(int x)
{
a=x;
}
friend data operator+(data obj1,data obj2)
{
cout<<"\n Binary operator overloading using friend function";
data temp;
temp.a=obj1.a+obj2.a ;
return temp;
}
void show()
{
cout<<a;
}
};
void main()
{
data t1(10),t2(20),t3;
clrscr();
cout<<"\n 2nd object value:";
t1.show();
t3=t1+t2; // compiler convert statement into operator+(t1,t2)
cout<<"\After Addition:";
cout<<"\n\tSum of Ist and 2nd object:";
t3.show();
getch();
}
/*Output
2nd object value:10
Binary operator overloading using friend function After Addition:
Sum of Ist and 2nd object:30
*/
//54.Program of add or concatenate two strings using operator overloading
#include<iostream.h>
#include<conio.h>
#include<string.h>
class data
{
char *st;
public:
data()
{
st=NULL;
}
data(char *p)
{
st=p;
}
data operator+(data obj)
{
data temp;
temp.st=strcat(st,obj.st);
return temp;
}
void show()
{
cout<<st;
}
};
void main()
{
data t1("aman"),t2("deep"),t3;
clrscr();
cout<<"\nIst object string:" ;
t1.show();
cout<<"\n2nd object string:";
t2.show();
t3=t1+t2; //compiler statement into t1.operator(t2);
cout<<"\n After concatenated of two string object";
cout<<"\nResultant string:";
t3.show();
getch();
}
/*Output
Ist object string:aman
2nd object string:deep
After concatenated of two string object
Resultant string:amandeep
*/
//55.Program to demonstrate compulsion of friend function in binary operator
overloading
#include<iostream.h>
#include<conio.h>
class data
{
int num;
public:
data(int x)
{
num=x;
}
data()
{
num=0;
}
friend data operator+(int a,data obj)
{
data temp;
temp.num=a+obj.num;
return temp;
}
void show()
{
cout<<num;
}
};
void main()
{
data d1(10),d2;
clrscr();
d2=5+d1;
cout<<"\n Ist object value:";
d1.show();
cout<<"\n 2nd object value:";
d2.show();
getch();
}
/*Output
Ist object value:10
2nd object value:15
*/
//56.Program to demonstrate pointer to derived class object
#include<iostream.h>
#include<conio.h>
class A
{
int x;
public:
virtual void get(int p)
{
x=p;
}
virtual void show()
{
cout<<"\n Data member of A class:"<<x;
}
};
class B:public A
{
int y;
public:
void get(int q)
{
y=q;
}
void show()
{
cout<<"\n Data member of B class:"<<y;
}
};
void main()
{
A a1;
B b1;
A *pt;
clrscr();
pt=&a1;
pt->get(10);
pt->show();
pt=&b1;
pt->get(20);
pt->show();
getch();
}
/*Output
Data member of A class:10
Data member of B class:20
*/
//57.Program of virtual function or runtime polymorphism
#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void show()
{
cout<<"\n Base class show invoked";
}
};
class B:public A
{
public:
void show()
{
cout<<"\n Derived class show invoked!";
}
};
void main()
{
A a1;
B b1;
A *pt;
clrscr();
pt=&a1;
pt->show();
pt=&b1;
pt->show();
getch();
}
/*Output
Base class show invoked
Derived class show invoked!
*/
//58.Program of pure virtual function
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a,b;
public:
virtual void calculate()=0; //pure virtual function
void get()
{
a=10;
b=5;
}
};
class B:public A
{
int sum;
public:
void calculate()
{
sum=a+b;
cout<<"\n sum of "<<a<<" and "<<b<<"="<<sum;
}
};
class C:public A
{
int pro;
public:
void calculate()
{
pro=a*b;
cout<<"\n product of "<<a<<" and "<<b<<"="<<pro;
}
};
void main()
{
B b1; //derived class object
C c1; //2nd derived class object
A *pt; //base class pointer
clrscr();
pt=&b1;
pt->get();
pt->calculate();
pt=&c1;
pt->get();
pt->calculate();
getch();
}
/* Output
sum of 10 and 5=15
product of 10 and 5=50
*/
//59.Program of this pointer
#include<iostream.h>
#include<conio.h>
class data
{
int a,b;
public:
void get()
{
this->a=10;
this->b=20;
}
void show()
{
cout<<"\n A="<<this->a<<"\t B ="<<this->b;
}
};
void main()
{
data d1,d2;
clrscr();
d1.get();
d2.get();
d1.show();
d1.show();
getch();
}
/* Output
A=10 B =20
A=10 B =20
*/
//60.Program to create a file 'stud.txt' and store student information into it using
<<operator
#include<process.h>
#include<fstream.h>
#include<conio.h>
int main()
{
clrscr();
int rollno;
char name[20];
int marks;
ofstream out_file("stud.txt");
if(!out_file)
{
cerr<<"file cannot open correctly";
exit(-1);
}
cout<<"Enter student detail \n";
cout<<"\n Enter roll no :";
cout<<roll no;
cout<<"\n Enter name :";
cin>>name;
cout<<"\n Enter marks :";
cin>>marks;
cout<<"Writing student details into file...";
out_file<<rollno<<endl;
out_file<<name<<endl;
out_file<<marks<<endl;
getch();
return 0;
}
/* Output
Enter student detail
Enter roll no :8467
Enter name :Rohit
Enter marks :456
Writing student details into file...
*/
//61.Program to read the content of a file 'stud.txt’ using >>operator
#include<fstream.h>
#include<conio.h>
#include<process.h> //for exit
int main()
{
clrscr();
int rollno;
char name[20];
int marks;
ifstream inp_file("stud.txt");
if(!inp_file)
{
cerr<<"file cannot open correctly";
exit(-1);
}
cout<<"Reading student detail from a file"<<endl;
inp_file>>roll no;
inp_file>>name;
inp_file>>marks;
cout<<"Content of file are : "<<endl;
cout<<"Rollno = "<<rollno<<endl;
cout<<"Name = "<<name<<endl;
cout<<"Marks = "<<marks<<endl;
getch();
return 0;
}
/* Output
Reading student detail from a file
Content of file are :
Rollno = 8467
Name = Rohit
Marks = 456
*/

You might also like