File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments