≡ Menu

How to Use C++ Single and Multiple Inheritance with an Example

Inheritance is the property by which a class can inherit data members and functions of another class.

In this case, the class which is inherited is known as base class while the class which inherits is known as derived or child class.

In this tutorial let us study the concept of inheritance in C++ programming with an example program.

Here is an example of how inheritance can take place :

class fourwheeler
{
public:
    int category;
    int eurolevel;

    int getCategory(void);
    char getEuroLevel(void);
};  

class car : public fourwheeler
{
public :
    char* brand;
    char* model;

    char* getBrand(void);
    char* getModel(void);
};

So we see that there is a class named ‘fourwheeler’ which is derived publicly by class ‘car’. This actually makes sense as in reality also a car should have all the generic characteristics of a fourwheeler. So, in terms of C++ programming, once we create a class ‘fourwheeler’ its members can be reused in any specific fourwheeler category class (like a car). This way C++ empowers you to reuse any required code. This also helps in keeping the code simple and maintainable.

One thing you might be wondering (or might have missed here) is that I have used the term ‘derived publicly’ in the last paragraph. What do we mean by it? Well, as we discussed in C++ introduction article, there are three access specifiers ie public, private and protected. While deriving a class, these specifiers are used to convey what all needs to be inherited and in which manner.

Here are the three points that you need to remember :

  • If base class is derived publicly, the public members of base class become public members of derived class. Similarly, the protected members of base class become protected members of derived class. But, private members of base class are not inherited.
  • If base class is derived in protected mode, both the public and protected members of base class become protected members of derived class. But, private members of base class are not inherited.
  • If base class is derived in private mode, both the public and protected members of base class become private members of the derived class. But, again, private members if the base class are not inherited.

If you are new to C++, start with Introduction to C++ and C++ constructors and destructors.

Coming back to the example above, lets complete it using public inheritance :

#include<iostream>

class fourwheeler
{
public:
    int category;
    int eurolevel;

    inline int getCategory(void)
    {
        return category;
    }
    inline char getEuroLevel(void)
    {
        return eurolevel;
    }
};

class car : public fourwheeler
{
public :
    char* brand;
    char* model;

    char* getBrand(void);
    char* getModel(void);
};

int main(void)
{
    car obj_car;
    obj_car.category = 1;
    obj_car.eurolevel = 3;

    std::cout <<"\n The fourwheeler category is "<<obj_car.category;
    std::cout << "\n The fourwheeler eurolevel is "<<obj_car.eurolevel<<"\n";
    return 0;
}

Now, when this code was compiled and executed, here is the output :

$ ./inheritance 

 The fourwheeler category is 1
 The fourwheeler eurolevel is 3

So we see that the public members of class ‘fourwheeler’ were easily accessed as public members of class ‘car’. Similarly we can do protected and private inheritance using protected and private access specifiers.

One important thing to understand here is how to decide whether public or protected or private inheritance is required? Here is the answer :

  • When the inheritance is public, the derived class and every body else can access the public members of base class through derived class because these members now become the public members of derived class. So, it for you to decide if you want this kind of visibility or not?
  • When the inheritance is protected, only the derived class and it’s children can access these members as these now become the protected members of derived class. Other than them, no one else can access them directly. So, again, its for you to decide if you want this kind of visibility.
  • Similarly, when the inheritance is private, only and only the derived class can access these members as these now become the private members of derived class and hence cannot be derived further. Also, no one else can use them ever. So, here again, it’s for you to decide when to opt for private inheritance.

Moving ahead, the example we just discussed earlier represents single inheritance. There can be multiple inheritance too. This means that a single class can inherit from more than one class simultaneously. Here is an example of that :

#include<iostream>

class sedans
{
public:
    int total_num_sedans;
    int in_production_sedans;
    int ready_sedans;

    int get_total_num_sedans(void)
    {
        return total_num_sedans;
    }
    int get__num_sedans_in_production(void)
    {
        return in_production_sedans;
    }
    int get_num_sedans_ready(void)
    {
        return ready_sedans;
    }
};

class suvs
{
public:
    int total_num_suvs;
    int in_production_suvs;
    int ready_suvs;

    int get_total_num_suvs(void)
    {
        return total_num_suvs;
    }
    int get__num_suvs_in_production(void)
    {
        return in_production_suvs;
    }
    int get_num_suvs_ready(void)
    {
        return ready_suvs;
    }
};

class honda : public sedans, public suvs
{
public :
    int ovrall_total_cars;
    int ovrall_total_production;
    int ovrall_total_ready;

    int get_ovrall_num_cars(void)
    {
        return (ovrall_total_cars = total_num_suvs + total_num_sedans);
    }
    int get_ovrall_in_production(void)
    {
        return (ovrall_total_production = in_production_suvs + in_production_sedans);
    }
    int get_ovrall_ready(void)
    {
        return (ovrall_total_ready = ready_suvs + ready_sedans);
    }

};   

int main(void)
{
    honda cmpny_stats;

    cmpny_stats.total_num_sedans = 5;
    cmpny_stats.total_num_suvs = 10;

    std::cout<<"\n Overall honda cars = "<<cmpny_stats.get_ovrall_num_cars()<<"\n";
    return 0;
}

Here is the output of code above :

Overall honda cars = 15

So we see that through multiple inheritance, class honda was able to inherit both class sedans and class suvs.

Finally, please note that constructors, destructors, friend functions and overloaded operators cannot be inherited.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Bob August 21, 2013, 8:28 am

    Great article. Thanks!

  • Ghasem Pahlavan August 21, 2013, 10:42 am

    Very Usefull. 😀 Thanks!

  • Ansudeen August 21, 2013, 12:01 pm

    Hi,
    Any one can explain with all access specifier.

  • Sachin September 1, 2013, 4:12 am

    Finally , some c++ stuff here.
    Keep up the good work! loving it.
    More C++ pls.

  • Mouli September 13, 2013, 7:46 am

    Simple, nice and well explained. Thanks

  • Riyaz October 7, 2013, 11:11 am

    thanx