C++ Inheritance

Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP) and is supported in C++. It allows you to create new classes based on existing classes, inheriting their attributes and behaviors. This makes it easier to create and manage complex class hierarchies and promote code reuse.

The base class is referred to as the parent class, while the derived class is referred to as the child class. The child class inherits all the properties and methods of the parent class and can also add new properties and methods of its own. This means that you can reuse the code in the parent class and extend it or override it as needed.

For example, you can create a Person class that contains basic information about a person, such as their name and date of birth. Then, you can create a Student class that inherits from the Person class and adds additional information, such as the student's ID number and list of courses. The Student class can then reuse the properties and methods of the Person class, saving you time and effort, and making it easier to manage the data for each student.

Here's an example to demonstrate inheritance in C++.

#include <iostream>
using namespace std;

// Base class
class Shape {
   public:
      void setWidth(int width) {
         this->width = width;
      }
      void setHeight(int height) {
         this->height = height;
      }
   protected:
      int width;
      int height;
};

// Derived class
class Rectangle: public Shape {
   public:
      int getArea() { 
         return (width * height); 
      }
};

int main(void) {
   Rectangle rectangle;

   rectangle.setWidth(5);
   rectangle.setHeight(7);

   cout << "Total area: " << 
       rectangle.getArea() << endl;

   return 0;
}
Output
Total area: 35

In this example, the Shape class is the base class and the Rectangle class is the derived class. The Rectangle class inherits the setWidth and setHeight methods from the Shape class and adds a new method getArea to calculate the area of a rectangle.


Is-A Relationship Between Two Classes

In C++, inheritance creates an "is-a" relationship between two classes. This means that a derived class is a type of its base class, and can be used interchangeably with the base class in certain situations. For example, if you have a Rectangle class that is derived from a Shape class, you can say that a Rectangle is a Shape, and use a Rectangle object wherever a Shape object is expected.

This relationship is established through inheritance, which allows the derived class to inherit the properties and methods of the base class. This makes it easier to reuse code and create more flexible and manageable class hierarchies. The derived class can also override or extend the methods and properties of the base class, allowing it to provide a more specialized behavior.

Here are some real life examples of is-a relationships.
  • Car is a Vehicle.
  • Dog is a Mammal.
  • Earth is a Planet.

Here is an example C++ program that demonstrates the "is-a" relationship in inheritance with a Planet class as the base class and an Earth class as the derived class.

#include <iostream>
#include <string>

using namespace std;

class Planet {
public:
    Planet(string name) {
        this->name = name;
    }

    void rotate() {
        cout << name << " is rotating." << endl;
    }

    string name;
};

class Earth : public Planet {
public:
    Earth() : Planet("Earth") {
    }

    void supportLife() {
        cout << name << " supports life." << endl;
    }
};

int main() {
    Planet planet("Planet");
    planet.rotate();

    Earth earth;
    earth.rotate();
    earth.supportLife();

    // Demonstrating the "is-a" relationship in inheritance
    Planet *pPlanet = &earth;
    pPlanet->rotate();

    return 0;
}
Output
Planet is rotating.
Earth is rotating.
Earth supports life.
Earth is rotating.