Multiple inheritance in C++ example
What is multiple inheritance ?
When more than one class is inherited by one class, that inheritance is called multiple inheritance. In this, the property of more than one class is inherited by one class. In this there is more than one super class and one sub class.
Note:- Java does not support multiple inheritance.
Multiple Inheritance This is the third type of inheritance.
Multiple inheritance can have more than one base class and one derived class. Derived class inherits two or more base classes, it is called Multiple Inheritance.
Syntax for Multiple Inheritance
class Base_class_Name1
{
// body_of_Base_class
};
class Base_class_Name2
{
//body_of_Base_class2
};
class Derived_class_Name : Access_Specifier Base_class_Name1, Access_Specifier Base_cass_Name2,.......
{
//body_of_Derived_class2
};
Program:-
#include <iostream.h> using namespace std; class A{ public: display1(){ cout<<"Class A"<<endl; } }; class B{ public: display2(){ cout <<"Class B"<<endl; } }; class C: public B, public A{ public: display3(){ cout <<"Class C"<<endl; } }; int main(){ C obj; obj.display1(); obj.display2(); obj.display3(); return 0; }
Output:-
Class A Class B Class C