Skip to content

Commit a2595ab

Browse files
authored
Merge pull request #638 from pushpakumari5117/issue-326
Added the code for Hybrid Inheritance in C++
2 parents f867806 + 38be7a4 commit a2595ab

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// C++ program to demonstrate Hybrid Inheritance
2+
3+
// Hybrid Inheritance - It is a combination of more than one type of Inheritance.
4+
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
8+
// base class
9+
class Animal{
10+
public:
11+
void eat(){
12+
cout<<"I can eat!"<<endl;
13+
}
14+
void sleep(){
15+
cout<<"I can sleep!"<<endl;
16+
}
17+
};
18+
19+
// derived class 1 from Animal
20+
class Cat:public Animal{
21+
public:
22+
void meow(){
23+
cout<<"I can meow! meow! meow!"<<endl;
24+
}
25+
};
26+
27+
// derived class 2 from Animal
28+
class Cow:public Animal{
29+
public:
30+
void moo(){
31+
cout<<"I can moo! moo! moo!"<<endl;
32+
}
33+
};
34+
35+
// derived from class Cat
36+
class Kitten:public Cat{
37+
public:
38+
void info(){
39+
cout<<"I am a Kitten."<<endl;
40+
}
41+
};
42+
43+
// Here in this code, we have Inherited Cow and Cat classes from Animal class; which is an example of
44+
// Hierarchical Inheritance.
45+
46+
// Also we have Inherited Kitten class from Cat class which itself is Inherited from Animal
47+
// class; so, this is an example of Multilevel Inheritance.
48+
49+
// So, we can say that together they(Hierarchical Inheritance & Multilevel Inheritance) form Hybrid
50+
// Inheritance
51+
52+
int main(){
53+
// Create object of Animal class
54+
Animal a1;
55+
cout<<"Animal Class:"<<endl;
56+
//Calling members of the Animal class
57+
a1.eat();
58+
a1.sleep();
59+
60+
// Create object of Cat class
61+
Cat cat1;
62+
cout<<"\nCat Class:"<<endl;
63+
//Calling members of the Animal class
64+
cat1.eat();
65+
cat1.sleep();
66+
//Calling members of the Cat class
67+
cat1.meow();
68+
69+
// Create object of Cow class
70+
Cow cow1;
71+
cout<<"\nCow Class:"<<endl;
72+
//Calling members of the Animal class
73+
cow1.eat();
74+
cow1.sleep();
75+
//Calling members of the Cow class
76+
cow1.moo();
77+
78+
// Create object of Kitten class
79+
Kitten k1;
80+
cout<<"\nKitten Class:"<<endl;
81+
//Calling members of the Animal class
82+
k1.eat();
83+
k1.sleep();
84+
//Calling members of the Cat class
85+
k1.meow();
86+
//Calling members of the Kitten class
87+
k1.info();
88+
return 0;
89+
}

0 commit comments

Comments
 (0)