08 - C++ Inheritance

In this section of the tutorial, you will learn the following:

  • Inheritance
  • Types of Inheritance
  • Types of Access Specifiers

8.1 C++ Inheritance

Inheritance allows you to create a new specialized class from an existing class. The new class, which inherits the features, is called the derived class/sub-class/child class and the class whose features are inherited is called the base/ super class/ parent class.

The properties and functions of base class are available in the derived class. The derived class can further add properties and features of its own. Inheritance helps code reusability; you do not have to write the same code from scratch. 

Class B: public A
{
    private:

    public:
};

 In the above example, class B is the derived from class A. Thus, class B is derived/sub-class/child class and A is the base/super/parent class.

Note: The derived class does not inherit the private members of the base class. These members are directly accessible from within the class definition. 

Sometimes you may want the derived class to be able to access certain private members of the base class. However, private members of class are only accessible from within the class definition.

The access specifier that helps solve the above problem is as follows:

8.2  Protected Access Specifier

You can inherit the protected members of a base class. These members are accessible from the base class and from the derived class, but not outside of these two classes.

8.2.1  Inheritance Example

Consider that a father wants everyone to know his last name, his middle name only to his children and his salary to none at all. The lastname is public and salary is private. However, in this case, you want only the derived classes to know (inherit) the middlename. Hence, you declare it as protected in the class definition as follows:

class father
{
    private:
        int salary;
    protected:
        string middlename;
    public:
        string firstname; 
        string  lastname;
};
class daughter: public class father  
{
    #include<iostream.h>    
    public:
        string lastname;
        void display()
        cout<<middlename<<lastname<<endl;
};

In the above example, you are creating a new class daughter from an existing class Father. Thus, daughter is the derived class and father is the base class.

The daughter class inherits the middle name and the lastname of her father (protected and public variables of the base class) but not the salary (private variable of the base class). In addition, the daughter class has a function named displays.

In essence, public members of a class are accessible from within that class, its derived class and from outside the class. Private members are directly accessible only from within the class definition. The derived class does not inherit the private members of the base class. They are not visible to the derived class. Protected members of a class are accessible from within that class and the derived class. However, they are not accessible from outside of these two classes.

8.2.2    Types of Inheritance

The type of inheritance determines the behavior of the members of the base in the derived class. The ways in which you can inherit from a base class are as follows:

  • Private Inheritance: In private inheritance, public and protected members of the base class become private members of the derived class. This is rarely used.
  • Protected Inheritance: The protected and public members of the base class become protected members of the derived class.
  • Public Inheritance: The protected and public members are inherited as is.

Like us on Facebook