Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is Multiple Inheritance in C++

In Multiple Inheritance, the new class is derived from more than one base calsses. The derived class inheritance all the member functions and data members of the base class. It can also have its own members.
Consider the following pictorial form, explaining multiple inheritance between three classes A, B and C respectively.


This technique reduce the size of program and saves development time as compared to single inheritance. However, multiple inheritance is more complicated than single.
Some of the object oriented programming languages doesn't support multiple inheritance while C++ fully support it. The founder of C++ Bjarne Stroustrup said:

Multiple Inheritance (object-oriented programming) was widely supposed to be very difficult to implement efficiently. For example, in a summary of C++ in his book on objective C Brd.Cox actually claimed that adding Multiple inheritance to C++ was impossible. Thus, multiple inheritance seemed more of a challenge. Since I had considered multiple inheritance as early as 1982 and found a simple and efficient implementation technique in 1984. I couldn't resist the challenge. I suspect this to be the only case in which fashion affected the sequence of events.
Reference: (The Design and Evolution
of C++. page no. 417.)

Defining Derived Class

As we know that inheritance can be done differently with three different access specifiers (public, private and protected). The general syntax to derive a new class using multiple inheritance is similar to that of single inheritance:

class  new_class :  mod1 base1 , mod2 base2,...

 {

   ------

   ------

   //body of the class

   ------

 };


Where:
new_class It indicates the name of new class which is to be derived from multiple base classes.
mod1, mod2 They represent the access modifiers (public, private or protected) of the base classes base1 and base2.
base1, base2 They indicate the name of base classes.
Consider, to define a new class C from two base classes A and B, the structure of the base classes A & B and derived class C are as follows:

 class A

            {

               -------

               //body of class A

               -------

            };

 class B

            {

               -------

             //body of class B

               -------

            };

 class C : public A, public B

            {

               -------

            //body of derived claas C

               -------

            };

As in single inheritance, the members of the base classes A & B are accessed by derived class objects in similar way. In above structure, the objects of class C are able to access public and protected members of class A & B.



This post first appeared on Programming Explain, please read the originial post: here

Share the post

What is Multiple Inheritance in C++

×

Subscribe to Programming Explain

Get updates delivered right to your inbox!

Thank you for your subscription

×