Inheritance in C++

Inheritance in C++

Inheritance in C++ is basically the ability for a  class to be able to derive its properties from a different class. It is the most important feature of object oriented programming and allows –

  • High code re-use
  • Lesser time to code
  • Easier to maintain application and edit codes
  • Re-use pre-defined properties and data

Benefits of Inheritance

  1. Improves code usability – Write common properties in Base class and extend to sub classes example – All vehicles have tyres, so you can define noTyres in base class only once and for classes bike, car, truck inherit from vehicle class
    1. This helps in reduce cost for projects
    2. Saves time in coding
    3. Decreases complexity of the program
    4. Increases reliability
Inheritance in C++

Sub Class in Inheritance

Sub class inherits some of its properties from another class. Example – Bike, Car and Truck are sub classes as they are inheriting their properties from Vehicle class.

  • Sub Class is also popularly – known as
    • Child Class
    • Derived Class

Super Class in Inheritance

Super Class is the class which from which the sub class is inheriting from its properties from. Example Vehicle is the super class in our case as its inheriting its properties to Bike, Car and Truck

  • Super Class is also popularly – known as
    • Parent Class
    • Base Class

Different Modes of Inheritance in C++

There are three different ways in which we can define relationship between base and derived class while defining inheritance –

  1. Public mode:
    1. Public members of base class become public in derived class
    2. Protected members of base class become Protected in derived class
    3. Private members are inaccessible in derived class
  2. Protected mode:
    1. Public members of base class become protected in derived class
    2. Protected members of base class become Protected in derived class
    3. Private members are inaccessible in derived class
  3. Private mode:
    1. Public members of base class become private in derived class
    2. Protected members of base class become private in derived class
    3. Private members are inaccessible in derived class
Modes of Inheritance in C++

Syntax for Inheriting Declaration –

class NameOfDerivedClass : (Visibility mode) NameOfBaseClass{

// Data Members and functions
}

In terms of Parent and Child nomenclature –

class NameOfChildClass : (Visibility mode) NameOfParentClass{

// Data Members and functions
}

Example

The following example explains really well how inheritance really works and ways different members are accessible or not accessible –

class A {

public:
int x;

protected:
int y;

private:
int z;
};

class B : public A {
// x stays public
// y stays protected
// z is not accessible from B
};

class C : protected A {
// x becomes protected
// y stays protected
// z is not accessible from C
};

// 'private' is default for classes
class D : private A {
// x becomes private
// y becomes private
// z is not accessible from D
};

Types of Inheritance

There are the following types of inheritance in C++ –

  1. Single inheritance
  2. Multiple Inheritance
  3. Multilevel inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

Read more about these at – Types of Inheritance PrepInsta Page here.

Types of Inheritance Inheritance in C++

Please Login/Signup to comment