So far, we have been deriving a class from a single base class. However, it is also possible to derive a class from two or more unreleased base classes. When we derive a class from two or more base classes, this form of inheritance is known as Multiple Inheritance in C++. It allows the new class to inherit the functionality of two or more well developed and tested base classes, thus reusing predefined classes’ features. Like Single Inheritance, each derived class in multiple inheritances must have a kind of relationship with its base classes. This form of inheritance is useful in those situations in which the derived class represents a combination of features from two or more base classes. For Example: In an educational institute, a member can act like a teacher and a student. He may be a student of higher class as well as teaching lower classes.
The syntax for declaring a derived class that inherited from more than one base classes is
class der_class_name access_specifier base1, access_specifier base2, ... { //Implementation };
Here access_specifier may be either public, private or protected. Each base class from which class derived must be specified to the colon’s right after the access specifier and must be separated by commas.
The below figure shows multiple inheritances in which class C derived from class A and class B, independent of each other.
Here, class C inherits all the members of its base classes A and B. The following program segment shows how class C derived from two base classes A and B publicly.
class C : public A,public B { //Implementation };
The inheritance rules and the usages of access specifier for each base class member remain the same as in single inheritance. The access to each base class member is independent of the access to the members of other base classes using which you derive your class. For example: In the above derivation, public members of the base classes A and B inherited as public and protected members are inherited as protected in the derived class C.
Therefore, the object of the derived class can access public members of all the classes.
It can be represented in the following figure :
Some examples of derive class specification for inheriting a class from more than one base classes are as follows :
Class C : private A, private B {.....}; Class C : protected A, public B {....}; Class C : public A, B {... }; Class C : A, B { ... };
In the above examples, if we skip the access specifier while deriving a class, then it is private by default.
#include<iostream.h> #include<conio.h> class base1 { protected: int x; public: void readx() { cout<<"Enter value of x :"; cin>>x; } void showx() {cout<<"x= "<<x;} }; class base2 { protected: int y; public: void ready() { cout<<''Enter value of y :"; cin>>y; } void showy() {cout<<"\ny "<<y;} }; class der public basel,public base2 { private: int z; public: void add() { z=x+y; //Accessing protected members of base classes } void showz () { cout<<"\nz "<<z; } }; int main() { clrscr(); der d1; d1.readx();//calling readx() of basel d1.ready{);//calling ready() of base2 d1.add();//calling add() of der d1.showx();//calling showx() of basel d1.showy();//calling showy() of base2 d1.showz();//calling showz() of der getch(); return 0; } Output Enter value of x : 10 Enter value of y : 20 x = 10 y = 20 z = 30
Explanation: In this program, we define a class base1 containing a protected data member x and public member functions readx() and showx() for inputting and displaying the value of x. Similarly, another class base2 is defined that contains a data member y and two public member function ready() and showy(). The class der is inherited from both the base classes base1 and base2 publicly through multiple inheritances. As the class der is derived publicly from both the base class base1 and base2, the derived class object can access public base1 and base2 in the main(). In the main() , the statement d1.readx () ; invokes the member function readx() of the base1 class for inputting value of x. Similarly the statement, d1.ready() ; invokes the ready() of base2 for inputting value of y. The statement d1.add(); invokes the member function add() of the der class to perform the addition of values of protected data members x and y as they can be accessed directly in the der class. The remaining statement displays the values of x, y and z.