File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
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<<" \n Cow 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
+ }
You can’t perform that action at this time.
0 commit comments