Hierarchical Inheritance - OOP's Concept
What is Hierarchical Inheritance OOP's Concept in C++?
Explanation
Hierarchical Inheritance is a method of inheritance where one or more derived classes is derived from common base class.
Example:
#include<iostream.h>
#include<conio.h>
class first
{
public:
int a;
};
class sec:public first
{
public:
int b;
void get()
{
cin>>a>>b;
}
void disp()
{
cout<<a*b;
}
};
class third:public first
{
public:
int c;
void get()
{
cin>>a>>c;
}
void disp()
{
cout<<a*c;
}
};
int main()
{
sec s;
third t;
s.get();
t.get();
s.disp();
t.disp();
getch();
return 0;
}
exp 2:
#include<iostream.h>
#include<conio.h>
class first
{
int a;
public:
void get()
{
cin>>a;
}
int disp()
{
return a;
}
};
class second:public first
{
int b;
public:
void getsec(){
get();
cout<<"enter avlue of b";
cin>>b;
}
void sum()
{
cout<<"sum ="<<disp()+b;
}
};
class third:public first
{
int c;
public: void getthird()
{
get();
cout<<"enter value if c";
cin>>c;
}
void sub()
{
cout<<"sub="<<disp()-c;
}
};
main()
{
second s1;
s1.getsec();
s1.sum();
third t1;
t1.getthird();
t1.sub();
getch();
}
Hierarchal Inheritance
Reviewed by Shobhit Goel
on
May 05, 2015
Rating:
No comments:
Post a Comment