-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsingle_protected.cpp
64 lines (60 loc) · 1.24 KB
/
single_protected.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
using namespace std;
class base
{
int a;
protected:
int b;
public:
int c;
void set_data(int a1,int b1, int c1)
{
a=a1; b=b1; c=c1;
}
base()
{
a=10; b=15; c=20;
}
};
class derived: protected base
{
int a2;
protected:
int b2;
public:
int c2;
derived()
{
a2=5;
b2=6;
c2=10;
}
void show()
{
cout<<"Protected Member of base "<<b<<endl;
cout<<"Public Member of base "<<c<<endl;
cout<<"Private Member of Derived "<<a2<<endl;
cout<<"Protected Member of Derived "<<b2<<endl;
cout<<"Public Member of Derived "<<c2<<endl;
cout<<endl;
}
void show1()
{
set_data(5,6,10);
cout<<"Protected Member of base "<<b<<endl;
cout<<"Public Member of base "<<c<<endl;
cout<<"Private Member of Derived "<<a2<<endl;
cout<<"Protected Member of Derived "<<b2<<endl;
cout<<"Public Member of Derived "<<c2<<endl;
}
};
int main()
{
cout<<"Single Level Inheritance with protected derivation"<<endl;
derived obj;
obj.show();
obj.show1();
return 0;
}
/* Note that in protected derivations public and private members are accessible only as protected. That is they can be accessed within child class but not in main with the object. Thats why here we have called set_data function within the derived class and not in main.
"Protected members are like private for a given class" */