Inheritance in C++ | Part 1

05 min read

An important characteristic of object-oriented programming paradigm is inheritance. An existing class whose functionality is inherited by some other class is called a base class. A base class can also be called as a parent class or a superclass. The class that inherits the functionality of other class is called a derived class. The derived class can also be called as a subclass or a child class. 

Example: consider that we need to represent the different types of vehicles. To define a family of four-wheeler vehicles, we may create a class called AutoMobile. In this class, we would define the functionality for a four-wheel vehicle that is common to all. Assume we have to also create classes for Car, which is also a kind of automobile, this class can now acquire the characteristics of AutoMobile class and only the additional characteristics meant for a car can be added to the Car class. Thus it avoids defining the Car class from the scratch. Here, AutoMobile is the superclass and Car is the subclass. Thus a subclass is one which inherits all of the members defined in the superclass and adds its own unique members.  

Uses of Inheritance:  Inheritance helps in code reusability. Reuse in turn Saves time, Saves effort. Saves money and Increases reliability

 

Forms of Inheritance: The derived class inherits some or all the features of the base class depending on the visibility mode and level of inheritance. Level of inheritance refers to the length of its (derived class) path from the root (top base class). Inheritance is classified as follows: 

1) Single Inheritance: A subclass inherits only from a single base class.



Here, class B inherits the features form class A.

2) Multilevel Inheritance: Derivation of a class from another derived class.



Here, class 'C' is inherited from class 'B' which in turn is inherited from class 'A'.

3) Multiple Inheritance: Derivation of a class from several (two or more) base classes.

class 'C' is inherited from class 'A' and class 'B'.

4) Hierarchical Inheritance: Derivation of a several classes from a single base class.



Class 'B', 'C' and 'D' inherits the properties of class 'A'.

5) Hybrid Inheritance: It is a combination of two or more inheritances

 

Syntax for inheriting a class from a superclass:

class Derivedclass : (Visibility mode) Baseclass
{
//members of derived class and they can access members of base class
);

The derivation of the DerivedClass from the BaseClass is indicated by the colon (:). Visibility mode specifies whether the features of the base class are inherited in public or private or protected mode. Specifying the VisibilityMode is optional. The default visibility mode is private.

 

Styles of Derivation:

1) 

class B : public A    //public derivation
{
Members of B
};

Inheritance of a base class with visibility mode 'public', by a derived class, causes public members of the base class to become public members of the derived class and the protected members of the base class become the protected members of the derived class. Member functions and objects of the derived class can treat these derived members as though they are defined in the derived class itself. The objects of a derived class can access public members of the base class that are inherited as public using the dot operator. However, protected members cannot be accessed with the dot operator. The private members of the base class are not inheritable. So, a derived class can access them through the inherited member functions of the base class.

2)

class B : protected A //protected derivation
{
Members of B
}; 

Inheritance of a base class with visibility mode protected, by a derived class, causes public members of the base class to become protected members of the derived class and the protected members of the base class become the protected members of the derived class. Member functions and objects of the derived class can treat these derived members as though they are defined in the derived class itself with the protected modifier. Thus objects of the derived class cannot access these members directly. The private members of the base class are not inheritable. So, a derived class can access them through the inherited member functions of the base class.

3)

class B : private A   //private derivation
{
Members of B
};

Inheritance of a base class with visibility mode private, by a derived class, causes public members of the base class to become private members of the derived class and the protected members of the base class become the private members of the derived class. Member functions and objects of the derived class can treat these derived members as though they are defined in the derived class itself with the private modifier. Thus objects of derived class cannot access these members directly. Subsequent derivation of the classes from a privately derived class cannot access any members of the grand-parent class. The private members of the base class are not inheritable. So, a derived class can access them through the inherited member functions of the base class.



POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error