Inheritance in C++

Inheritance in C++Kateryna BondarenkoBlockedUnblockFollowFollowingMar 22DefinitionInheritance is one of the fundamental OOP principles.

According to it, class can use variables and methods of another class as its own.

Class that inherits data is called subclass, derived class, or child.

Class from which data or methods are inherited is called super class, base class, or parent.

Terms “parent” and “child” are extremely helpful in understanding of inheritance.

Just as child gets characteristics of his or her parents, derived class gets methods and variables from base class.

Inheritance might be highly useful because it allows to reuse previously written code, which in turn can significantly speed up the development process.

Despite this, inheritance should be used with caution, since inheritance creates the closest bond possible between classes.

Most changes in the super class will affect all children classes, which should be managed carefully because can lead to unexpected behavior.

Basic exampleFunction turn_on() and variable serial_number were not declared or defined in subclass Computer.

However, they can be used since they are inherited from base class.

Important note: private variables and methods can not be inherited.

3 types of inheritanceC++ supports several types of inheritance:public — pubic and protected is inherited “as is”;protected — all inherited data becomes protected;private — all inherited data is private.

Variables and methods become private if inherited as private.

Data access level does not changes for base class Device, but since derived class Computer inherits data as private, it becomes private for Computer class.

Class Computer now uses function turn_on() as it would use any private function.

For Computer, turn_on() can be called inside the class, but attempt to call it directly from main will cause compile time error.

For basic class Device, turn_on() is public and can be called by main.

Constructors and DestructorsIn C++, constructors and destructors are not inherited.

However, they are called when child class initializes its instance.

Constructors are called one by one hierarchically, starting from base class, and ending with the last derived class.

Destructors are called in reverse order.

Constructors: Device -> Computer -> Laptop.

Destructors: Laptop -> Computer -> Device.

Multiple inheritanceMultiple inheritance occurs when subclass has two or more super classes.

Here, class Laptop inherits both Monitor and Computer at the same time.

Diamond problemDiamond problem is a classical problem in languages that support multiple inheritance feature.

It occurs when classes B and C inherit from A, and class D inherits from both B and C.

For example, class A, B, and C have a method print_letter().

If print_letter() will be called by D, it is unclear which method to use — the one from A, B, or C classes.

Different languages approach to diamond problem differently.

In C++, solving the problem is left to the programmer.

Diamond problem is a design problem and should be settled at a design stage.

However, if not addressed on time, it can still be resolved at a development stage by:Calling method of specific super class;Treating subclass instance as an instance of specific super class;Overriding problematic method in the last child class ( in code example, turn_on() in subclass Laptop ).

Calling my_laptop.

turn_on(), if it is not defined in Laptop,will result in compile time error.

Object Laptop can access two definitions of turn_on() function simultaneously, Device:Computer:Laptop.

turn_on() and Device:Monitor:Laptop.


Without redefining turn_on(), call to the function is ambiguous and can not be resolved by compiler itself.

ConclusionInheritance is useful, however, should be designed carefully to avoid ambiguousness and related problems.

In terms of inheritance, C++ provides a broad set of tools which opens a variety of opportunities to a programmer.

.. More details

Leave a Reply