1. Program to Single Inheritance
#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int val;
public:
void setData(int v)
{
val=v;
}
};
class Derived: public Base
{
public:
void cube()
{
cout<<"Cube of "<<val<<" is "<<val*val*val;
}
};
int main ()
{
Derived d;
clrscr();
d.setData(10);
d.cube();
getch();
return 0;
}
Output:
Cube of 10 is 1000
2. Program to explain Single Inheritance
#include <iostream.h>
#include <conio.h>
class A
{
int x;
public:
void getX()
{
cout << "Enter x : ";
cin >> x;
}
void showX()
{
cout << "\n x : " << x;
}
};
class B : public A
{
int y;
public:
void getY()
{
cout << "Enter y : ";
cin >> y;
}
void showY()
{
cout << "\n y : " << y;
}
};
void main()
{
clrscr();
B b1;
b1.getX();
b1.getY();
b1.showX();
b1.showY();
getch();
}
Output:
Enter x : 10
Enter y : 20
x : 10
y : 20
3. Program for Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class Area
{
public:
float area_calc(float l,float b)
{
return l*b;
}
};
class Perimeter
{
public:
float peri_calc(float l,float b)
{
return 2*(l+b);
}
};
class Rectangle : private Area, private Perimeter
{
private:
float length, breadth;
public:
Rectangle() : length(0.0), breadth(0.0) { }
void get_data( )
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}
float area_calc()
{
return Area::area_calc(length,breadth);
}
float peri_calc()
{
return Perimeter::peri_calc(length,breadth);
}
};
int main()
{
clrscr();
Rectangle r;
r.get_data();
cout<<"Area = "<<r.area_calc();
cout<<"\nPerimeter = "<<r.peri_calc();
getch();
return 0;
}
Output:
Enter length: 4
Enter breadth: 5
Area = 20
Perimeter = 18
4. Program to explain Multiple Inheritance
#include <iostream.h>
#include <conio.h>
class A
{
int x;
public:
void getX()
{
cout << "Enter x : ";
cin >> x;
}
void showX()
{
cout << "\n x : " << x;
}
};
class B
{
int y;
public:
void getY()
{
cout << "Enter y : ";
cin >> y;
}
void showY()
{
cout << "\n y : " << y;
}
};
class C : public A, public B
{
int z;
public:
void getZ()
{
cout << "Enter z : ";
cin >> z;
}
void showZ()
{
cout << "\n z : " << z;
}
};
void main()
{
clrscr();
C c1;
c1.getX();
c1.getY();
c1.getZ();
c1.showX();
c1.showY();
c1.showZ();
getch();
}
Output:
Enter x : 10
Enter y : 20
Enter z : 30
x : 10
y : 20
z : 30
5. Program for Hierarchical Inheritance
#include<iostream.h>
#include<conio.h>
class A //Base Class
{
public:
int a,b;
void getnumber()
{
cout<<"\n\nEnter Number :";
cin>>a;
}
};
class B : public A //Derived Class 1
{
public:
void square()
{
getnumber();
cout<<"\nSquare of the number :"<<(a*a);
cout<<"\n-------------------------------";
}
};
class C : public A //Derived Class 2
{
public:
void cube()
{
getnumber();
cout<<"\nCube of the number :"<<(a*a*a);
cout<<"\n-------------------------------";
}
};
int main()
{
clrscr();
B b1;
b1.square();
C c1;
c1.cube();
getch();
}
Output:
Enter Number :10
Square of the number :100
-------------------------------
Enter Number :::23
Cube of the number :12167
-------------------------------
6. Program to explain Hierarchical Inheritance
#include <iostream.h>
#include <conio.h>
class A
{
int x;
public:
void getX()
{
cout << "Enter x : ";
cin >> x;
}
void showX()
{
cout << "\n x : " << x;
}
};
class B : public A
{
int y;
public:
void getY()
{
cout << "Enter y : ";
cin >> y;
}
void showY()
{
cout << "\n y : " << y;
}
};
class C : public A
{
int z;
public:
void getZ()
{
cout << "Enter z : ";
cin >> z;
}
void showZ()
{
cout << "\n z : " << z;
}
};
void main()
{
clrscr();
B b1;
C c1;
b1.getX();
b1.getY();
c1.getX();
c1.getZ();
b1.showX();
b1.showY();
c1.showX();
c1.showZ();
getch();
}
Output:
Enter x : 10
Enter y : 20
Enter x : 30
Enter z : 40
x : 10
y : 20
x : 30
z : 40
7. Program for MultiLevel Inheritance
#include<iostream.h>
#include<conio.h>
class top
{
public:
int a;
void getdata()
{
cout<<"\n\nEnter first Number :\t";
cin>>a;
}
void putdata()
{
cout<<"\nFirst Number Is :\t"<<a;
}
};
//First level inheritance
class middle : public top
{
public:
int b;
void square()
{
getdata();
b=a*a;
cout<<"\n\nSquare Is :"<<b;
}
};
//Second level inheritance
class bottom :public middle // class bottom is derived_2
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<<"\n\nCube : "<<c;
}
};
int main()
{
clrscr();
bottom b1;
b1.cube();
getch();
}
Output:
Enter first Number : 23
Square Is :529
Cube : 12167
8. Program to explain Multi-level Inheritance
#include <iostream.h>
#include <conio.h>
class A
{
int x;
public:
void getX()
{
cout << "Enter x : ";
cin >> x;
}
void showX()
{
cout << "\n x : " << x;
}
};
class B : public A
{
int y;
public:
void getY()
{
cout << "Enter y : ";
cin >> y;
}
void showY()
{
cout << "\n y : " << y;
}
};
class C : public B
{
int z;
public:
void getZ()
{
cout << "Enter z : ";
cin >> z;
}
void showZ()
{
cout << "\n z : " << z;
}
};
void main()
{
clrscr();
C c1;
c1.getX();
c1.getY();
c1.getZ();
c1.showX();
c1.showY();
c1.showZ();
getch();
}
Output:
Enter x : 10
Enter y : 20
Enter z : 30
x : 10
y : 20
z : 30
9. Program for Hybrid Inheritance
#include<iostream.h>
#include<conio.h>
int a,b,c,d,e;
class A
{
public:
void getab()
{
cout<<"Enter A:";
cin>>a;
cout<<"Enter B:";
cin>>b;
}
};
class B:public A
{
public:
void getc()
{
cout<<"Enter C:";
cin>>c;
}
};
class C
{
public:
void getd()
{
cout<<"Enter D:";
cin>>d;
}
};
class D:public B,public C
{
public:
void result()
{
getab();
getc();
getd();
e=a+b+c+d;
cout<<"\n Addition is :"<<e;
}
};
void main()
{
clrscr();
D d1;
d1.result();
getch();
}
Output:
Enter A:10
Enter B:20
Enter C:30
Enter D:40
Addition is :100
10. Program to explain Hybrid Inheritance
#include <iostream.h>
#include <conio.h>
class A
{
int x;
public:
void getX()
{
cout << "Enter x : ";
cin >> x;
}
void showX()
{
cout << "\n x : " << x;
}
};
class B : public A
{
int y;
public:
void getY()
{
cout << "Enter y : ";
cin >> y;
}
void showY()
{
cout << "\n y : " << y;
}
};
class C : public A
{
int z;
public:
void getZ()
{
cout << "Enter z : ";
cin >> z;
}
void showZ()
{
cout << "\n z : " << z;
}
};
class D : public B, public C
{
int w;
public:
void getW()
{
cout << "Enter w : ";
cin >> w;
}
void showW()
{
cout << "\n w : " << w;
}
};
void main()
{
clrscr();
D d1;
// d1.getX(); // Error: ambigous
d1.B::getX();
d1.C::getX();
d1.getY();
d1.getZ();
d1.getW();
// d1.showX(); // Error: ambigous
d1.B::showX();
d1.C::showX();
d1.showY();
d1.showZ();
d1.showW();
getch();
}
Output:
Enter x : 10
Enter x : 20
Enter y : 30
Enter z : 40
Enter w : 50
x : 10
x : 20
y : 30
z : 40
w : 50
11. Program to explain public Visibility Mode
#include <iostream.h>
#include <conio.h>
class A
{
int x;
public:
void getX()
{
cout << "Enter x : ";
cin >> x;
}
void showX()
{
cout << "\n x : " << x;
}
};
class B : public A
{
int y;
public:
void getY()
{
cout << "Enter y : ";
cin >> y;
}
void showY()
{
cout << "\n y : " << y;
}
};
void main()
{
clrscr();
B b1;
b1.getX();
b1.getY();
b1.showX();
b1.showY();
getch();
}
Output:
Enter x : 10
Enter y : 20
x : 10
y : 20
12. Program to explain protected Visibility Mode
#include <iostream.h>
#include <conio.h>
class A
{
int x;
public:
void getX()
{
cout << "Enter x : ";
cin >> x;
}
void showX()
{
cout << "\n x : " << x;
}
};
class B : protected A
{
int y;
public:
void getY()
{
getX();
cout << "Enter y : ";
cin >> y;
}
void showY()
{
showX();
cout << "\n y : " << y;
}
};
void main()
{
clrscr();
B b1;
b1.getY();
b1.showY();
getch();
}
Output:
Enter x : 10
Enter y : 20
x : 10
y : 20
13. Program to explain private Visibility Mode
#include <iostream.h>
#include <conio.h>
class A
{
int x;
public:
void getX()
{
cout << "Enter x : ";
cin >> x;
}
void showX()
{
cout << "\n x : " << x;
}
};
class B : private A
{
int y;
public:
void getY()
{
getX();
cout << "Enter y : ";
cin >> y;
}
void showY()
{
showX();
cout << "\n y : " << y;
}
};
void main()
{
clrscr();
B b1;
b1.getY();
b1.showY();
getch();
}
Output:
Enter x : 10
Enter y : 20
x : 10
y : 20
14. Program to Single Inheritance in Private Mode
#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int val;
public:
void setData(int v)
{
val=v;
}
int getData()
{
return val;
}
};
class Derived : private Base
{
public:
void cube()
{
cout<<"Cube of "<<val<<" is "<<getData()*getData()*getData();
}
};
int main ()
{
Derived d;
clrscr();
d.setData(10);
d.cube();
getch();
return 0;
}
Output:
Cube of 10 is 1000
15. Program to explain Inheritance with Counter class example
#include <iostream.h>
#include <conio.h>
class Counter
{
protected:
unsigned int count;
public:
Counter()
{
count = 0;
}
Counter( int cnt )
{
count = cnt;
}
int getCount()
{
return count;
}
Counter operator ++()
{
return Counter(++count);
}
};
class CountDn : public Counter
{
public:
Counter operator --()
{
return Counter(--count);
}
};
void main()
{
clrscr();
CountDn c1;
cout << "\n c1 : " << c1.getCount();
++c1; ++c1; ++c1;
cout << "\n c1 : " << c1.getCount();
--c1; --c1;
cout << "\n c1 : " << c1.getCount();
getch();
}
Output:
c1 : 0
c1 : 3
c1 : 1
16. Program to explain Inheritance with Counter class with Constructors in Base and Derived class
#include <iostream.h>
#include <conio.h>
class Counter
{
protected:
unsigned int count;
public:
Counter()
{
count = 0;
}
Counter( int cnt )
{
count = cnt;
}
int getCount()
{
return count;
}
Counter operator ++()
{
return Counter(++count);
}
};
class CountDn : public Counter
{
public:
CountDn()
{ }
CountDn( int c )
{ }
Counter operator --()
{
return Counter(--count);
}
};
void main()
{
clrscr();
CountDn c1;
cout << "\n c1 : " << c1.getCount();
++c1; ++c1; ++c1;
cout << "\n c1 : " << c1.getCount();
--c1; --c1;
cout << "\n c1 : " << c1.getCount();
getch();
}
Output:
c1 : 0
c1 : 3
c1 : 1
17. Program to explain Inheritance with protected Data Member
#include <iostream.h>
#include <conio.h>
#define PI 3.14
class Circle
{
protected:
float radius;
public:
void readRadius()
{
cout << " Enter Radius ::: ";
cin >> radius;
}
void showRadius()
{
cout << "\n Radius : " << radius;
}
float area()
{
float ar;
ar = PI * radius * radius;
return ar;
}
};
class Cylinder : protected Circle
{
private:
float height;
public:
void readHeight()
{
readRadius();
cout << " Enter Height ::: ";
cin >> height;
}
void showHeight()
{
showRadius();
cout << "\n Height : " << height;
}
float volume()
{
float v;
v = PI * radius * radius * height;
return v;
}
};
void main()
{
clrscr();
Cylinder cl1;
cout << " Enter Details of Cylinder : \n";
cl1.readHeight();
cout << "\n Cylinder : ";
cl1.showHeight();
float v = cl1.volume();
cout << "\n Volume of Cylinder : " << v;
getch();
}
Output:
Enter Details of Cylinder :
Enter Radius ::: 10
Enter Height ::: 10
Cylinder :
Radius : 10
Height : 10
Volume of Cylinder : 3140
0 Comments