January 13, 2021

C++ OOP – Inheritance

In the previous lesson we covered the Constructors. In this lesson, we would examine yet another OOP concept: Inheritance. The following topics will be addressed:

  1. Overview of C++ Inheritance
  2. Access Specifiers
  3. Inheritance Access Modes

 

1. Overview of C++ Inheritance

As mentioned, inheritance is a key feature of OOP. It is a feature that allows to create a new class based on an existing class. And clearly, this aligns with the DRY(don’t repeat yourself) principle.

In writing a new class, the new class can derive functionalities, such as data members from existing class. Therefore the new class is called a derived class while the existing class is called a base class. Other terms care parent class and child class. It could also be superclass and subclass.

Inheritance implements is-a relationship as shown in the figure.

Inheritance in C++

Let’s take an example:

// Base class
class Vehicle {
  public:
    string make = "Toyota";
    void honk() {
      cout << "Poom poom! \n" ;
    }
};

// Derived class of Vehicle
class Car: public Vehicle {
  public:
    string model = "Corolla";
};

int main() {
  Car car1;
  car1.honk();
  cout << car1.make + " " + car1.model;
  return 0;
}

Here, the Car class derives from the Vehicle class. Since car derives from Vehicle, the member of Vehicle are accessible to Car.

One thing you should observe is the use of the keyword public while inheriting Car from Vehicle.

class Car: public Vehicle {
   ...
};

The public keyword here is an access specifier and defines the access mode of the inheritance. We’ll now talk about access specifiers. Then we move on to access modes.

 

2. Access Specifiers

By now, you have seen from previous examples that members of a class can be public. They can also be private or protected. These are called access specifiers. They specify how members of a class can be accessed. For example, the public members can be accessed from outside the class. The three access modifiers for class members are explained below.

  • public – members are accessible from outside of the class
  • private – members are not visible from outside the class
  • protected – members are accessible from outside the class only by derived classes.

The program below illustrates this. There are 3 error I’ve indicated. Check to understand the reason behind each error

#include <iostream>

using namespace std;

// declare a class, shape
class  Shape {

  private:
       double height = 12.3;

  public:
       double width = 9.2;

  protected:
       double length = 34.02;

};

// create a derived class cube
class Circle: Shape {

public:
	void getValues(){

		height = 89.2; //3. Error: private member

		length = 23.3; //Ok: derived class
	}
};


int main() {

	Shape shape1;

	shape1.height = 9.3; //1. Error: private member

	shape1.length = 10.2; //2. Error: protected member

	shape1.width = 34.08; //OK: public member

    return 0;
}

 

3. Inheritance Access Mode(public, private, protected)

The access mode(also called types of inheritance) defines the ways classes can be derived. In order words, how much of the parents functionalities are inherited by the child. Let’s see.

  • public: if a child class is derived using the public mode, then the members of the base class are inherited exactly as they are
  • private:if a child class is derived with the private mode, then the members of the base class now become private members of the derived class.
  • protected:in this case, public members of the base class become protected members of the derived class.
  • default: when you don’t specify and access mode, then it’s automatically public.

In subsequent lessons, we’ll cover other aspects of inheritance including: multiple inheritance and function overloading.

2 thoughts on “C++ OOP – Inheritance

Leave a Reply

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