Skip to content

Commit d97ccf9

Browse files
Added the code for Hierarchical Inheritance in C++
1 parent 64539e5 commit d97ccf9

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// C++ program to demonstrate Hierarchical Inheritance
2+
3+
// Hierarchical Inheritance - It is defined as the inheritance in which more that one derived classes
4+
// are inherited from a single base class.
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
// base class
10+
class Animal{
11+
public:
12+
void eat(){
13+
cout<<"I can eat!"<<endl;
14+
}
15+
void sleep(){
16+
cout<<"I can sleep!"<<endl;
17+
}
18+
};
19+
20+
// derived class 1
21+
class Cat:public Animal{
22+
public:
23+
void meow(){
24+
cout<<"I can meow! meow! meow!"<<endl;
25+
}
26+
};
27+
28+
// derived class 2
29+
class Cow:public Animal{
30+
public:
31+
void moo(){
32+
cout<<"I can moo! moo! moo!"<<endl;
33+
}
34+
};
35+
36+
int main(){
37+
// Create object of Cat class
38+
Cat cat1;
39+
cout<<"Cat Class:"<<endl;
40+
//Calling members of the base class
41+
cat1.eat();
42+
cat1.sleep();
43+
//Calling members of the derived class
44+
cat1.meow();
45+
46+
// Create object of Cow class
47+
Cow cow1;
48+
cout<<"\nCow Class:"<<endl;
49+
//Calling members of the base class
50+
cow1.eat();
51+
cow1.sleep();
52+
//Calling members of the derived class
53+
cow1.moo();
54+
return 0;
55+
}

0 commit comments

Comments
 (0)