c-plus-plus
  1. c-plus-plus-inheritance

C++ Inheritance

Inheritance is one of the most important features of Object-Oriented Programming (OOP) in C++. Inheritance enables a class to reuse properties and behavior of an existing class, known as the base class or parent class, to create a new class, known as the child class or derived class.

Syntax

class derived_class_name : access_specifier base_class_name {
    // body of derived class
};
  • derived_class_name: Name of the derived class
  • access_specifier: Access specifiers define how the derived class can access the members of the base class. The access specifiers can be public, protected or private.
  • base_class_name: Name of the base class

Example

#include <iostream>
using namespace std;

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

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

int main() {
   Rectangle rect;
   rect.setWidth(5);
   rect.setHeight(7);

   // Print the area of the object.
   cout << "Total area: " << rect.getArea() << endl;

   return 0;
}

Output

Total area: 35

Explanation

In the above example, Shape class is the base class and Rectangle class is the derived class. The Rectangle class inherits the members of the Shape class using the public access specifier. The setWidth and setHeight member functions of the Shape class can be accessed by the Rectangle class. The getArea member function of the Rectangle class calculates and returns the area of the rectangle.

Use

Inheritance allows us to create new classes that are built upon existing classes. It provides a mechanism for reusing code and reducing complexity in software development. Inheritance makes the code more manageable, easier to understand and modify.

Important Points

  • A derived class can access the members of the base class that are public and protected.
  • A derived class cannot access the private members of the base class.
  • When a derived class is created, the constructor of the base class is called first and then the constructor of the derived class is called.
  • When a derived class is destroyed, the destructor of the derived class is called first and then the destructor of the base class is called.

Summary

  • Inheritance is a mechanism in C++ that allows a class to inherit properties and behavior of an existing class.
  • The derived class is created using the class keyword followed by the name of the derived class and an access specifier.
  • The base class is specified using the : operator followed by the name of the base class and its access specifier.
  • A derived class can access the public and protected members of the base class.
  • Inheritance makes the code more manageable, easier to understand and modify.
Published on: