Skip to content

Commit b763511

Browse files
Added the code for Multilevel Inheritance in C++
1 parent d97ccf9 commit b763511

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// C++ program to demonstrate Multilevel Inheritance
2+
3+
// Multilevel Inheritance - It is defined as the inheritance in which the derived class is inherited
4+
// from a base class which itself is inherited from another base class.
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
// base class for Cat
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+
// base class for Kitten, and derived from class Animal
21+
class Cat:public Animal{
22+
public:
23+
void meow(){
24+
cout<<"I can meow! meow! meow!"<<endl;
25+
}
26+
};
27+
28+
// derived from class Cat
29+
class Kitten:public Cat{
30+
public:
31+
void info(){
32+
cout<<"I am a Kitten."<<endl;
33+
}
34+
};
35+
36+
int main(){
37+
// Create object of Kitten class
38+
Kitten k1;
39+
//Calling members of the Kitten class
40+
k1.info();
41+
//Calling members of the Cat class
42+
k1.meow();
43+
//Calling members of the Animal class
44+
k1.eat();
45+
k1.sleep();
46+
return 0;
47+
}

0 commit comments

Comments
 (0)