C++ Inheritance

C++ Inheritance mechanism is used to build new classes from existing classes. The existing class is called the base class and the newly created class is called the derived class.

Inheritance basically defines an IS-A relationship between a base class and its derived classes. This means that an object of a derived class can be used wherever an object of the base class can be used.  

The derived class inherits members of the base class and hence promotes code reusability. However, the derived class itself can add its own new properties and behaviors . 

A derived class must use the colon symbol  :  to derive from a base class which must be written in the header of the derived class definition. See the syntax below.

Syntax for creating derived class from base class:

class derived_class : access_specifier base_class {
  // members
};

Here access_specifier denotes any one of public, protected, or private, and base_class is the name of an existing class. If the access_specifier is not used, then it is private by default.

We will learn about the differences among using private, public and protected later in this tutorial.

 

Inheritance Example

The following example shows that Area is the base class and Volume is the derived class. By rule, the Volume class can inherit the non-private members of the Area class. See the program below.

inherit.cpp

// Example code to demonstrate inheritance
#include <iostream>
using namespace std;

// Base class
class Area {

protected:
// data members
int length;
int breadth;

public:
// base class constructor
Area() {
cout << "\nbase class constructor" << endl;
}

// base member functions
void inputArea(int l, int b) {
length = l;
breadth = b;
}
int computeArea() {
return length * breadth;
}
};

// Derived class
class Volume : public Area {

private:
// data member
int height;

public:
// derived class constructor
Volume() {
cout << "derived class constructor" << endl;
}

// derived member functions
void inputVolume(int l, int b, int h) {
inputArea(l, b); // inherited
height = h;
}
int computeVolume() {
return length * breadth * height;
}
};

int main() {
// creating object of Area class
Area ob1;

// calling base class member function
ob1.inputArea(20, 15);

// computing area for ob1
cout << "AREA1 : " << ob1.computeArea() << endl;

// creating object of Volume class
Volume ob2;

// calling derived class member function
ob2.inputVolume(30, 20, 10);

// computing area for ob2
cout << "AREA2 : " << ob2.computeArea() << endl;

// computing volume for ob2
cout << "VOLUME : " << ob2.computeVolume() << endl;

   return 0;
}

Output:


base class constructor
AREA1 : 300

base class constructor
derived class constructor
AREA2 : 600
VOLUME : 6000

 

 

IS-A Relationship

IS-A is a way of specifying: this object is a type of that object. Let us see how to achieve inheritance.

class Animal { }

class Mammal : public Animal { }

class Lion : public Mammal { }

Now, based on the above example, in OOP terms, the following are true −

  • Animal is the base class of Mammal class.
  • Mammal is a derived class of Animal class.
  • Lion is derived class of both Mammal and Animal classes.

Now, if we consider the IS-A relationship, we can say −

  • Mammal IS-A Animal
  • Lion IS-A Mammal
  • Hence Lion IS-A Animal as well

The derived classes will be able to inherit all the properties of the base class except for its private properties.

 

 

Types of Inheritance

There are five types of inheritance in C++:

  • Single inheritance Class B derived from class A only.
  • Multilevel inheritance Class B derived from class A; then class C derived from class B.
  • Hierarchical inheritance Class A acts as the base class for classes B, C, and D.
  • Multiple inheritance Class C derives from classes A and B.
  • Hybrid inheritance Mix of two or more types of inheritance.

 

Access Specifier and Inheritance

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

The various ways we can derive classes are known as access modes. These access modes have the following effect:

  1. public: If a derived class is declared in public mode, then the members of the base class are inherited by the derived class just as they are.
  2. private: In this case, all the members of the base class become private members in the derived class.
  3. protected: The public members of the base class become protected members in the derived class.

Thus. we can summarize the different access modes based on who can access them according to the following rules −

Accesspublicprotectedprivate
Same classyesyesyes
Derived classesyesyesno
Outside classesyesnono

 

A derived class inherits all member functions of its base class with the following exceptions −

  • The constructors, destructors and copy constructors of its base class.
  • The overloaded operators of its base class.
  • The friend functions of its base class.

NOTE: Inheritance provides an opportunity to reuse the code functionality and fast development time.