What is Hierarchical inheritance in C++ programmig language?

What is Hierarchical inheritance in C++ programmig language?

What is Inheritance in C++ :
In C++,
-Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class.

Type of Inheritance in C++:
-Single Inheritance
-Multiple Inheritance
-Multilevel Inheritance
-Hierarchical inheritance
-Hybrid inheritance

Hierarchical inheritance:
-Hierarchical inheritance is defined as the process of deriving more than one class from a base class.
syntax:
class parent
{
};
class child1 : public parent
{
};
class child2 : public parent
{
}

program:

#include < iostream>
using namespace std;
// Declaration of base class.
class Shape
{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
// inheriting Shape class
class Rectangle : public Shape
{
public:
int rect_area()
{
int result = a*b;
return result;
}
};
// inheriting Shape class
class Triangle : public Shape
{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
cout <<"Enter the length and breadth of a rectangle: "<< endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
cout << "Area of the rectangle is: " << m<< endl;
cout << "Enter the base and height of the triangle: "<< endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
cout <<"Area of the triangle is : " << n<< endl;
return 0;
}




Read More →
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!