Multiple Inheritance

A class can inherit the attributes from two or more classes. This mechanism is known as ‘MULTIPLE INHERITANCE’. Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new class.

Multiple Inheritance
Multiple Inheritance

Example:

Class Base1   // Base class for Class C
{
       //body
}
Class Base2 // Base class for class C 
{
     //body
}
Class Derived : public B
ase1, public Base2
{
    //body
}

Here, Class Derived is inheriting properties of class Base1 and Base2. So finally in Derived class will have properties of Base1 and Base2 class.

Example:

#include<iostream.h>
class divA
{
	public:
		int a;
		void getA()
		{
			cout<<"Enter an Integer value"<<endl;
			cin>>a;
		}
};
class divB
{
	public:
		int b;
		void getB()
		{
			cout<<"Enter an Integer value"<<endl;
			cin>>b;
		}
};
class divC:public divA,public divB
{
	public:
		int c;
		void add()
		{
			c=a+b;
			cout<<a<<"+"<<b<<"="<<c<<endl;
		}
};
int main()
{
	divC obj;
	obj.getA();
	obj.getB();
	obj.add();
}

Related posts