6. Multi-path Inheritance
It refers to the process of deriving a class from two or more classes which is being derived from the base class.
Figure: Multipath Inheritance
Syntax
class Baseclass
{
private://Access specifiers
public:
void function()
{
//statements
}
};
class Derivedclass1: virtual public
Baseclass
{
//statements
};
class Derivedclass2: virtual public
Baseclass
{
//statements
};
class Derivedclass3: public
Derivedclass2
{
public:
void function()
{
function();
}
};
Program
#include<iostream.h>
#include<conio.h> ,
class Base
{
protected:
int b;
};
class D1 : public virtual Base
//declaration of virtual base‘class.
{
protected: :
int dl; //declaration of
member variable
};
class D2 : public virtual Base
//declaration of virtual class
{
protected:
int d2; //declaration of
member variable
};
class D3 : public D1, D2
//declaration of virtual class
{
int d3; //declaration of member of
variable
public:
void get() //declaration of member
function D3.
{
cout<< “Give the input
values for b,d1,d2 and d3 =”;
cin>>b>>d1>>d2>>d3;
}
void put()
{
cout<<“b=”<<b;
cout<<“\nd1=” <<d1;
cout<<“\nd2=” <<d2;
cout<<“\nd3=” <<d3;
}
};
int main()
{
clrscr();
D3 x;
x.get(); //data is read
x.put(); //data is displayed
getch();
return 0;
}
Output