C++ Inheritance

Inheritance is a concept of linking two or more classes with each other in a hierarchical manner so that their properties and functions can be shared. (One class will extend to another class.) This leads to the biggest advantage of re-usability of the members and avoids redundancy.

Inheritance leads to various issues such as:

  • What is Inherited
  • types of inheritance
  • accessibility modes (public, private and protected) in inheritance
  • Friend function and inheritance etc.

#include <iostream>
using namespace std;

class Cpolygon
{
	protected:
		int width, height;
	public:
		void input_values (int one, int two)
		{
			width=one;
			height=two;
		}
};

class Crectangle: public Cpolygon
{
	public:
		int area ()
		{
			return (width * height);
		}
};

class Ctriangle: public Cpolygon
{
	public:
		int area ()
		{
			return (width * height / 2);
		}
};

int main ()
{
	Crectangle rectangle;
	Ctriangle triangle;
	rectangle.input_values (2,2);
	triangle.input_values (2,2);
	cout << rectangle.area() << endl;
	cout << triangle.area() << endl;
	return 0;
}

In the example above we have used the protected members of the class Cpolygon in the class Crectangle and in the Ctriangle class. This is only possible through Inheritance.

What is inherited?

When inheritance is done, various links and tables (index, virtual etc) are created which are used to provide the accessibility of the members of the base class in derived class and in other class hierarchy. This means saying “public members are inherited” is better to say as “public members become accessible”.

A derived class inherits every member of a base class except:

  • its constructor and its destructor
  • its friends
  • its operator=() members

Types of Inheritance

There are five different inheritances supported in C++:

  • (1) Simple / Single
  • (2) Multilevel
  • (3) Hierarchical
  • (4) Multiple
  • (5) Hybrid

Types of Inheritance flow chart

Multiple Inheritance

Multiple inheritance is achieved whenever more than one class acts as base classes for other classes. This makes the members of the base classes accessible in the derived class, resulting in better integration and broader re-usability.
Take a look at an example:


#include <iostream>
using namespace std;

class Cpolygon
{
	protected:
		int width, height;
	public:
		void input_values (int one, int two)
		{
			width=one;
			height=two;
		}
};

class Cprint
{
	public:
		void printing (int output);
};

void Cprint::printing (int output)
{
	cout << output << endl;
}

class Crectangle: public Cpolygon, public Cprint
{
	public:
		int area ()
		{
			return (width * height);
		}
};

class Ctriangle: public Cpolygon, public Cprint
{
	public:
		int area ()
		{
			return (width * height / 2);
		}
};

int main ()
{
	Crectangle rectangle;
	Ctriangle triangle;
	rectangle.input_values (2,2);
	triangle.input_values (2,2);
	rectangle.printing (rectangle.area());
	triangle.printing (triangle.area());
	return 0;
}

Note:the two public statements in the Crectangle class and Ctriangle class.

Accessibility modes and Inheritance

We can use the following chart for seeing the accessibility of the members in the Base class (first class) and derived class (second class).

Accessibility modes inheritance chart

Here X indicates that the members are not inherited, i.e. they are not accessible in the derived class.

That is all for this tutorial.
In the next C++ tutorial we look at friend function and friend classes.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 33 responses to “C++ Inheritance”

Why not let us know what you think by adding your own comment!

  1. ireshagun on May 1st, 2010:

    This is very nicely presented! Good work

  2. SSA on May 6th, 2010:

    Chart of Accessibility modes is really simple to understand……
    Nice work :)

  3. kc on September 29th, 2010:

    nice one!!!!!! really helpfull

  4. ashruff on January 26th, 2011:

    easy to learn….

  5. sellamuthu on February 8th, 2011:

    nice one!!!!!! really helpfull…………

    its really nice

  6. San Bhardwaj on February 28th, 2011:

    Really great stuff,
    very helpfull,
    thanks a lot………………..

  7. darshan tejani on March 8th, 2011:

    thank you for help to solve my problem

  8. shamshad on June 12th, 2011:

    nice… but need sum mor xplannation about types of inheritance..

  9. shiva on October 18th, 2011:

    easy to understand…. helped me a lot

  10. Viji on November 7th, 2011:

    Really good explanation….

  11. karthik on November 28th, 2011:

    really gud explanation:):)
    awesome:)

  12. varsha on December 20th, 2011:

    such a helpful artical..

  13. uma on March 16th, 2012:

    good explanation

  14. k v s kumar on March 21st, 2012:

    in types of inheritance u put the un sequenced members
    i.e 1 2 3 2 3 instead of 1 2 3 4 5.
    and more over your presentation is very good.

  15. admin on March 21st, 2012:

    @kvs kumar : thx nice catch, fixed the mistake.

  16. chakshu on April 16th, 2012:

    owsm!!!!!!!!!!.xplanation

  17. deepika on May 13th, 2012:

    example is easy i understand easily

  18. Aman on May 14th, 2012:

    nice help

  19. Sevim on June 8th, 2012:

    these are perfect tutorials:)

  20. shelly on June 13th, 2012:

    good explanation!!

  21. sowjanya on July 16th, 2012:

    good to understand

  22. abdul on July 18th, 2012:

    easy to understand…

  23. lovingdamsel on August 31st, 2012:

    thanx a lot for the help…. really nice work

  24. Shilpa on September 3rd, 2012:

    A simple,short but sweet example….

  25. biju mohamed on October 6th, 2012:

    The arrow direction is not correct in 4 and 5. Nice info though.

  26. jaism on November 26th, 2012:

    thanks u soo much

  27. Advait Shastri on February 2nd, 2013:

    Thnxxxxx Dude!!!!!!!!!!!!!!!!!

  28. ajmal ps on February 3rd, 2013:

    very nice

  29. vitthal karad on April 2nd, 2013:

    give hybrid inheritance with example

  30. vitthal karad on April 2nd, 2013:

    i am satisfied good explanation

  31. sathish kumar on July 9th, 2013:

    neat with clear explanation

  32. vicky on September 17th, 2013:

    its really helpful ,.,.,.,,,thnx

  33. nice explanination... m satisfyied.... on October 18th, 2013:

    tnxs for give a such a nice explanation

Leave a Reply: