Inheritance in C++

Inheritance is one of the crucial features of Object-oriented programming in C++. It consents user to make a new class  from an existing class.

The derived class take over all the features from the base class also can have additional features of its own.

Inherit Definition – Derive quality and characteristics from parents or ancestors. Like you inherit features of your parents.

Example: “She had inherited the prettiness of her mother”

Inheritance in Object Oriented Programming can be defined as a process of producing new classes from existing classes.

New classes inherit some of the properties besides behavior of the existing classes. An existing class that is “parent” of a new class is named a base class. New class that inherits properties of the base class is termed a derived class.

Inheritance is a method of code reuse. It also conveys possibility to extent existing classes by forming derived classes.

Syntax:

classDerivedClass : accessSpecifierBaseClass

here,Accessspecifier can be public, protected as well as private. bydefault access specifier is private. Access specifiers affect availability of data members of base class from the derived class. itregulates the ease of access of data members of base class outside the derived class.

Base & Derived Classes

A class can be plagiaristic from more than one classes.it can inherit data and functions from numerous base classes. To describe a derived class, we use a class derivation list to identify the base class(es). A class derivation list names one otherwise more base classes .it has the form:

class derived-class: access-specifier base-class

here,access-specifier is one of public, protected, or private, and base-class is the name of a earlier defined class. In case,If the access-specifier is not used, then it is private by default.

Example:

In the following example, a base class Shape and its derived class Rectangle.

Output:

Access modifiers

  Private

In private mode the public plus protected members of Base class convert private members of Derived class.

classDerivedClass : privateBaseClass

classDerivedClass : BaseClass   // By default inheritance is private

 Retrieving Base class members  public  protected  private
 From Base class  Yes  Yes  Yes
 From object of a Base class  Yes  No  No
 From Derived classes  Yes (As Private)  Yes (As Private)  No
 From object of a Derived class  No  No  No
 From Derived class of Derived Classes  No  No  No

Derived class of Derived Classes: If we are take over a derived class using a public inheritance as like this,

class B : private A

class C : private B

then public and protected members of class A will not be manageable in class C

  Public

This inheritance mode is used generally. Here, the protected member of Base class grow into protected members of Derived class and public converts public.

classDerivedClass : publicBaseClass

 Accessing Base class members  public  protected  private
 From Base class  Yes  Yes  Yes
 From object of a Base class  Yes  No  No
 From Derived classes  Yes (As Public)  Yes (As Protected)  No
 From object of a Derived class  Yes  No  No
 From Derived class of Derived Classes  Yes (As Public)  Yes (As Protected)  No

Derived class of Derived Classes: If we are inheriting a derived class using a public inheritance as shown below

class B : public A

class C : public B

then public and protected members of class A will be accessible in class C as public and protected respectively.

  Protected

In protected approach, the public and protected members of Base class turn out to be protected members of Derived class.

classDerivedClass : protectedBaseClass

 Accessing Base class members  public  protected  private
 From Base class  Yes  Yes  Yes
 From object of a Base class  Yes  No  No
 From Derived classes  Yes (As Protected)  Yes (As Protected)  No
 From object of a Derived class  No  No  No
 From Derived class of Derived Classes  Yes (As Protected)  Yes (As Protected)  No

Derived class of Derived Classes: If we are inheriting a derived class using a public inheritance as shown below

class B : protected A

class C : protected B

then public and protected members of class A will be accessible in class C as protected

 Difference between Private, Public and Protected

 Types of Inheritance

  Single

  • Single inheritance signifies a form of inheritance when there is only one base class and one derived class.

Example of Single Inheritance :

Here,Weneed to produce new class Student which should have the same information as Person class plus one new information about university. we can make a derived class Student:

  • Class Student is having access to all the data members of the base class (Person).
  • Meanwhile class Student does not have a constructor so you can produce a constructor as below

  • If you need to call the parameterized constructor of a base class from a derived class then consider the following example:

  • Here, Person(szName, iYear) signifies call of a constructor of the base classPerson. The passing of values to the constructor of a base class is completed via member initialization list.
  • Also,We can contact member functions of a base class from a derived class. example:

If you need to call the member function of the base class then you have to use the name of a base class

   Multiple

  • Multiple inheritance characterizes a kind of inheritance when a derived class gets properties ofmultiple  For example,

syntax:

Class DerivedClass: accessSpecifier BaseClass1, BaseClass2, …,BaseClassN

Example:

  • You can generate a new class that will get all the properties of all these classes:

 Multilevel

Multilevel inheritance characterizes a type of inheritance when a Derived class is a base class for one more class. the deriving a class from a derived class is the figure identified as multi-level inheritance.

Below Image shows the example of multilevel inheritance

here, Class Person is the base class of both Student plus Employee classes. At the same time, Class Student is the base class for ITStudent and MathStudent classes. Employee is the base class for Driver as well as Engineer classes.

Example:

 Hybrid

  Hierarchical

  • to generate multiple Derived classes that inherit properties of the same Base class is identified as Hierarchical inheritance

Hybrid Inheritance

The Grouping of Multi-level and Hierarchical inheritance will give you Hybrid inheritance,as like this:

Diamond Problem

When you have a hybrid inheritance then a Diamond problem may arise. Here, a Derived class will have many paths to a Base class. This will end in duplicate inherited members of the Base class. This problem is recognized as Diamond problem

 

Leave A Comment

Your email address will not be published. Required fields are marked *