Inheriting One Class from Multiple in C++

Inheriting one class from multiple classes in C++ programming. In which an object or class can inherit characteristics and features from more than one parent object or parent class.

Program Multiple Inheritance

Program

#include<iostream.h>
#include<conio.h>
class cls
{
public:
void fun()
{
cout<<"This is fun() function of class cls"<<endl;
}
};
class cls2
{
public:
void fun2()
{
cout<<"This is fun2() function of class cls2"<<endl;
}
};  //class 'cls3' Inheriting from class 'cls' and 'cls2'.
class cls3:public cls, public cls2
{
public:
void fun3()
{
cout<<"This is fun3() function of class cls3"<<endl;
}
};
void main()
{
cls3 obj; //'obj' is object of class 'cls3'.
obj.fun2();
obj.fun3();
obj.fun();
getch();
}
Output
This is fun2() function of class cls2
This is fun3() function of class cls3
This is fun() function of class cls

Program Inheriting One Class from Multiple Classes

Program

#include<iostream.h>
#include<conio.h>
class cls
{
public:
void fun()
{
cout<<"This is fun() function of class cls"<<endl;
}
};
class cls2
{
public:
void fun2()
{
cout<<"This is fun2() function of class cls2"<<endl;
}
};
class cls3
{
public:
void fun3()
{
cout<<"This is fun3() function of class cls3"<<endl;
}
}; //class 'cls4' inheriting from class 'cls2, cls3 and cls'.
class cls4: public cls2, public cls3, public cls
{
public:
void fun4()
{
cout<<"This is fun4() function fo class cls4"<<endl;
}
};
class cls5: public cls4 //class 'cls5' inheriting from class 'cls4'.
{
public:
void fun5()
{
cout<<"This is fun5() function of class cls5"<<endl;
}
};
void main()
{
cls5 obj; //'obj' is object of class 'cls5'.
obj.fun(); //and calling functions.
obj.fun4();
obj.fun2();
obj.fun5();
obj.fun3();
getch();
}
Output
This is fun() function of class cls
This is fun4() function fo class cls4
This is fun2() function of class cls2
This is fun5() function of class cls5
This is fun3() function of class cls3