Image

C++ - Inheritance - 4. Multiple Inheritance

4. Multiple Inheritance

More than one class inherits its property into single derived class, then such type of inheritance known as multiple inheritance.


e.g: WAP to calculate area and perimeter
#include<conio.h>
#include<iostream.h>
class Ractangle{
     protected:
	float length, breadth;
     public:
	    void get(){
	     cout<<"\nEnter Length and Breadth  ";
	     cin>>length>>breadth;
	    }

};
class Area:public Ractangle{
 public:
	float calc()
	{
	 return length*breadth;
	}

};
class Perimeter:public Ractangle{
 public:
	float calc()
	{
	 return 2*(length*breadth);
	}

};

void main()
{
  clrscr();
 cout<<"\n Enter data for first ractanlge to calculate area ";
 Area a;
 a.get();
 cout<<"\n Area : "<<a.calc()<<" square meter";
 Perimeter p;
 cout<<"\n Perimeter : "<<p.calc()<<" meter";
 getch();
}
O/P
Enter data  for first  rectangle to calculate area
Enter length and breadth
5
6
Area =30 square meter
Perimeter 5.084232e-39meter

In the above diagrams class C is derived class of base class A & B. Therefore object of class C can access the public property of base class A & B as well as its so on.