Xpode.com        Click here to Print this article.

Multiple Inheritance

It is the process of deriving only one new class from multiple base classes.



Here B1, B2 and B3 are the base classes and D is the derived class.

The syntax and example for multiple inheritance in case of C++ is given below:

Syntax:


class B1
{
-------
-------
};

class B2
{
-------
-------
};

class B3
{
-------
-------
};

class D : visibility_label B1, visibility_label B2,
visibility_label B3
{
-------
-------
};

Here the visibility_label can be private, protected or public. If we do not specify any visibility_label then by default is private.

For example:

				

class A
{
protected:
int x;
};

class B
{
protected:
int y;
};

class C
{
protected:
int z;
};

class D : private A, private B, private C
{
private:
int sum;
public:
void get_data ( )
{
cout<<"Enter data:";
cin>>x>>y>>z;
}
void add ( )
{
sum=x+y+z;
cout<<"\nThe sum is:" <<sum;
}
};

void main ( )
{
D obj;
clrscr ( );
obj.get_data ( );
obj.add ( );
getch ( );
}

Here ‘A’, ‘B’ and ‘C’ are base classes and ‘D’ is derived class that has been inherited from all three classes privately. The members of the base classes have been declared as protected. They will become private members of the ‘D’ class since the type of the derivation is private. Since the inherited members have become private they can not be accessed by the object of the ‘D’ class.




http://
http://

Contributed by:
Rohit kakria
I am software developer, moderator of xpode.com

Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=14

Click here to go on website