Skip to content

Commit 0347301

Browse files
authored
Merge pull request #642 from pushpakumari5117/issue-328
Added the code for Protected Inheritance in C++
2 parents a643014 + a162ee2 commit 0347301

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
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+
}

OOPs/Inheritance/Public_Inheritance_cpp.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)