Introduction to C++ | Inheritance

C++ Inheritance

Inheritance is one of the core concepts of Object-Oriented Programming (OOP) in C++. It allows a class (known as the derived class) to inherit the properties and behaviors (attributes and methods) of another class (known as the base class). Inheritance helps in code reusability and establishes a relationship between classes, where the derived class is a specialized version of the base class.

What is Inheritance?

Inheritance allows a class to use the methods and properties of another class without needing to rewrite the same code. The derived class inherits data members and member functions of the base class, which allows the derived class to extend or modify the functionality of the base class.

Types of Inheritance

C++ supports several types of inheritance:

Syntax of Inheritance

The syntax for inheritance in C++ is as follows:

Syntax of Inheritance


        class BaseClass {
            // base class members
        };
        
        class DerivedClass : accessSpecifier BaseClass {
            // derived class members
        };
                    

1.Single Inheritance

Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.

When one class inherits another class, it is known as single level inheritance. Let's see the example of single level inheritance which inherits the fields only.

Code Example: Single Inheritance


        #include <iostream>
        using namespace std;
        
        // Base class
        class Animal {
        public:
            void eat() {
                cout << "This animal eats food." << endl;
            }
        };
        
        // Derived class
        class Dog : public Animal {
        public:
            void bark() {
                cout << "The dog barks." << endl;
            }
        };
        
        int main() {
            Dog d;
            d.eat();  // Inherited method
            d.bark(); // Derived class method
        
            return 0;
        }
                    

Output:

This animal eats food.
The dog barks.

2.Multiple Inheritance

Multiple inheritance occurs when a class inherits from more than one base class

When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.

Code Example: Multiple Inheritance


        #include <iostream>
        using namespace std;
        
        // Base class 1
        class Animal {
        public:
            void eat() {
                cout << "This animal eats food." << endl;
            }
        };
        
        // Base class 2
        class Bird {
        public:
            void fly() {
                cout << "This bird flies." << endl;
            }
        };
        
        // Derived class
        class Duck : public Animal, public Bird {
        public:
            void quack() {
                cout << "The duck quacks." << endl;
            }
        };
        
        int main() {
            Duck d;
            d.eat();    // Inherited from Animal class
            d.fly();    // Inherited from Bird class
            d.quack();  // Method of Duck class
        
            return 0;
        }
                    

Output:

This animal eats food.
This bird flies.
The duck quacks.

3.Multilevel Inheritance

In multilevel inheritance, a derived class becomes a base class for another derived class

Code Example: Multilevel Inheritance


        #include <iostream>
        using namespace std;
        
        // Base class
        class Animal {
        public:
            void eat() {
                cout << "This animal eats food." << endl;
            }
        };
        
        // Derived class
        class Mammal : public Animal {
        public:
            void walk() {
                cout << "This mammal walks." << endl;
            }
        };
        
        // Further derived class
        class Dog : public Mammal {
        public:
            void bark() {
                cout << "The dog barks." << endl;
            }
        };
        
        int main() {
            Dog d;
            d.eat();   // Inherited from Animal
            d.walk();  // Inherited from Mammal
            d.bark();  // Inherited from Dog
        
            return 0;
        }
                    

Output:

This animal eats food.
This mammal walks.
The dog barks.

4.Hybrid Inheritance

Hybrid inheritance is a combination of more than one type of inheritance.

Code Example: Hybrid Inheritance


            #include <iostream>
            using namespace std;
            
            // Base class 1
            class Vehicle {
            public:
                void showVehicle() {
                    cout << "This is a vehicle." << endl;
                }
            };
            
            // Base class 2
            class Engine {
            public:
                void showEngine() {
                    cout << "This vehicle has an engine." << endl;
                }
            };
            
            // Derived class 1
            class Car : public Vehicle {
            public:
                void showCar() {
                    cout << "This is a car." << endl;
                }
            };
            
            // Hybrid Derived class
            class SportsCar : public Car, public Engine {
            public:
                void showSportsCar() {
                    cout << "This is a sports car." << endl;
                }
            };
            
            int main() {
                SportsCar sc;
                sc.showVehicle();    // Inherited from Vehicle
                sc.showEngine();     // Inherited from Engine
                sc.showCar();        // Inherited from Car
                sc.showSportsCar();  // Specific to SportsCar
            
                return 0;
            }
                    

Output:

This is a vehicle.
This vehicle has an engine.
This is a car.
This is a sports car.

5.Hierarchical Inheritance

Hierarchical inheritance is defined as the process of deriving more than one class from a base class.

Code Example: Hierarchical Inheritance


        #include <iostream>
        using namespace std;
        
        // Base class
        class Animal {
        public:
            void eat() {
                cout << "This animal eats food." << endl;
            }
        };
        
        // Derived class 1
        class Bird : public Animal {
        public:
            void fly() {
                cout << "This bird can fly." << endl;
            }
        };
        
        // Derived class 2
        class Fish : public Animal {
        public:
            void swim() {
                cout << "This fish can swim." << endl;
            }
        };
        
        int main() {
            Bird b;
            Fish f;
        
            b.eat();  // Inherited from Animal
            b.fly();  // Specific to Bird
        
            f.eat();  // Inherited from Animal
            f.swim(); // Specific to Fish
        
            return 0;
        }
                

Output:

This animal eats food.
This bird can fly.
This animal eats food.
This fish can swim.

Ambiquity Resolution in Inheritance

Ambiguity can be occurred in using the multiple inheritance when a function with the same name occurs in more than one base class.


                #include   
                    using namespace std;  
                    class A  
                    {  
                        public:  
                        void display()  
                        {  
                            std::cout << "Class A" << std::endl;  
                        }  
                    };  
                    class B  
                    {  
                        public:  
                        void display()  
                        {  
                            std::cout << "Class B" << std::endl;  
                        }  
                    };  
                    class C : public A, public B  
                    {  
                        void view()  
                        {  
                            display();  
                        }  
                    };  
                    int main()  
                    {  
                        C c;  
                        c.display();  
                        return 0;  
                    }  
                

Output:

error: reference to 'display' is ambiguous display();

The above issue can be resolved by using the class resolution operator with the function. In the above example, the derived class code can be rewritten as:



                #include 
                    using namespace std;
                    
                    class A {
                    public:
                        void display() {
                            cout << "Class A" << endl;
                        }
                    };
                    
                    class B {
                    public:
                        void display() {
                            cout << "Class B" << endl;
                        }
                    };
                    
                    int main() {
                        A objA;
                        B objB;
                    
                        objA.display(); // Output: Class A
                        objB.display(); // Output: Class B
                    
                        return 0;
                    }
                                

Output:

Class A Class B

Access Specifiers in Inheritance

The access level of inherited members in the derived class depends on the access specifier used in the inheritance declaration. There are three access specifiers in C++:

Example: Access Specifiers

Here’s an example illustrating how access specifiers affect inheritance:

Code Example: Access Specifiers in Inheritance


        #include <iostream>
        using namespace std;
        
        // Base class
        class Base {
        public:
            int publicVar;
            
            Base() {
                publicVar = 10;
            }
        };
        
        // Derived class with private inheritance
        class Derived : private Base {
        public:
            void show() {
                cout << "Value of publicVar: " << publicVar << endl;
            }
        };
        
        int main() {
            Derived d;
            d.show();
        
            // This will cause an error because publicVar is private in Derived
            // cout << d.publicVar << endl;
        
            return 0;
        }
                    

Output:

Value of publicVar: 10

Pro Tip:

💡 Pro Tip

Inheritance is a powerful feature, but overuse of inheritance can lead to complex and difficult-to-maintain code. If your class needs to inherit from multiple classes, consider using composition (where one class contains objects of other classes) instead of inheritance. Always evaluate whether inheritance is the best solution for your problem or if other design patterns may be more appropriate.