Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Inheritance in C++ programming language

Posted on February 15, 2017January 20, 2020

Table of Contents

  • Inheritance in C++ programming language
      • Hybrid inheritance  in C++

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.

 

Inheritance in C++ programming language
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.
Inheritance in C++ programming language
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

 

Inheritance in C++ programming language
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 
 
Inheritance in C++ programming language
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 
Inheritance in C++ programming language
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.

 

Single level inheritance in java         Multiple Inheritance
 
Multi level-inheritance in Java           Multilevel inheritance in C++
 
 Inheritance in Java  
 
Hierarchical Inheritance in Java
 
Interface in Java           Inheritance in C++                

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes