0% found this document useful (0 votes)
40 views

C++ Inheritance - 5

The document discusses hierarchical inheritance in C++. Hierarchical inheritance involves deriving multiple subclasses from a common base class, so that the subclasses inherit features from the base class. An example is provided of a base "Car" class that subclasses like "Audi" and "Ferrari" can inherit from. The key aspects of hierarchical inheritance include a base class defining common features, subclasses deriving from the base class, and subclasses accessing base class properties based on the access specifier used. An example program demonstrates two subclasses "B" and "C" deriving from a base class "A" and calling different methods that utilize a shared data member from the base class.

Uploaded by

md shankar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

C++ Inheritance - 5

The document discusses hierarchical inheritance in C++. Hierarchical inheritance involves deriving multiple subclasses from a common base class, so that the subclasses inherit features from the base class. An example is provided of a base "Car" class that subclasses like "Audi" and "Ferrari" can inherit from. The key aspects of hierarchical inheritance include a base class defining common features, subclasses deriving from the base class, and subclasses accessing base class properties based on the access specifier used. An example program demonstrates two subclasses "B" and "C" deriving from a base class "A" and calling different methods that utilize a shared data member from the base class.

Uploaded by

md shankar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

10/13/2020 C++ Hierarchical Inheritance (With Examples) - Trytoprogram

C++ Tutorials

C++ Hierarchical Inheritance

When several classes are derived from common base class it is called hierarchical inheritance.

In C++ hierarchical inheritance, the feature of the base class is inherited onto more than one sub-class.

For example, a car is a common class from which Audi, Ferrari, Maruti etc can be derived.

Following block diagram highlights its concept.

C++ Hierarchical Inheritance Block Diagram

www.trytoprogram.com/cplusplus-programming/hierarchical-inheritance/ 1/6
10/13/2020 C++ Hierarchical Inheritance (With Examples) - Trytoprogram

As shown in above block diagram, in C++ hierarchical inheritance all the derived classes have common base
class. The base class includes all the features that are common to derived classes.

As in other inheritance, based on the visibility mode used or access speci er used while deriving, the
properties of the base class are derived. Access speci er can be private, protected or public.

Click here to learn in detail about access speci ers and their use in inheritance

C++ Hierarchical Inheritance Syntax

www.trytoprogram.com/cplusplus-programming/hierarchical-inheritance/ 2/6
10/13/2020 C++ Hierarchical Inheritance (With Examples) - Trytoprogram

class A // base class


{
..............
};
class B : access_specifier A // derived class from A
{
...........
} ;
class C : access_specifier A // derived class from A
{
...........
} ;
class D : access_specifier A // derived class from A
{
...........
} ;

C++ Hierarchical Inheritance Example

www.trytoprogram.com/cplusplus-programming/hierarchical-inheritance/ 3/6
10/13/2020 C++ Hierarchical Inheritance (With Examples) - Trytoprogram

// hierarchial inheritance.cpp
#include <iostream>
using namespace std;

class A //single base class


{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};
class B : public A //B is derived from class base
{
public:
void product()
{
cout << "\nProduct= " << x * y;
}
};
class C : public A //C is also derived from class base
{
public:
void sum()
{
cout << "\nSum= " << x + y;
}
};
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
} //end of program

www.trytoprogram.com/cplusplus-programming/hierarchical-inheritance/ 4/6
10/13/2020 C++ Hierarchical Inheritance (With Examples) - Trytoprogram

Output

Enter value of x and y:


2
3
Product= 6
Enter value of x and y:
2
3
Sum= 5

Explanation
In this example, there is only one base class A from which two class B and C are derived.

Both derived class have their own members as well as base class members.

The product is calculated in the derived class B , whereas, the sum is calculated in the derived class C but
both use the values of x and y from the base class.

www.trytoprogram.com/cplusplus-programming/hierarchical-inheritance/ 5/6
10/13/2020 C++ Hierarchical Inheritance (With Examples) - Trytoprogram

All rights reserved @trytoprogram.com

www.trytoprogram.com/cplusplus-programming/hierarchical-inheritance/ 6/6

You might also like