C++ Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) and is a mechanism that allows one class to inherit the properties and behaviors of another class. In C++, inheritance is implemented using the class keyword, and it allows a class to inherit the properties and behaviors of one or more base classes. This allows for code reuse and the ability to create a hierarchy of classes that can be used to model real-world scenarios.

Types of Inheritance

There are several types of inheritance that can be implemented in C++, each with their own unique characteristics and use cases. These include:

  • Single Inheritance: This is the simplest form of inheritance and is used when a class inherits from only one base class. The derived class inherits all the public and protected members of the base class, but not the private members.
  • Multiple Inheritance: This type of inheritance is used when a class inherits from multiple base classes. It can lead to ambiguity, so it should be used with caution.
  • Multilevel Inheritance: This type of inheritance is used when a class inherits from a class that already inherits from another class. It creates a hierarchy of classes, with the base class at the top and the derived classes at the bottom.
  • Hierarchical Inheritance: This type of inheritance is used when multiple classes inherit from a single base class. It creates a hierarchy of classes, with the base class at the top and the derived classes at the bottom.
  • Hybrid Inheritance: This type of inheritance is used when a combination of the above types of inheritance is used. It allows for the creation of complex class hierarchies that can be used to model real-world scenarios.

Single Inheritance

Single inheritance is the simplest form of inheritance and is used when a class inherits from only one base class. The derived class inherits all the public and protected members of the base class, but not the private members. The following is an example of single inheritance:

class Base {
public:
    int x;
    void display() {
        cout << "x = " << x << endl;
    }
};

class Derived : public Base {
public:
    int y;
    void display() {
        cout << "x = " << x << ", y = " << y << endl;
    }
};

In this example, the Base class has a public variable x and a public function display(). The Derived class inherits from the Base class using the public keyword. As a result, it inherits the public variable x and the public function display(). The Derived class also has a public variable y and a public function display() that overrides the display() function of the base class. This is known as function overriding, and it allows the derived class to provide its own implementation for a function that it inherits from the base class.

It’s also worth noting that in C++, a derived class can access the private members of its base class using public or protected member functions of the base class. This is known as friendship and it allows for a limited amount of access to the private members of a base class.

Multiple Inheritance

Multiple inheritance is a type of inheritance where a class inherits from multiple base classes. It can lead to ambiguity, so it should be used with caution. Ambiguity arises when two or more base classes have a member with the same name and the derived class tries to access that member using its name alone. To avoid this, C++ provides a way to specify which base class’s member to use by using the scope resolution operator (::). The following is an example of multiple inheritance:

class Base1 {
public:
    int x;
};
class Base2 {
public:
int x;
};

class Derived : public Base1, public Base2 {
public:
void display() {
cout << "x from Base1 = " << Base1::x << endl;
cout << "x from Base2 = " << Base2::x << endl;
}
};

In this example, the Base1 class has a public variable x, and the Base2 class also has a public variable x. The Derived class inherits from both the Base1 and Base2 classes using the public keyword. In the display() function of the Derived class, we use the scope resolution operator to specify which base class’s x variable to access. This eliminates the ambiguity and allows us to access the correct variable.

Multilevel Inheritance

Multilevel Inheritance is a type of inheritance where a class inherits from a class that already inherits from another class. It creates a hierarchy of classes, with the base class at the top and the derived classes at the bottom. The following is an example of multilevel inheritance:

class Base {
public:
    int x;
    void display() {
        cout << "x = " << x << endl;
    }
};
class Derived1 : public Base {
public:
int y;
void display() {
cout << "x = " << x << ", y = " << y << endl;
}
};

class Derived2 : public Derived1 {
public:
int z;
void display() {
cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}
};

In this example, the Base class has a public variable x and a public function display(). The Derived1 class inherits from the Base class and adds a public variable y and a new implementation of the display() function. The Derived2 class then inherits from the Derived1 class and adds a public variable z and its own implementation of the display() function. This creates a hierarchy of classes, with Base at the top, Derived1 in the middle, and Derived2 at the bottom. The derived classes can access the members of their base classes, and in this case, the Derived2 class can access the members of both the Base and Derived1 classes. This is a example of multilevel inheritance.

Hierarchical Inheritance

In C++, hierarchical inheritance is a type of inheritance where one class can be inherited by multiple subclasses. In other words, one base class is inherited by multiple derived classes. Here is an example of hierarchical inheritance in C++:

class Shape {
   public:
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }
   protected:
      int width;
      int height;
};

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

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

int main(void) {
   Rectangle Rect;
   Triangle  Tri;

   Rect.setWidth(5);
   Rect.setHeight(7);
   cout << "Total area: " << Rect.getArea() << endl;

   Tri.setWidth(5);
   Tri.setHeight(7);
   cout << "Total area: " << Tri.getArea() << endl;

   return 0;
}

In this example, the Shape class is the base class, and the Rectangle and Triangle classes are the derived classes. The Rectangle and Triangle classes inherit the data members and member functions of the Shape class.

Hybrid Inheritance

Hybrid Inheritance is a combination of two or more types of inheritance. It can be achieved by combining any two types of inheritance (Single, Multiple, Multi-level, Hierarchical, and Hybrid).

Here is an example of Hybrid Inheritance in C++:

class Shape {
   public:
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }
   protected:
      int width;
      int height;
};

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

class Square: public Rectangle {
   public:
      void setWidth(int w) {
         width = w;
         height = w;
      }
      void setHeight(int h) {
         width = h;
         height = h;
      }
};

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

class Pyramid: public Square, public Triangle {
   public:
      int getVolume() {
         return (width * height * (width/3))/2;
      }
};

int main() {
   Pyramid Py;
   Py.setWidth(5);
   Py.setHeight(7);

   cout << "Total area of Pyramid: " << Py.getArea() << endl;
   cout << "Total volume of Pyramid: " << Py.getVolume() << endl;

   return 0;
}

In this example, the Shape class is the base class, and the Rectangle, Square, Triangle and Pyramid classes are the derived classes. The Rectangle and Triangle classes inherit the data members and member functions of the Shape class directly, while the Square and Pyramid classes inherit them indirectly through the Rectangle class. The Square class overrides the setWidth and setHeight methods inherited from the Shape class, while the Pyramid class inherits the getArea method from both the Square and Triangle classes and has its own method, getVolume, to calculate the volume of the pyramid. This example demonstrates how hybrid inheritance can be achieved by combining multiple inheritance and multi-level inheritance.

Leave a Reply

Your email address will not be published. Required fields are marked *