Skip to main content

Inheritance in c++

Hello friends, in this post today, we learn about Inheritance in c++ concept and types of inheritance in c++. Which  is very important. so lets start...

Inheritance in c++, Inheritance
Inheritance 



Inheritance 

Class can acquire the properties and methods of another class.


Inheritance is the process of deriving a new class from an already existing class.

In other words" Inherit all the properties and methods of class into another class, is called Inheritance."

Inheritance provides the idea of reusability i.e., code once written can be used again and again in number of new classes.


The new class is called derived class and old class is called base class.


The new class can use all or some of the features of the already existing class and the programmer can define his own members to the new class.


Types of Inheritance

In c++, there are 5 types of inheritance.


Types of inheritance in c++, inheritance
Types of Inheritance


Syntax of Inheritance:-

class derived_classname : access_spacifier base_classname


Where class is keyword used to create a class derived_classname of new derived class,  access specifiers  may be private, public, or protected. If access specifier is not present default mode is private. base_classname is the name of an already existing class. It may be a user defined or a built-in class.


a)-Single Inheritance (parent-child relationship)

In single level inheritance we have just one base class and one derived class.


Single level inheritance in c++
Single level inheritance 

class A
  {

  };
  
class B:Access_specifier A
  {

  };

Here, class B is derived from class A.


b)-Multilevel Inheritance (Grandparent-parent-child relationship)

In multiple inheritances we have one base class and one derived at one level. At the next level the derived class becomes base class for the next class and so on.


Multilevel inheritance in c++
Multilevel inheritance 

class A
{

};
class B:Access_specifier A
{

};
class C:Access_specifier B
{

};
class D:Access_specifier C
{
  
};

 Here, class C is derived from class B and class B is derived from class A.



c)-Multiple Inheritance (More than one parents-one child relationship)

In multiple inheritance a child can have more than parent i.e., a child can inherit properties from more than one class.


Multiple inheritance in c++
Multiple inheritance 

class A 
  
};
class B
{

};
class C:Access_specifier AAccess_specifier B
{

};

   Here, class C is derived from two classes, class A and class B.



d)-Hierarchical Inheritance (One parent-more than one child relationship)

In this type of inheritance multiple classes share the same base class. That is number of classes inherits the properties of one common base class. The derived classes again may become base class for other classes.


Hierarchical inheritance in c++
Hierarchical inheritance 

class A
 {

 };

class BAccess_specifier A
 {

 };
class C:Access_specifier A
 {

 };

Here, class B, class C  are derived from class A.




e)-Hybrid Inheritance(Mixture of inheritance)

It is a combination of any above inheritance types. That is either multiple or multilevel or hierarchical or any other combination.


Hybrid inheritance in c++
Hybrid inheritance 


class A
 {

 };

class BAccess_specifier A
 {

 };
class C:Access_specifier A
 {

 };
class D:Access_specifier B, Access_specifier D
{

};

Here, class B and class C are derived from class A and class D is derived from class B and class C.

class A, class B and class C is example of Hierarchical Inheritance and class B, class C and class D is example of Multiple Inheritance so this hybrid inheritance is combination of Hierarchical and Multiple Inheritance.


 Advantages of Inheritance:

Reusability of Code

Saves Time and Effort

Faster development, easier maintenance and easy to extend

Capable of expressing the inheritance relationship and its transitive nature which ensures closeness with real world problems



visibility mode in Inheritance

There are basically 3 different types of accessing standards/specifiers/derivation used by which the derived class object access the members of the base class.

A derived class inherits all base class methods with the following exceptions:

        Constructors, destructors and copy constructors of the base class. 

        Overloaded operators of the base class.

        The friend functions of the base class.

When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. We hardly use protected or private inheritance but public inheritance is commonly used. While using different type of inheritance, following rules are applied:


public derivation mode

All the public members of  Base class becomes public members of the derived class.
All the protected members of Base class becomes protected members of the derived class. They are directly accessible by the members on the derived class. 
Private members are never inherited.


private derivation mode

All the public members of Base class becomes private members  of the derived class.
All the protected members of the Base class  becomes private members of the derived class. 
Private members are never inherited.


protected derivation mode

All the public members of Base class becomes protected members  of the derived class.
All the protected members of Base class  becomes protected members  of the derived class.
Private members are never inherited.


    

Visibility mode of inheritance, Access control of Inheritance
Access control of Inheritance



I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with something new information .


Thank you so much for reading. And take care about yourself and your family.





Comments

Popular posts from this blog

Constructor in c++

Hello friends, in this post today, we learn about  what is constructor in c++ and types of constructor . so lets start... Constructor in c++ Constructor:-   A constructor is a “special” member function which initializes the objects of class.  Constructor is used to solve problem of initialization. Properties of constructor:- Constructors are used to construct the object of the class. Constructor name must be same as class name. Constructor is invoked automatically when objects of the class are created. The constructors are always declared in the public section. If declared in the private section then objects are can only be created inside the member functions but serve no purpose. It must be an instance member function , that is, it can never be static. Constructors  do not have any return type not even void so they cannot return any value. Constructor can also be overloaded. Constructors cannot be inherited, but they can be called from the constructors of derived  class . Constructor

What is scope resolution operator in c++

Hello friends, in this post today, we will learn about What is scope resolution operator in c++ and how to use in our program. so let's start.... Scope resolution operator  Scope resolution operator :- Scope resolution operator which is also known as ( membership lable ), represnted as (::) . The scope resolution operator is employed to resolve or extend the scope of variable or functions. C++ is block structured language. We know that the same variable or function name can be used to have different meaning in different block. The scope resolution operator will refer value of worldwide variable or function from anywhere (also from inner block). Without scope resolution operator all variable will refer local value. Example for variabe: #include   <iostream>   using   namespace   std ; int  m= 10 ; int   main () {       int  m= 20 ;      {       int  k=m;        int  m= 30 ;     cout<< "we are in inner block \n " ;      cout<< "k=" <<k<