Written by Sai Sandeep Thota on April 2, 2013 in C++ > Programming

What is Hierarchical Inheritance?

GET ALERTS:

Get our Latest updates delivered to your mailbox!

As we have discussed 3 types of Inheritances: Single, Multilevel, Multiple Inheritances we will dicuss about the 4th type of Inheritance i.e., Hierarchical Inheritance. Hierarchical Inheritance is that in which a Base class has many sub classes or when a Base class is used or inherited by many sub classes. Take a look at the structure below for understanding.

hierachical-inheritance

#include<conio.h>
#include<iostream.h>

class hier {
	protected:
	int x;
	public:
	void seti(int a) {
		x=a;
	}
	void showi() {
		cout<<"\nA Value:"<<x;
	}
};

class derived: public hier {
	protected:
	int j;
	public:
	void setj(int a, int b) {
		x=a;
		j=b;
	}
	void showj() {
		cout<<"\nA value:"<<x<<"\nB value:"<<j;
	}
};

class derived1: public hier {
	protected:
	int k;
	public:
	void setk(int c) {
		k=c;
	}
	void showk() {
		cout<<"\nC value:"<<k;
	}
};

void main() {
	derived ob;
	derived1 ob2;
	clrscr();
	ob.seti(10);
	ob.showi();
	ob.setj(222,2);
	ob.showj();
	ob2.seti(50);
	cout<<"\nOB2";
	ob2.showi();
	ob2.setk(23);
	ob2.showk();
	getch();
}

Output:
hierachical-inheritance-output

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: