Inheritance In C++ & Different Types Of Inheritance And Their uses

inheritance in c++

Description:

inheritance in c++ – this is a very detail tutorial about inheritance and we will also discuss different types of inheritance in this tutorial.

So let’s get started!

Inheritance in c++

The second most important aspect of object-oriented programming is inheritance. The code of existing classes is used in inheritance for making new ones that saves time for a new class to write and debug the entire code. Inheriting means getting a new class is written in inheritance that it can access or use the members of an existing class. The new one that can reach existing class members is called the derived class, or child class.

The existing class is referred to as base class or parent class.The derived class can take advantageof the base class data members and member functions. It may also have members and member functions of its own data type. Consequently a derived class can be even larger than a base class.

inheritance in c++

A relation between the derived class and the base class is shown in the figure above. The arrow head points to the base classes A and B. The arrow head means that the derived class will access all the members of the base class. But the base classes cannot access the derived class data/members. The derived class has only one individual member,

inheritance in C++



In the figure shown above, the derived class has only one member of its own. The two members are shown enclosed in the green rectangle are the members of the base class. The derived class can only access these two members of the base class. Thus, whereas an object of the base class can access only two members, an object of the derived class can access three members.

A new class can be derived from one or more existing classes. Based upon the number of base classes from which a class is derived, the inheritance is divided into two categories.

  • Single Inheritance in c++
  • Multiple Inheritance c++

Single Inheritance  in c++

In single inheritance, the new class is derived from only one base class.

Multiple Inheritance in c++

In multiple Inheritance, the new class is derived from more than one base classes.

Protected Access Specifier 

The public members of a class are accessible by all functions in the program and the private members of a class are accessible only by member functions and friend functions of that class. Similarly, the protected members of a class are accessible by the member functions and friend functions of that class.

The protected members of a base class are, however, accessible by members of its derived classes but the private members of the base class are not accessible directly by members of its derived classes. This is the main difference between the protected and the private access specifiers.

The protected members of a base class fall between private and public member. These members are public for the derived class but for the rest of program, these are treated as private.

Defining Derived Classes

The syntax used to describe a derived class varies significantly from the base class definition syntax. The declaration of a derived class also includes the name of the base class it derived from.

The following is the general syntax for defining a derived class is:

 


The student class has two data members and two member functions Assume the marks received by a student in three different subjects are to be included as new data members in the above class and the cumulative marks of these subjects. This is done by introducing new class members. Those new members can be added to the class in two ways.

These are:

Add new members in the original class.

or

Create a new class that includes the new members and then incorporates them in the current student’s class. The concept of inheritance is to use the members of an existing class. The new class is the derived class. The original class serves as the base class for the derived class.

Deriving a new class from the existing class reduces the size of program. it also eliminates duplication of code within the program.

For example, let the name of the new class be marks. This class uses the members of the existing student class.

The class marks is derived from the class student. The class marks is the derived class. The class student is the base class.

The derived class marks has four data members of integer type and two member functions. It also uses the code of the base class student.

The derived class cannot directly access the private data members of the base class student by using the dot operator. These members are only accessible to the derived class through the interface functions within the base class. The program given below explains the above example.


Derived Classes example:

Example: write a program in which take the student marks and sum them and show the total marks on-screen using inheritance in c++

Programming explanation:

The class “marks” is defined as a derived class. The keyword “public” and the name of the base class “student” followed by colon(:) are written while defining the derived class. This shows that the objects of the derived class are able to access public members of the base class. It is called Public Inheritance.

The derived class “marks” can access the “input” & “show” member functions of the base class. It cannot access other private members of the base class.

An object “m” of class “marks” is created. The member function input of the class “student” is called through “m” object of class marks”. Similarly, the “show” function is also called in the “show_detail” member function of the class “marks” since the derived class “marks” has been declared as public of the student” class. The objects of the “marks” class can access only the public members of base class student.



 Types of Inheritances in c++

There are three kinds of inheritance in c++.

  • Public inheritance in c++.
  • Private inheritance in c++.
  • Protected inheritance in c++.

Public Inheritance in c++

In Public Inheritance, the public members of the base class become the public members of the derived class. thus the base class member(both data and function ) is publicly accessible for the object of the derived class. Similarly, the protected data members of the base class also become the protected members of the derived class.

The general syntax for deriving a public class from a base class is:

class sub_class_name  : public base_class_name

{

Expression;

},

where

public—————- specifies the public inheritance

sub_class_name—-represents the name of the derived class

base_class_name—represents the name of the base class

Example: write a program which explains the concept of public inheritance in c++:

Programming explanation:

In the above program, the class B is derived as private from the base case A The objects of the class B:

  • cannot access the private data members al & a2 of the base class A.
  • can access the public function member ppp of the base class A.
  • can access the protected data members pal & pa2 of  the base class A.


Private Inheritance in c++

 In Private Inheritance, the objects of the derived class cannot access the public members of the base class. Its objects can only access the protected data members of the base class.

 The general syntax for deriving a private class from a base class is:

class sub class_name : private base_class_name.

{

Expressions;

}

 where

private —————specifies private inheritance

sub_class_name —-represents the name of the derived class

 base class_name—-represents the name of the base class

Example: write a program which explains the concept of private inheritance in c++:

Programming explanation:

In the above program, class B is derived as private for the base class A. The objects of class B:

  • cannot access the private data members al & a2 of base class A.
  • cannot access the public function member ppp of basc class A.
  • can only access the protected data members pal & pa2 of base class A.


Protected Inheritance in c++

The object of the class that is derived as protected can access only the protected member of the base class.

The general syntax for deriving a protected class from basc class is:

class sub_class_name : protected base_class_name

{

Expressions;

};

where

protected—————–specifics protected inheritance

sub_class_name——–represents the name of the derived class

base_class_name——-represents the name of the base class

Example: write a program which explains the concept of protected inheritance in c++:

Programming explanation:

In the above program, the class B is derived as protected from the base class A. The objects of class B:

  • can only access the protected data members pal & pa2 of base class A



Derived Class Constructors

In inheritance, a constructor of the derived class as well the constructor functions of the base class are automatically executed when an object of the derived class is created.

The following example explains the concept of execution of constructor functions in single inheritance.

Simple program example of constructor functions in single inheritance in c++

In the above program, when an object of the derived class created, the constructors of both the derived class as well as the base class are executed.

Constructors in Single Inheritance in c++ with Arguments

 In the case of constructor functions with arguments, the syntax to define constructor of the derived class is different. To execute a constructor of the base class that has arguments through the derived class constructor, the derived class constructor is defined as:

  • write the name of the constructor function of the derived class with parameter.
  • place a colon immediately after this and then write the name of the constructor function of the base class with parameters.

Example: how to write a constructor function with arguments in single inheritance in c++:


Multiple Inheritance in c++

 In multiple inheritance in c++, a class is derived by using more than one classes, In multiple inheritance, the derived class receives the members of two or more base classes. Multiple inheritance in c++ is a powerful feature of Object-Oriented Programming. This technique reduces the program size and saves the programmer’s time. The programmer uses the existing base classes in the program coding to solve problems, The figure shown below illustrates the relation of the derived class

inheritance in c++

In the figure class C is derived from two base classes A & B The syntax of multiple inheritance in c++ is similar to that of single inheritance  class the name of base classes  are written separated by commas(,)

 The general syntax is:

{

Expressions;

}

 where:

 sp1——specifies the access specifier of the first base class

 sp2——- specifies the access specifier of the second base class

Example: how to write a program in multiple inheritance in c++:



Constructors in Multiple Inheritance in c++

When a class is derived from more than one base class, the constructor of the derived class as well as of all its base classes are executed when an object of the derived class is created. If the constructor functions have no parameters then first the constructor functions of the base classes are executed and then the constructor function of the derived class is executed. A program is given below to explain the execution of the constructor without parameters.

Simple program example of Constructors in Multiple Inheritance in c++:

Programming explanation:

In the above program, three classes are defined. The eee class is publicly derived from two classes aaa & bbb. All the classes have constructor functions. When the object of the derived class ccc is created,

the constructor functions are executed in the following sequence:
• the constructor of class aaa is executed first.
• the constructor of class bbb is executed second.
the constructor of derived class ccc is executed in the end.

Constructors in Multiple Inheritance in c++ with Arguments

To execute constructors of base classes that have arguments through the derived constructor functions, the derived class constructor is defined
as:
• write the constructor function of the derived class with parameters.
place a colon (:) immediately after this and then write the
constructor function names of base classes with parameters
separated by commas.

Example: how to write a constructor function with arguments in multiple inheritance in  c++:

 

Recommended For You

Leave a Reply

%d bloggers like this: