-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathbase_class_access_specifiers.cpp
More file actions
176 lines (148 loc) · 5.4 KB
/
base_class_access_specifiers.cpp
File metadata and controls
176 lines (148 loc) · 5.4 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
*
* Program: Base Class Access Specifier Examples
*
* Description: Demonstration of how the base class access specifier works with
* inheritance in C++. The base class access specifier works in conjunction
* with the base class member access specifiers to determine the access
* specification of the members in the derived class, in this code we go over
* the different combinations with examples.
*
* YouTube Lesson: https://www.youtube.com/watch?v=eFoHKBCRbKQ
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
// This table shows which access specifier will result in the derived class for
// which combination of base class access specifier and member access specifier
// in the base class.
//
//
// * Member Access Level
// Base Class *
// Access Specifier * Private Protected Public
// *****************************************************************
// *
// Public * Inaccessible Protected Public
// *
// Protected * Inaccessible Protected Protected
// *
// Private * Inaccessible Private Private
//
//
// The base class access specifier will affect how members are inherited by the
// derived class. For example, when using the proteted acces specifier when
// inheriting the base class, the public members of the base class will become
// protected members of the derived class. Generally, we use the public
// specifier. Notably, if no access specifier is provided the default is
// private.
//
#include <iostream>
using namespace std;
// A simple BaseClass with public, protected and private members
class BaseClass
{
public:
int _public_member;
protected:
int _protected_member;
private:
int _private_member;
};
// A derived class using the PUBLIC base class access specifier
class DerivedClass1 : public BaseClass
{
public:
void member()
{
// private members of the base class will be inaccessible in the derived
// class
// _private_member = 20;
// protected members of the base class will become protected members of
// the derived class and thus accessible in the derived class
_protected_member = 30;
// public members of the base class will become public members of the
// derived class
_public_member = 10;
}
};
// A derived class using the PROTECTED base class access specifier
class DerivedClass2 : protected BaseClass
{
public:
void member()
{
// private members of the base class will become inaccessible in the derived
// class
// _private_member = 30;
// protected members of the base class will become protected members in the
// derived class
_protected_member = 20;
// public members of the base class will ALSO become protected members in
// the derived class
_public_member = 10;
}
};
// A derived class using the PRIVATE base class access specifier
class DerivedClass3 : private BaseClass
{
public:
void member()
{
// private members of the base class will become inaccessible in the derived
// class
// _private_member = 30;
// both protected and public members of the base class will become private
// members in the derived class
_protected_member = 10;
_public_member = 20;
}
};
// If we make a derived class of DerivedClass3, we find that we cannot access
// _protected_member and _public_member because they were both made private
// members of DerivedClass3 when it inherited them from BaseClass using the
// private base class access specifier!
class NextLevel : public DerivedClass3
{
public:
void member()
{
// _protected_member = 20;
// _public_member = 30;
}
};
int main()
{
// create an instance of DerivedClass1 to confirm the usual behaviours of
// the members variables based on their access specification
DerivedClass1 derived1;
// we can't access the protected member outside of the class, as is the usual
// case with a protected member
//
// derived1._protected_member = 30;
// we can access a public member outside the class, as is the usual case with
// a public member
derived1._public_member = 10;
// create an instance of DerivedClass2 to confirm the usual behaviours of
// the members variables based on their access specification
DerivedClass2 derived2;
// we can't access the protected member outside of the class, as is the usual
// case with a protected member
//
// derived2._protected_member = 30;
// _public_member has now been turned into a protected member in DerivedClass2
// because we used the protected base class access specifier, and so we can
// no longer access it outside DerivedClass2
//
// derived2._public_member = 30;
// create an instance of DerivedClass3 to confirm the usual behaviours of
// the members variables based on their access specification
DerivedClass3 derived3;
// We cannot access _public_member or _protected_member because when we used
// the private base class access specifier when inheriting BaseClass it gave
// both these variabales a private access specification.
//
// derived3._public_member = 20;
// derived3._protected_member = 30;
return 0;
}