C++ inheritance


Introduction

Inheritance is one of the core concepts of Object-Oriented Programming (OOP), and it is extensively used in C++. It allows a class (called the derived class) to inherit properties and behaviors (attributes and methods) from another class (called the base class or parent class). Inheritance provides a way to reuse code, increase modularity, and improve maintainability in a program.

C++ supports different types of inheritance, such as single inheritance, multiple inheritance, multilevel inheritance, and hierarchical inheritance.


1. What is Inheritance in C++?

In C++, inheritance allows a class to derive properties and behaviors from another class. This means that the derived class inherits all the members (attributes and methods) of the base class, but it can also have additional features or override the base class methods.

Key Points:

  • Base class (or Parent class) is the class whose properties are inherited.
  • Derived class (or Child class) is the class that inherits the properties from the base class.
  • The derived class can extend or modify the base class’s functionality.

2. Syntax of Inheritance

To create a derived class, you use the following syntax:

class DerivedClass : accessSpecifier BaseClass {
    // Additional members and methods of the derived class
};

The access specifier can be:

  • public: Members of the base class are accessible as public members in the derived class.
  • protected: Members of the base class are accessible as protected members in the derived class.
  • private: Members of the base class are accessible only within the derived class.

3. Types of Inheritance in C++

C++ supports several types of inheritance. Let's explore each type:

1. Single Inheritance

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

Example:

#include <iostream>
using namespace std;

// Base class
class Animal {
public:
    void speak() {
        cout << "Animal speaks!" << endl;
    }
};

// Derived class
class Dog : public Animal {
public:
    void bark() {
        cout << "Dog barks!" << endl;
    }
};

int main() {
    Dog dog;
    dog.speak();  // Inherited from Animal class
    dog.bark();   // Defined in Dog class

    return 0;
}

Explanation:

  • The Dog class is derived from the Animal class.
  • The Dog class inherits the speak() function from the Animal class.
  • The bark() function is specific to the Dog class.

2. Multilevel Inheritance

In multilevel inheritance, a class is derived from another class, and that derived class is further inherited by another class.

Example:

#include <iostream>
using namespace std;

// Base class
class Animal {
public:
    void speak() {
        cout << "Animal speaks!" << endl;
    }
};

// Derived class from Animal
class Mammal : public Animal {
public:
    void walk() {
        cout << "Mammal walks!" << endl;
    }
};

// Further derived class from Mammal
class Dog : public Mammal {
public:
    void bark() {
        cout << "Dog barks!" << endl;
    }
};

int main() {
    Dog dog;
    dog.speak();  // Inherited from Animal
    dog.walk();   // Inherited from Mammal
    dog.bark();   // Defined in Dog

    return 0;
}

Explanation:

  • Dog inherits from Mammal, and Mammal inherits from Animal.
  • The Dog class has access to all the functions from both Mammal and Animal.

3. Multiple Inheritance

In multiple inheritance, a derived class can inherit from more than one base class.

Example:

#include <iostream>
using namespace std;

// Base class 1
class Animal {
public:
    void speak() {
        cout << "Animal speaks!" << endl;
    }
};

// Base class 2
class Vehicle {
public:
    void drive() {
        cout << "Vehicle drives!" << endl;
    }
};

// Derived class inheriting from both Animal and Vehicle
class Car : public Animal, public Vehicle {
public:
    void honk() {
        cout << "Car honks!" << endl;
    }
};

int main() {
    Car car;
    car.speak();  // Inherited from Animal
    car.drive();  // Inherited from Vehicle
    car.honk();   // Defined in Car

    return 0;
}

Explanation:

  • The Car class inherits from both Animal and Vehicle.
  • This allows Car to have methods from both the Animal and Vehicle classes.

4. Hierarchical Inheritance

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

Example:

#include <iostream>
using namespace std;

// Base class
class Animal {
public:
    void speak() {
        cout << "Animal speaks!" << endl;
    }
};

// Derived class 1
class Dog : public Animal {
public:
    void bark() {
        cout << "Dog barks!" << endl;
    }
};

// Derived class 2
class Cat : public Animal {
public:
    void meow() {
        cout << "Cat meows!" << endl;
    }
};

int main() {
    Dog dog;
    dog.speak();  // Inherited from Animal
    dog.bark();   // Defined in Dog

    Cat cat;
    cat.speak();  // Inherited from Animal
    cat.meow();   // Defined in Cat

    return 0;
}

Explanation:

  • Both Dog and Cat inherit from the same base class Animal.
  • Both derived classes have access to the speak() function from Animal.

4. Access Specifiers in Inheritance

In C++, when a derived class inherits from a base class, the members of the base class can be accessed depending on the access specifier used:

1. Public Inheritance

  • Members of the base class that are public in the base class become public in the derived class.
  • Members of the base class that are protected in the base class remain protected in the derived class.
  • Members of the base class that are private in the base class are not accessible by the derived class.

2. Protected Inheritance

  • Members of the base class that are public in the base class become protected in the derived class.
  • Members of the base class that are protected in the base class remain protected in the derived class.
  • Members of the base class that are private in the base class are not accessible by the derived class.

3. Private Inheritance

  • Members of the base class that are public in the base class become private in the derived class.
  • Members of the base class that are protected in the base class become private in the derived class.
  • Members of the base class that are private in the base class are not accessible by the derived class.

5. Constructor and Destructor in Inheritance

In inheritance, the constructor and destructor of the base class are invoked when a derived class object is created or destroyed.

  • Constructor: When a derived class object is created, the constructor of the base class is called first (unless explicitly mentioned otherwise). If the base class has a constructor that takes parameters, it must be invoked using an initializer list.
  • Destructor: When a derived class object is destroyed, the destructor of the base class is called automatically.

Example: Constructor and Destructor in Inheritance

#include <iostream>
using namespace std;

class Animal {
public:
    Animal() {
        cout << "Animal constructor called!" << endl;
    }

    ~Animal() {
        cout << "Animal destructor called!" << endl;
    }
};

class Dog : public Animal {
public:
    Dog() {
        cout << "Dog constructor called!" << endl;
    }

    ~Dog() {
        cout << "Dog destructor called!" << endl;
    }
};

int main() {
    Dog dog;
    return 0;
}

Output:

Animal constructor called!
Dog constructor called!
Dog destructor called!
Animal destructor called!

Explanation:

  • The constructor of the base class (Animal) is called first, followed by the constructor of the derived class (Dog).
  • When the object goes out of scope, the destructors are called in reverse order.