Inheritance in C++ programming

By  Falak Hasija    65 - 10 August, 20
inheritance-in-cpp-programming

Share article on social media


What really makes classes powerful?

The answer is Inheritance.

It is the ability to extend a class so that it creates a new class while at the same time retaining the basic characteristics of the original. 

Concept of the base class : 

class Polygons

{

protected:

double area;

double perimeter;

void printArea () const;

void printPeri () const;

};

 

Polygon class is a base class because it contains all of the common attributes and functions that form the basis for the derivation of other classes. 

Concept of the derived class:

class Triangle : public Polygons

{

private:

double sideA;

double sideB;

double sideC;

void calcArea();

void calcPeri();

};

class Square : public Polygons

{

private:

double side;

void caclArea();

void calcPeri();

};

 

Here, Triangle and Square classes are derived classes because they have access to the attributes and the functions in the base class while having their own attributes and functions and functions to process data.

Inheritance syntax:

To relate a new class to a base class and make it a derived class we use a colon (:) followed by inheritance type and name of the base class.

The inheritance type can be :

  • private
  • public
  • protected
class derivedClass: inheritance_type baseClass

 

INHERITANCE RULES:

  1. All data members of the base object are inherited.
  2. All function members of the base object are inherited, except:

                a. Constructors

                b. Destructors

                 c. Nonmember functions

                 d. Assignment operators

                 e. Virtual methods 

 

INHERITANCE TYPES:

Private, Protected, Public


Inheritance types Base access type Derived access type
private

private

protected

public

inherited but inaccessible

private

private

protected

private

protected

public

inherited but inaccessible

protected

protected

public

private

protected

public

inherited but inaccessible

protected

public

 

Private inheritance type:

When the inheritance is private, access to inherited data and methods is highly limited. The private data and methods in the base class are inaccessible in the derived class. 

class Triangle : private Polygons

{

...

}; //Triangle 

 

Protected inheritance type:

When the inheritance is protected, access to inherited data and methods is again limited. The private data and methods in the base class become inaccessible. Protected and public members become protected. 

class Triangle : protected Polygons

{

...

}; //Triangle

 

Public inheritance type: 

When inheritance type is public, access to inherited data and methods is less limited than the two previous situations. The data and methods that have private access in the base class are still inaccessible in the derived class. However, protected access remains protected and public access remains public.

class Triangle : public Polygons

{

...

}; //Triangle

 

Overriding inherited access specifiers:

There are situations in which we need to override the default access types that we inherit from a class.

Consider two classes :

class B

{

  protected:

         int datum; 

  public:

           void print();

          .......

}; //B

class D : private B

{

  protected:

        using B :: datum;

  

       

          .......

};  //D

Because of the inheritance rules, datum and print are inherited as private. If we want to inherit datum as protected, we can override it a using statement and the scope resolution operator as shown in the next example.

protected:

using B :: datum;

 

MANAGER FUNCTIONS UNDER INHERITANCE:

Constructors: 

Constructors in C++ are not inherited, C++ requires that a base class constructor be called, either implicitly or explicitly. This means that a derived class constructor must have a code to call the base class constructor first, and then whatever code is needed to initialize the derived class members.

Derived : : Derived (parameters) : BaseClass (parameters)

{

                ...

 

Copy constructors:

When a copy constructor is used for the derived class, the copy constructor for the base class is called.

Derived :: Derived ( const Derived& derivedObject ) : BaseClass (derivedObject)

{

              ....

 

Destructors:

While constructors must ensure that the base case is executed before the derived case, the opposite is true for destructors.

 

                                                                                    KEEP LEARNING!