C++ Inheritance

by anupmaurya
5 minutes read

Inheritance is one of the key features of Object-oriented programming in C++.Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This

Advantage of C++ Inheritance

Code re-usability: It also provides an opportunity to reuse the code functionality and fast implementation time. So, there is no need to define the member again and less code is required in the class.

Base and Derived Classes

A Derived class is defined as the class derived from the base class. A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes.

C++ Inheritance
C++ Inheritance

Syntax for derived class

class derived-class: access-specifier base-class

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

The derived class inherits the features from the base class and can have additional features of its own. For example,

#include <iostream>
 
using namespace std;

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

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

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

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

   return 0;
}

Here, the Rectangle class is derived from the Shape class. Since Rectangle is derived from Shape, members of Rectangle are accessible to Shape.

OUTPUT

Total area: 35

Is-a relationship

Inheritance is an is-a relationship. We use inheritance only if an is-a relationship is present between the two classes.

Here are some examples:

  • Apple is a fruit.
  • A surgeon is a doctor.
  • A bike is a vehicle.
  • A cat is an animal.

Access Control and Inheritance

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to – who can access them in the following way −

Accesspublicprotectedprivate
Same classyesyesyes
Derived classesyesyesno
Outside classesyesnono
Access Control and Inheritance

A derived class inherits all base class methods with the following exceptions −

  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.

Modes of Inheritance

We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied −

  1. Public Inheritance − When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class’s private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
  2. Protected Inheritance − When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
  3. Private Inheritance − When deriving from a private base class, public and protected members of the base class become private members of the derived class.

How to make a Private Member Inheritable

The private member is not inheritable. If we modify the visibility mode by making it public, but this takes away the advantage of data hiding.

C++ introduces a third visibility modifier, i.e., protected. The member which is declared as protected will be accessible to all the member functions within the class as well as the class immediately derived from it.

To know about different Types of Interitance.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.