Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




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.


Share this article   |    Print    |    Article read by 3276 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles: No related article
Related Interview Questions: No related interview question