-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotected_access_modifier.cpp
More file actions
43 lines (35 loc) · 987 Bytes
/
protected_access_modifier.cpp
File metadata and controls
43 lines (35 loc) · 987 Bytes
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
#include <iostream>
using namespace std;
class Base
{
private:
int a = 10;
protected:
int b = 20;
};
class Derive : public Base
{
public:
void print()
{
// cout<<"The value of the a is "<<a<<endl; //This show the error cause a is private so we can inheritate it
cout << "The value of the b is " << b << endl; // we can inheritate the protected members but not access as private
}
};
int main()
{
Base o;
// cout<<"The value of the b is "<<o.b<<endl; //This show error b is protected
Derive o1;
o1.print();
// cout<<o.b<<endl; //error cause b is protected
return 0;
}
// Note :
/*
For a protected member:
Public Derivation Private Derivation Protected Derivation
1. Private members Not Inherited Not Inherited Not Inherited
2. Protected members Protected Private Protected
3. Public members Public Private Protected
*/