CS Electrical And Electronics
@cselectricalandelectronics
All PostsCPPProgrammingWhat Is

What Is An Inheritance In CPP, Types Of Inheritance, Code, Syntax

Hello guys, welcome back to my blog. In this article, I will discuss what is an inheritance, types of inheritance, working, codes, or programs on in-heritance.

If you require an article on some other topics then comment us below in the comment box. You can also catch me @ Instagram – Chetan Shidling.

Also, read:

  1. Difference Between Machine Learning Artificial Intelligence Deep Learning
  2. App Developers, Skills, Job Profiles, Scope, Companies, Salary
  3. Computer Science Engineers, Skills, Salary, Companies, Job profiles
  4. Artificial Intelligence Scope, Companies, Salary, Roles, Jobs

Inheritance

The inheritance is a quite famous and special feature of object-oriented programming that is supported in C++.

What Is An Inheritance?

Inheritance means creating a new class from already existing classes is called Inheritance. The existing class is called a base class or superclass or parent class. The new class which is created using an existing class is known as a derived class or child class or subclass. The derived class apart from having its own member functions and data members can access some or all the member functions or data members of the parent or base class. The in-heritance allows the reusability of the code.

The in-heritance can be viewed as a tree-like structure. Consider the following diagram.

Inheritance
  1. In the above diagram, SHAPE is the base class.
  2. The class TRIANGLE is derived from the class SHAPE. So, TRIANGLE is the derived class.
  3. The class RECTANGLE is also derived from the base class SHAPE.
  4. The class CIRCLE is derived from the class RECTANGLE.

Syntax Of Inheritance

For example, consider the following base class.

class B_class               //Base class
{
    //members of class B
};

The three possible styles of derivation are shown below:

01. Derivation using public access specifier

class D: public B_class        //public derivation
{
       //members of class D
};

Note:

  1. Class is a keyword in C++.
  2. D is the name of the derived class.
  3. Access specifiers can be private, public, or protected.
  4. B_class is the name of the base class.

02. Derivation using private access specifier

class D: private B_class       //private derivation
{
       //members of class D
};

03. Derivation using default access specifier

class: B_class        // It will be private derivation by default
{
       //members of class D
};

Program On Inheritance

#include<iostream.h>
class X
{
   private:
        int a;
   protected:
        int b;
   public:
        int c;
      
        void abc_x()
        {
             a = 1;
             b = 2;
             c = 3;
        }
};

class Y: protected X
{
   public:
       void abc_y()
       {
             a = 10;      //Error: Private member cannot be inherited
             b = 20;      // OK
             c = 30;      // OK
             abc_x();     // OK
       }
);

class Z: public Y
{
   public:
      void abc_z()
      {
         a = 100;     //Error: Private member cannot be inherited
         b = 200;     //OK
         c = 300;     //OK
         abc_y();     //OK
         abc_x();     //OK
       }
};

void main()
{
    z  ob1;
    ob1.a = 100;     //Error: Not inherited and cannot be accessed
    ob2.b = 200;     //Error: Protected data member cannot be accessed
    ob3.c = 300;     //Error: Protected data member cannot be accessed

    ob1.abc_z();    //OK
    ob1.abc_y();    //OK
}

Benefits of Inheritance

  1. The main benefit of Inheritance is code reusability. Now, I will discuss “How does in-heritance enable code reusability?”, the reusability can be done as shown below.
  2. The library programmer defines several classes consisting of data members and generally used member functions. These classes can be described as base classes.
  3. The other programmer who needs to create the application (a new program written to achieve a task) can inherit the data members and member functions from the previously existing classes. These classes can be denominated as derived classes.
  4. The programmer can add the special data members and write the essential member functions specific to this application and can be put in the derived classes.
  5. Thus, the derived classes, apart from having their own data members and member functions, can also use the data members and member functions of an already existing class.

Types Of Inheritance

The different kinds or types of inheritance are:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

01. Single Inheritance

A single class derived from an already existing class is termed as single in-heritance. The existing class is called a base class and the class which is obtained from the base class is called derived class. For example, let A is existing class and B has to be derived from class A. The representation is shown below.

Single inheritance

This single in-heritance can be explained using the following program.

class A
{
   private:
      int a;

   public:
      inita(){a = 10;}
      void showa() {cout << a; }
};

class B: public A
{
    private:
       int b;

    public:                          
       initb() { b = 20; }
       void showb() { cout << b; }
};

void main()
{
     Y ob;
     ob.inita();     //ob.a is initialized to 10
     ob.intb();      //ob.b is initialized to 20
 
     ob.showa();   //10
     ob.showb();   //20
}

Note: In this derivation, some of the data members of base class are also the data members of the derived class.

02. Multiple Inheritance

A class derived from two or more already existing classes is said to have multiple inheritance. This in-heritance allows us to combine the various features of several existing classes into a new class. It is similar to the child inheriting the beauty of the mother and intelligence of the father. The representation along with example for multiple in-heritance is shown below.

Multiple Inheritance
#include<iostream.h>

//Base class 1

class A
{
    private:
       int a;
    public:
       inita() { a = 10; }
       showa() { cout << a; }
};

//Base class 2
class B
{  
    private:
       int b;
    public:
       initb() { b=20; }
       showb() { cout << b; }
};

class C: public A, public B   //Derived class
{
        private:
               int c;
        public:
               initc() { c = 30; }
               showc() { cout << c; }
}

void main()
{
     Z ob;
     ob.inita();  
     ob.showa();
     ob.initb();
     ob.showb();
     ob.initc();
     ob.showc();
}

Output:

10

20

30

Note:

  1. A derived class object using multiple inheritances contains the members defined in this class along with members defined in the base classes.
  2. So, the size of the derived object = sum of the sizes of data members of the derived class + sum of sizes of data members of the respective parent classes.
  3. It is possible to call the members of base class along using an object of the derived class.

03. Hierarchical Inheritance

If a single class serves as a base class for more than one derived class, it results in hierarchical in-heritance. The representation of hierarchical inheritance is shown below.

Hierarchical Inheritance

For example, consider the following program:

#include<iostream.h>

class C
{
   public:
      void showb()
      {
          cout << "Show of class C";
      }
};

class A: public C
{
    public:
      void showx()
      {
           cout << "Show of class X";
      }
};

class B: public C
{
     public:
       void showy()
       {
          cout << "Show of class Y";
       }
};

void main()
{
    A ob1;
    B ob2;
  
    ob1.showb();   //Show of class C
    ob1.showx();   //Show of class A
    ob2.showb();   //Show of class C
    ob2.showy();   //Show of class B
}

The hierarchical in-heritance is the best example to show the concept of code re-usability. The common features of two or more classes can be inherited to a new class using this approach. Thus, the need to duplicate the common features in more than one is eliminated.

04. Hybrid Inheritance

The method of deriving a class using a combination of multilevel or multiple or hierarchical inheritances is called hybrid inheritance. The representation of hybrid in-heritance is shown below.

Hybrid Inheritance

05. Multilevel Inheritance

if a class is derived from its parent which in turn is derived from its parent and so on to any depth is said to have a multilevel In-heritance. In this case, the data members and member functions can be inherited down the line for only one class. The representation that shows the concept of multilevel in-heritance.

Multilevel Inheritance
class C
{
   private:
      int c;

   public:
      inita(){c = 10;}
      void showc() {cout << c; }
};

class B: public C
{
    private:
       int b;

    public:                          
       initb() { b = 20; }
       void showb() { cout << b; }
};

class A: public B
{
    private:
       int a;

    public:                          
       initb() { a = 30; }
       void showa() { cout << a; }
};

class D: public A
{
    private:
       int d;

    public:                          
       initd() { d = 40; }
       void showd() { cout << d; }
};

void main()
{
     Y ob;
     ob.initc();     //ob.a is initialized to 10
     ob.intb();      //ob.b is initialized to 20
     ob.inita();     //ob.a is initialized to 30
     ob.intd();      //ob.b is initialized to 40
 
     ob.showc();   //10
     ob.showb();   //20
     ob.showa();   //30
     ob.showd();   //40
}

I hope this article may help you all a lot. Thank you for reading. If you have any doubts comment below.

Also, read:

Author Profile

CS Electrical And ElectronicsChetu
Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking
Share Now

CS Electrical And Electronics

Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking

Leave a Reply

Your email address will not be published. Required fields are marked *