INHERITANCE
Inheritance, Base Class, Derived Class, Single Inheritance, Multiple, Hierarchical, Multilevel and Hybrid Inheritance, Visibility Mode, Containership, Overriding of function in inheritance, Virtual Base Class
Inheritance:It is the capability of one class to inherit properties from another class.
Base Class: It is the class whose properties are inherited by another class. It is also called Super Class.
Derived Class:It is the class that inherit properties from base class(es).It is also called Sub Class.
FORMS OF INHERITANCE
Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class.
Multiple Inheritance:It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es)
Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherits from one base class.
Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes.
Hybrid Inheritance:The inheritance hierarchy that reflects any legal combination of other four types of inheritance.
Visibility Mode:It is the keyword that controls the visibility and availability of inherited base class members in the derived class.It can be either private or protected or public.
Private Inheritance:It is the inheritance facilitated by private visibility mode.In private inheritance ,the protected and public members of base class become private members of the derived class.
Public Inheritance:It is the inheritance facilitated by public visibility mode.In public inheritance ,the protected members of base class become protected members of the derived class and public members of the base class become public members of derived class.;
Protected Inheritance:It is the inheritance facilitated by protected visibility mode.In protected inheritance ,the protected and public members of base class become protected members of the derived class.
Base Class Visibility | Derived class visibility | ||
Public derivation | Private derivation | Protected derivation | |
Private | Not inherited | Not inherited | Not inherited |
Protected | Protected | Private | Protected |
Public | Public | Private | Protected |
Containership:When a class contains objects of other class types as its members, it is called containership.It is also called containment,composition, aggregation.
Execution of base class constructor
Method of inheritace | Order of execution |
class B : public A { }; | A(); base constructor B(); derived constructor |
class A : public B, public C | B();base (first) C();base (second) A();derived constructor |
When both derived and base class contains constructors, the base constructor is executed first and then the constructor in the derived class is executed. In case of multiple inheritances, the base classes are constructed in the order in which they appear in the declaration of the derived class.
Overriding of method(function) in inheritance
We may face a problem in multiple inheritance, when a function with the same name appears in more than one base class. Compiler shows ambiguous error when derived class inherited by these classes uses this function.
We can solve this problem, by defining a named instance within the derived class, using the class resolution operator with the function as below :
class P : public M, public N //multiple inheritance
{
public :
void display() //overrides display() of M and N
{
M::display()
}
};
{
public :
void display() //overrides display() of M and N
{
M::display()
}
};
we can now used the derived class as follows :
void main()
{
P obj;
obj.display();
}
void main()
{
P obj;
obj.display();
}
Virtual Base Class
Multipath inheritance may lead to duplication of inherited members from a grandparent base class. This may be avoided by making the common base class a virtual base class. When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited.
class A
{
....
....
};
class B1 : virtual public A{
....
....
};
{
....
....
};
class B2 : virtual public A
{
....
....
};
class C : public B1, public B2
{
.... // only one copy of A
.... // will be inherited
};
....
....
};
class B2 : virtual public A
{
....
....
};
class C : public B1, public B2
{
.... // only one copy of A
.... // will be inherited
};
ReplyDeleteVery informative article.Thank you author for posting this kind of article .
http://www.wikitechy.com/view-article/hybrid-inheritance-program-in-cpp-with-example-and-explanation
Both are really good,
Cheers,
Venkat