Skip to content

Commit e695ebb

Browse files
Added the code for Protected Inheritance in C++
1 parent a6ac158 commit e695ebb

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// C++ program to demonstrate Protected Inheritance
2+
3+
// Protected Inheritance - It is defined as the inheritance in which public members of the base class becomes protected members
4+
// of the derived class and also the protected members of the base class becomes protected members of the derived class.
5+
6+
//Note : Private members are not inherited.
7+
8+
#include<bits/stdc++.h>
9+
using namespace std;
10+
11+
// base class
12+
class Animal{
13+
private:
14+
void info(){
15+
cout<<"I am an animal !!"<<endl;
16+
}
17+
protected:
18+
void sleep(){
19+
cout<<"I can sleep!"<<endl;
20+
}
21+
public:
22+
void eat(){
23+
cout<<"I can eat!"<<endl;
24+
}
25+
};
26+
27+
// derived class
28+
class Cat:protected Animal{
29+
public:
30+
void animal_info(){
31+
sleep();
32+
eat();
33+
}
34+
void meow(){
35+
cout<<"I can meow! meow! meow!"<<endl;
36+
}
37+
};
38+
39+
int main(){
40+
// Create object of the Cat class
41+
Cat cat1;
42+
// Calling public and protected members of the base class through derived class function
43+
// Note : Only derived class can access the member functions and data members of the base class in case of protected inheritance.
44+
cat1.animal_info();
45+
// Calling member of the derived class
46+
cat1.meow();
47+
return 0;
48+
}

0 commit comments

Comments
 (0)