Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

What are the Different Types of Inheritance in C++?

Last Updated on April 27, 2023 by Prepbytes

Inheritance is one of the most important features of Object Oriented Programming (OOPs), and it plays a critical role in creating flexible and reusable code. Inheritance in C++ is a powerful feature that allows you to create new classes based on existing classes. In this article, we’ll explore the concept of inheritance in C++, its syntax, various types of inheritance, and modes of inheritance in C++.

Define Inheritance in C++

To define inheritance in C++, we first need to understand two important terms which are child class and parent class.

Child Class: A child class or derived class is a class that is derived from an existing class. The child class inherits all the data members and member functions of the parent class and can also define its own data members and member functions. The child class can override the parent class’s methods or add new methods and properties to the class.

Parent Class: The parent class, also known as the base class or superclass, provides a template or blueprint for creating child classes. The child classes inherit the parent class’s properties and behaviors, which can then be modified or extended as needed.

The concept of Inheritance in object-oriented programming (OOP) allows one class to inherit properties and behaviors from another class. Inheritance enables you to reuse existing code and create new classes based on existing classes. It enables you to create a hierarchy of classes that share common properties and behaviors, making it easier to build complex applications and maintain them over time. Inheritance also helps to improve code readability, maintainability, and reusability.

Syntax of Inheritance in C++

The Basic Syntax for defining the child class and parent class in all types of inheritance in C++ is as follows:

class parent_class
{
    //class definition of the parent class
};
class child_class : visibility_mode parent_class
{
   //class definition of the child class
};

Syntax Description:

  • Parent_class: Name of the parent class or the base class.
  • Child_name: Name of the child class or the derived class.
  • Visibility_mode: visibility mode specifies how the data members of the child class inherit from the parent class.

Why Do We Need Inheritance in C++?

Here are some reasons why we need inheritance in C++:

  1. Code Reusability: Inheritance enables code reuse by allowing you to create new classes based on existing classes. The child classes can inherit properties and methods from the parent class, which means that you can reuse code that you’ve already written and create new classes that share common properties and behaviors.
  2. Modularity and Organization: Inheritance provides a modular and organized approach to programming by allowing you to create a hierarchy of classes that share common properties and behaviors. This makes it easier to manage complex applications and modify code over time.
  3. Polymorphism: Inheritance allows for polymorphism, which means that the child classes can have their own implementations of the parent class’s methods. This enables you to create objects that can behave in different ways depending on their context, making your code more flexible and adaptable.
  4. Reduced Code Duplication: Inheritance reduces code duplication by allowing you to define common properties and methods in the parent class and then inherit them in the child classes. This reduces the amount of code you need to write and maintain.
  5. Encapsulation: Inheritance supports encapsulation by allowing you to control access to the parent class’s properties and methods. This means that you can define some properties and methods as private or protected, which can only be accessed by the child classes, while others can be public and accessed by anyone.

Modes of Inheritance in C++

There are three modes of inheritance in C++ that define how the derived class inherits the base class members:

  1. Public Mode: In public mode, the public and protected members of the base class are inherited as public and protected members of the derived class, respectively. The private members of the base class are not inherited. This means that the derived class can access the public and protected members of the base class, but cannot access its private members.

    The syntax for public inheritance is as follows:

    class Derived: public Base {
       // class members
    };

    Here, Derived is the derived class, and Base is the base class. The keyword public specifies the access level for the base class members that will be inherited.

  2. Protected Mode: In protected mode, the public and protected members of the base class are inherited as protected members of the derived class. The private members of the base class are not inherited. This means that the derived class can access the public and protected members of the base class, but cannot access its private members, except within the derived class or its friend functions.

    The syntax for protected inheritance is as follows:

    class Derived: protected Base {
       // class members
    };

    Here, Derived is the derived class, and Base is the base class. The keyword protected specifies the access level for the base class members that will be inherited.

  3. Private Mode: In private mode, the public and protected members of the base class are inherited as private members of the derived class. The private members of the base class are not inherited. This means that the derived class can access the public and protected members of the base class, but only within the derived class or its friend functions.

    The syntax for private inheritance is as follows:

    class Derived: private Base {
       // class members
    };

    Here, Derived is the derived class, and Base is the base class. The keyword private specifies the access level for the base class members that will be inherited.

Types of Inheritance in C++

There are five types of inheritance in C++.

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance
  • Single Inheritance in C++
    In single inheritance, a class is allowed to inherit from only one class according to data structures in C++ i.e. one subclass is inherited by one base class only.

    Syntax of Single Inheritance in C++:

    class sub_class : access_mode base_class
     {
         //body of subclass
     };

    Example of Single Inheritance in C++:
    Here is an example of single inheritance in C++:

    // base class 
    class Vehicle { 
        public: Vehicle() { 
            cout << "This is a Vehicle" << endl; 
        } 
    }; 
    // sub class derived from two base classes 
    class Car: public Vehicle { }; 
    
    // main function 
    int main() {       
    // creating object of sub class will invoke the constructor of base classes     
    Car obj;
    return 0; }
    

    Output

    This is a vehicle
  • Multiple Inheritance in C++
    In this type of inheritance a single derived class may inherit from two or more than two base classes. Multiple inheritances specify access specifiers separately for all the child classes at the time of inheritance. The constructors of inherited classes are called in the same order in which they are inherited.

    Syntax of Multiple Inheritance in C++

    class sub_class : access_mode base_class1, base_class2
    {
       //body of subclass
    };

    Example of Multiple Inheritance in C++
    Here is an example of multiple inheritance in C++:

    class A {
      public:
      A() { 
           cout << "A's constructor called" << endl; 
          }
      };
      class B {
        public:
        B() { 
             cout << "B's constructor called" << endl; 
            }
       };
      class C: public B, public A {  // Note the order
        public:
        C() {
             cout << "C's constructor called" << endl; 
             }
        };
      int main() {
         C c;
         return 0;
      }
    

    Output:

    B’s constructor called
    A’s constructor called
    C’s constructor called
  • Multilevel Inheritance in C++
    In this type of inheritance, a derived class is created from another derived class. Let’s take an example: there are 3 classes. A is the base class that derives from class B. Therefore B is the derived class of A. And C class is derived from class B. Therefore, B is the base class for class C.

    Example of Multilevel Inheritance in C++
    Here is an example of multilevel inheritance in C++:

    class A {
        public:
        A() {
             cout << "A's constructor called" << endl;
            }
        };
        class B : public A {
           public:
           B() { 
                cout << "B's constructor called" << endl; 
                }
           };
        class C: public B {
          public:
          C() { 
              cout << "C's constructor called" << endl; 
              }
          };    
        int main() {
           C c;
           return 0;
       }
    

    Output:

    A’s constructor called
    B’s constructor called
    C’s constructor called
  • Hierarchical Inheritance in C++
    In this type of inheritance more than one subclass inherits from a single base class. This inheritance has a tree-like structure in which every class acts as a base class for one or more child classes.

    Example of Hierarchical Inheritance in C++
    Here is an example of hierarchical inheritance in C++:

    // base class 
    class A { 
        public: 
        A() { 
           cout<<"This is A’s constructor"<< endl; 
        } 
    }; 
    // first sub class 
    class B: public A { }; 
    // second sub class 
    class C: public A { }; 
    // main function 
    int main() {    
        /* creating object of sub class will invoke the constructor of base class */
      B obj1;
      C obj2;
     return 0; 
    } 
    

    Output:

    This is A’s constructor
    This is B’s constructor
  • Hybrid Inheritance in C++
    Hybrid inheritance is a combination of two or more types of inheritance. For example, the combination of multiple inheritance and multilevel inheritance can be combined together which forms a hybrid inheritance.

    Example of Hybrid Inheritance in C++
    Here is an example of Hybrid inheritance in C++:

    // base class
    class A {
        public:
        A() {
             cout << "This is A" << endl;
            }
      };
    //base class 
    class B {
        public:
        B() {
             cout<<"This is B " <<endl;
             }
    };
    // first sub class
    class C: public A {
    };
    // second sub class
    class D: public A, public B {
    };
    // main function   
    int main() {  
    // creating object of sub class will invoke the constructor of base //class
     D d;
     return 0;
    }
    

    Output:

    This is A
    This is B

Conclusion
In conclusion, inheritance in C++ allows for code reusability, modularity, and polymorphism. It enables you to design a hierarchy of classes that share common characteristics and behaviors, making complicated applications and code modifications easier to manage over time. You can use inheritance to eliminate code duplication, enhance encapsulation, and construct objects that can behave differently based on their context.

FAQs on Inheritance in C++

Here are some frequently asked questions on inheritance in C++.

Q1: Can a derived class access private members of a base class?
Ans: No, a derived class cannot access the private members of a base class.

Q2: Can a derived class override a method of a base class?
Ans: Yes, a derived class can override a method of a base class by defining its own implementation of the method.

Q3: Can a derived class add new members to itself?
Ans: Yes, a derived class can add new members to itself.

Q4: What is virtual inheritance?
Ans: Virtual inheritance is a type of inheritance that prevents the creation of multiple instances of a base class when it is inherited by multiple derived classes.

Q5: Can a class be both a base class and a derived class?
Ans: Yes, a class can be both a base class and a derived class.

Leave a Reply

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