Professional Documents
Culture Documents
21.program To Show The Use of Inline Function
21.program To Show The Use of Inline Function
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
/* OUTPUT
Enter account number and balance->43557 4367
Enter account number and balance->676288 5267
/* 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
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
/* OUTPUT
Enter value of x:15
Enter value of y:5
/*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
/* 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
/* 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