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
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).
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 is very nicely presented! Good work
Chart of Accessibility modes is really simple to understand……
Nice work 🙂
nice one!!!!!! really helpfull
easy to learn….
nice one!!!!!! really helpfull…………
its really nice
Really great stuff,
very helpfull,
thanks a lot………………..
thank you for help to solve my problem
nice… but need sum mor xplannation about types of inheritance..
easy to understand…. helped me a lot
Really good explanation….
really gud explanation:):)
awesome:)
such a helpful artical..
good explanation
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.
@kvs kumar : thx nice catch, fixed the mistake.
owsm!!!!!!!!!!.xplanation
example is easy i understand easily
nice help
these are perfect tutorials:)
good explanation!!
good to understand
easy to understand…
thanx a lot for the help…. really nice work
A simple,short but sweet example….
The arrow direction is not correct in 4 and 5. Nice info though.
thanks u soo much
Thnxxxxx Dude!!!!!!!!!!!!!!!!!
very nice
give hybrid inheritance with example
i am satisfied good explanation
neat with clear explanation
its really helpful ,.,.,.,,,thnx
tnxs for give a such a nice explanation