Categories: C++Learn C++Oop C++

Inheritance in C++ programming language

Inheritance in C++ programming language

In this tutorial, we will discuss the OOP concept of Inheritance in C++ programming language.

Inheritance is one of the Oop concepts in object-oriented programs such as C++, java. It is where one object acquires all the properties of (data members and methods) it’s the parent or base class.
It means the new class inherits some of the properties and behaviour of the existing class. The new class is the child or derived class and the existing class is known as the parent class.
It is the process of reusing the properties of an existing class without modifying them.

Existing class – Base class – The base class has its own properties and functionality. It’s also known as the parent class. It has the common qualities that all the objects can inherit.

New class – Derived class – The derived class is the child class. The derived class inherits the properties and functionality of the base class.

 

Table

Advantages in inheritance

  • Inheritance means code reusability  The existing class or base class can be used by the derived class without any changes . Hence, when a code is created, it can be used by many programmers for their use.
  • Data abstraction – Derived class can add its own data and member function to the class without affecting the base class.
  • No need to define the data members again.
  • Inheritance in C++ takes place between classes.
Diagram

Type of inheritance in C++

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Multiple inheritances
  • Hybrid inheritance

 

Single inheritance

Single inheritance is easy to understand as class extends to only one class (Single child is derived from single parent). Single inheritance only has one parent and child class. Child class has its own property as well as those authorised from the parent class. The diagram below shows that only B extend from A. Here, A is the parent class or base class and B is the derived or child class.
Single inheritance in C++ with example program

 

Single
class A{ //parent class
Block of code.
………………..
};
public class B: public A{ //child class
Block of code.
………………..
}

 Multilevel inheritance

Multilevel means one class(derived) extends from two or more (more than one) classes(base). A class can be the child (derived) class as well as the parent class to other classes. In the diagram below, Class B inherits the property of class A and Class C inherits the property of class B. Again,
in order, Class A is the parent to Class B and Class B is the parent to Class C.
Multilevel inheritance in C++ with Example
Multi level

 

class A{ //parent class
Block of code.
………………..
};
class B: public A{ //parent class or child class
Block of code…
……………………
};
class C :public B{//child class
Block of code.
……………………
};
……may be continue

..
Multiple inheritance

In multiple inheritance, one derived class extends from more than one parent class. In multiple inheritance, one child class has two or more parent classes. In the diagram below,  Class C inherits the properties of class A and Class B.
Multiple inheritance in C++ with Example
Multiple

 

Public class A{

 

Block of code…
}
public class B {
Block of code…
}
public class C : A,B{
Block of code…
}
Hierarchical inheritance
In the hierarchical inheritance, one parent class can be inherited by many children classes. In the example below, class A is inherited by Class B and Class C.  Class A acts as the base class for class B and class C.
Hierarchical inheritance with example in C++
Public class A{



Block of code…
}
public class B extends A{
Block of code…
}
public class C extends A{
Block of code…
}

Hybrid inheritance  in C++

Hybrid inheritance is different from other inheritance. It is a combination of single, multilevel, multiple, and hierarchical inheritance.

 

Interface in Java       Inheritance in C++            
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.