Skip to content

Commit a643014

Browse files
authored
Merge pull request #641 from pushpakumari5117/issue-327
Added the code for Public Inheritance in C++
2 parents 25fdd56 + b50a508 commit a643014

File tree

2 files changed

+45
-89
lines changed

2 files changed

+45
-89
lines changed

OOPs/Inheritance/Hybrid_Inheritance_cpp.cpp

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// C++ program to demonstrate Public Inheritance
2+
3+
// Public Inheritance - It is defined as the inheritance in which public members of the base class becomes public members
4+
// of the derived class and 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:public Animal{
29+
public:
30+
void meow(){
31+
cout<<"I can meow! meow! meow!"<<endl;
32+
}
33+
};
34+
35+
int main(){
36+
// Create object of the Cat class
37+
Cat cat1;
38+
// Calling public members of the base class (also the public member of the derived class)
39+
cat1.eat();
40+
// Calling protected members of the base class (also the protected member of the derived class)
41+
cat1.sleep();
42+
// Calling member of the derived class
43+
cat1.meow();
44+
return 0;
45+
}

0 commit comments

Comments
 (0)