Basics of Inheritance in C++


Inheritance is a fundamental concept in Object-Oriented Programming (OOP). It allows a class (called the derived or child class) to inherit properties and behaviors from another class (called the base or parent class). Inheritance promotes code reuse and establishes a relationship between classes.

Types of Inheritance in C++

  • Single Inheritance: A child class inherits from one parent class.
  • Multilevel Inheritance: A child class inherits from a parent class, and another class inherits from the child class.
  • Multiple Inheritance: A child class inherits from multiple parent classes.
  • Hierarchical Inheritance: Multiple child classes inherit from a single parent class.

1. Single Inheritance

In single inheritance, a single derived class inherits from a single base class.

Example:

    #include <iostream>

    class Animal {
    public:
        void eat() {
            std::cout << "Animal is eating." << std::endl;
        }
    };

    class Dog : public Animal {
    public:
        void bark() {
            std::cout << "Dog is barking." << std::endl;
        }
    };

    int main() {
        Dog dog;
        dog.eat(); // Inherited from Animal
        dog.bark();
        return 0;
    }
        

Output:

    Animal is eating.
    Dog is barking.
        

2. Multilevel Inheritance

In multilevel inheritance, a derived class inherits from another derived class, creating a chain of inheritance.

Example:

    #include <iostream>

    class Animal {
    public:
        void eat() {
            std::cout << "Animal is eating." << std::endl;
        }
    };

    class Mammal : public Animal {
    public:
        void walk() {
            std::cout << "Mammal is walking." << std::endl;
        }
    };

    class Dog : public Mammal {
    public:
        void bark() {
            std::cout << "Dog is barking." << std::endl;
        }
    };

    int main() {
        Dog dog;
        dog.eat();  // Inherited from Animal
        dog.walk(); // Inherited from Mammal
        dog.bark();
        return 0;
    }
        

Output:

    Animal is eating.
    Mammal is walking.
    Dog is barking.
        

3. Multiple Inheritance

In multiple inheritance, a single derived class inherits from two or more base classes.

Example:

    #include <iostream>

    class Engine {
    public:
        void startEngine() {
            std::cout << "Engine started." << std::endl;
        }
    };

    class Wheels {
    public:
        void rotateWheels() {
            std::cout << "Wheels are rotating." << std::endl;
        }
    };

    class Car : public Engine, public Wheels {
    public:
        void drive() {
            std::cout << "Car is driving." << std::endl;
        }
    };

    int main() {
        Car car;
        car.startEngine();  // Inherited from Engine
        car.rotateWheels(); // Inherited from Wheels
        car.drive();
        return 0;
    }
        

Output:

    Engine started.
    Wheels are rotating.
    Car is driving.
        

4. Hierarchical Inheritance

In hierarchical inheritance, multiple child classes inherit from a single base class.

Example:

    #include <iostream>

    class Animal {
    public:
        void eat() {
            std::cout << "Animal is eating." << std::endl;
        }
    };

    class Dog : public Animal {
    public:
        void bark() {
            std::cout << "Dog is barking." << std::endl;
        }
    };

    class Cat : public Animal {
    public:
        void meow() {
            std::cout << "Cat is meowing." << std::endl;
        }
    };

    int main() {
        Dog dog;
        Cat cat;

        dog.eat();  // Inherited from Animal
        dog.bark();

        cat.eat();  // Inherited from Animal
        cat.meow();

        return 0;
    }
        

Output:

    Animal is eating.
    Dog is barking.
    Animal is eating.
    Cat is meowing.
        

Conclusion

Inheritance in C++ allows the creation of a relationship between classes, enabling code reuse and logical structure. The types of inheritance provide flexibility in designing class hierarchies based on the requirements of the program.





Advertisement