Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Inheritance Types

In this tutorial, we will learn to use Inheritance Access Control (public, protected and private) in C++ with the help of examples.

Inheritance Types

Inheritance is one of the core feature of an object-oriented programming language. It allows software developers to derive a new class from the existing class. The derived class inherits the features of the base class (existing class).

In C++, we have 5 different types of Inheritance. Namely,

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual Inheritance)

1. Single Inheritance in C++

This is the simplest type of inheritance. In the single inheritance, one derived class can inherit from only one base class.
For example, the class Derived is inheriting from only one Class Base.

class A
{ 
... .. ... 
};
class B: public A
{
... .. ...
};

Here, class B is derived from the base class A.


Example 1: C++ Single Inheritance

#include <iostream>
using namespace std;
class A
{
    public:
      void display(){
          cout<<"Base class A content.";
      }
};
// sub class derived from base class 
class B : public A
{

};

int main(){
    B obj;
    obj.display();
    return 0;
}

Output

Base class A content.

In this program, class B is derived from class A. The obj object of class B is defined in the main() function.

When the display() function is called, display() in class A is executed. It’s because there is no display() function in class B.

The function also doesn’t exist in class B, so the compiler looks for it in class A (as B is derived from A).


2. Multiple Inheritance in C++

Multiple Inheritance is a feature of C++ where a single derived class can inherit property from more than one base class. For example, as explained below, class Derived inherits property from both Class Base1 and Class Base2.

class A
{ 
... .. ... 
};
class B: public A
{
... .. ...
};
class C: public A, public B
{
... .. ...
};

In C++ programming, a class can be derived from more than one parents. For example: A class Parrot is derived from base classes Animal and Bird. It makes sense because Parrot is a Animal as well as a Bird.


Example 2: C++ Multiple Inheritance in C++ Programming

#include <string>
using namespace std;

// First base class 
class Animal {
  public:
    Animal() {
      cout << "Animals can Run." << endl;
    }
};
// Second base class 
class Bird {
  public:
    Bird() {
      cout << "Birds can Fly." << endl;
    }
};
// sub class derived from two base classes 
class Parrot: public Animal, public Bird {

};
int main(){

    // creating object of sub class will invoke the constructor of base classes 
    Parrot b1;
    return 0;
}

Output

Animals can Run.
Birds can Fly.

Ambiguity in Multiple Inheritance

The most obvious problem with multiple inheritance occurs during function overriding.

Suppose, two base classes have a same function which is not overridden in derived class.

If you try to call the function using the object of the derived class, compiler shows error. It’s because compiler doesn’t know which function to call. For example,

class base1
{
  public:
     void someFunction( )
     { .... ... .... }  
};
class base2
{
    void someFunction( )
     { .... ... .... } 
};
class derived : public base1, public base2
{
    
};

int main()
{
    derived obj;

    obj.someFunction() // Error!  
}

This problem can be solved using scope resolution function to specify which function to class either base1 or base2.

int main()
{
    obj.base1::someFunction( );  // Function of base1 class is called
    obj.base2::someFunction();   // Function of base2 class is called.
}

3. Hierarchical Inheritance in C++

In hierarchical inheritance, more than one sub class is inherited from a single base class. In this, all features that are common in child classes are included in the base class.

For example: Football, Cricket, Badminton are derived from Sports class.


Syntax of Hierarchical Inheritance

class base_class {
     ... .. ...
}

class first_derived_class: public base_class {
     ... .. ...
}

class second_derived_class: public base_class {
     ... .. ...
}

Example 3: C++ Program to implement Hierarchical Inheritance

#include <iostream>
using namespace std;

// base class 
class Animal
{
    public:
        Animal(){
            cout<<"Animals Can Run.";
        }
};
// First Sub class 
class Dog : public Animal
{

};
// Second Sub class 
class Cat : public Animal
{

};

int main(){
    Cat obj1;
    Dog obj;2

    return 0;
}

Output

Animals Can Run.
Animals Can Run.

4. Multilevel Inheritance in C++

In multilevel inheritance, the derived class inherits property from another derived class.
For example, class B inherits from class A and class C inherits property from class B.

class A
{ 
... .. ... 
};
class B: public A
{
... .. ...
};
class C: public B
{
... ... ...
};

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


Example 4: C++ Multilevel Inheritance

#include <iostream>
using namespace std;

class Animal
{
    public:
    Animal()
      {
          cout<<"This is a Animal.";
      }
};

class fourLegs : public Animal
{
    public:
    fourLegs()
      {
          cout<<"Objects with 4 legs are Animals.";
      }
};

class Cow : public fourLegs
{
    public:
    Cow()
      {
          cout<<"Cow has 4 Legs.";
      }
};

int main()
{
    Cow obj;
    obj.display();
    return 0;
}

Output

This is a Animal.
Objects with 4 legs are Animals.
Cow has 4 Legs.

5. Hybrid (Virtual) Inheritance in C++

Hybrid inheritance is a combination of more than one type of inheritance (Combining Hierarchical inheritance and Multiple Inheritance.).
For example:- A child and parent class relationship that follows multiple and hierarchical inheritance both can be called hybrid inheritance.


Example 5: C++ Hybrid (Virtual) Inheritance

#include <iostream>
using namespace std;

class Animal{
    public:
    Animal()
      {
          cout<<"This is a Animal.";
      }
};

class Fare{
    public:
    Fare()
      {
          cout<<"Fare of Animal.";
      }
};

class Dog : public Animal
{

};

class Cat : public fourLegs, public Fare
{

};
int main(){

    Car obj2;
    return 0;
}

Output

This is a Animal.
Fare of Animals.

 

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!