C++ Class Access Modifiers
In C++ object-oriented programming (OOP), data encapsulation is one of the core concepts. Simply put, it means hiding data and exposing only the necessary interfaces to the outside world, to ensure data security.
To control who can see this data, C++ provides three keywords, calledaccess modifiers:
public(public)private(private)protected(protected)
class
Base
{
public
:
// public members
protected
:
// protected members
private
:
// Private members
}
;
For ease of understanding, we can think of aclass as your home:
| Modifiers | Real-world analogy | Who can access? | Typical use |
|---|---|---|---|
| public | Living room / front door | Everyone(class internals, subclasses, external code) | Interface functions (API) exposed to the outside. |
| protected | Bedroom | You and your children(class internals, subclasses) | Data for use only within the family (inheritance hierarchy). |
| private | Safe | Only yourself(only within this class) | Core data, variables you don't want arbitrarily modified. |
Note: If you write no modifier, C++ class membersdefault to
private(private) .
Public members
PublicMembers can be accessed anywhere in the program without going through member functions. Example below:
Example
#include
<
iostream
using
namespace
std;
class
Line
{
public
:
double
length
;
void
setLength
(
double
len
)
;
double
getLength
(
void
)
;
}
;
// Member function definition
double
Line
::
getLength
(
void
)
{
return
length
;
}
void
Line
::
setLength
(
double
len
)
{
length
=
len
;
}
// main function of the program
int
main
(
)
{
Line
line
;
// Set length
line
.
setLength
(
6
.0
)
;
cout
<<
"
Length of line :
"
<<
line
.
getLength
(
)
<<
endl
;
// Set length without using a member function
line
.
length
=
10
.0
;
// OK: because length is public
cout
<<
"
Length of line :
"
<<
line
.
length
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Length of line : 6
Length of line : 10
Private members
Private members are completely closed off from outside the class; external code cannot read, modify, or call them, and derived classes also have no direct access. Only the class's own member functions and entities granted friend privileges can manipulate this content.
If no access specifier is used in a class, members default to private. As shown below, width automatically falls into the private region, meaning unmarked members are treated as private:
Example
class
Box
{
double
width
;
public
:
double
length
;
void
setWidth
(
double
wid
)
;
double
getWidth
(
void
)
;
}
;
In practice, we usually define data in the private region and related functions in the public region, so these functions can also be called from outside the class, as shown below:
Example
#include
<
iostream
using
namespace
std;
class
Box
{
public
:
double
length
;
void
setWidth
(
double
wid
)
;
double
getWidth
(
void
)
;
private
:
double
width
;
}
;
// Member function definition
double
Box
::
getWidth
(
void
)
{
return
width
;
}
void
Box
::
setWidth
(
double
wid
)
{
width
=
wid
;
}
// main function of the program
int
main
(
)
{
Box
box
;
// Set length without using a member function
box
.
length
=
10
.0
;
// OK: because length is public
cout
<<
"
Length of box :
"
<<
box
.
length
<<
endl
;
// Set width without using a member function
// box.width = 10.0; // Error: because width is private
box
.
setWidth
(
10
.0
)
;
// Set width using a member function
cout
<<
"
Width of box :
"
<<
box
.
getWidth
(
)
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Length of box : 10
Width of box : 10
Protected members
protected exists mainly forinheritance.
- Without inheritance, it behaves like
privateprivate (invisible to outsiders). - With inheritance,a subclass (derived class) can access the parent class's
protectedmembers, but cannot accessprivatemembers.
In the next chapter, you will learn about derived classes and inheritance. Now, in the example below, we derive from the parent class Box a subclass smallBox.
The following example is similar to the previous one, where width members can be accessed by any member function of the derived class smallBox.
Example
#include
<
iostream
using
namespace
std;
class
Box
{
protected
:
double
width
;
}
;
class
SmallBox
:
Box
// SmallBox is the derived class
{
public
:
void
setSmallWidth
(
double
wid
)
;
double
getSmallWidth
(
void
)
;
}
;
// Member function of the subclass
double
SmallBox
::
getSmallWidth
(
void
)
{
return
width
;
}
void
SmallBox
::
setSmallWidth
(
double
wid
)
{
width
=
wid
;
}
// main function of the program
int
main
(
)
{
SmallBox
box
;
// Set width using a member function
box
.
setSmallWidth
(
5
.0
)
;
cout
<<
"
Width of box :
"
<<
box
.
getSmallWidth
(
)
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Width of box : 5
Access permission changes in inheritance
When you define a subclass (such as class B : public A), what follows the colon is public also an access specifier, which determineshow the parent class's members "appear" in the subclass.
This may seem complex, but you only need to remember thisdowngrade principle:
The inheritance method determines the maximum permission of the parent class's members in the subclass.
If the inheritance method is stricter than the member's original permission, the member's permission is "downgraded" to the level of the inheritance method.
Permission change table
| Original permission of parent class member | public inheritance (most common) | protected inheritance | private inheritance |
|---|---|---|---|
| public | Remains public | Downgraded to protected | Downgraded to private |
| protected | Remains protected | Remains protected | Downgraded to private |
| private | Inaccessible | Inaccessible | Inaccessible |
Note: "inaccessible" means subclass code cannot directly use the variable, but the variable still exists in memory.
Comprehensive demonstration code
For clarity, we rename the variables to pub_var (public), pro_var (protected), pri_var (private).
Example
#include
using
namespace
std
;
class
Parent
{
public
:
int
pub_var
;
protected
:
int
pro_var
;
private
:
int
pri_var
;
// Only Parent itself can touch it
public
:
Parent
(
)
{
pub_var
=
1
;
pro_var
=
2
;
pri_var
=
3
;
}
}
;
// 1. Public Inheritance - most common, unchanged
class
ChildA
:
public
Parent
{
public
:
void
test
(
)
{
cout
<<
pub_var
<<
endl
;
// OK
cout
<<
pro_var
<<
endl
;
// OK
// cout << pri_var << endl; // error! base-class private members are not visible
}
}
;
// 2. Protected Inheritance - everything becomes protected
class
ChildB
:
protected
Parent
{
public
:
void
test
(
)
{
cout
<<
pub_var
<<
endl
;
// OK, but in ChildB's view, it is protected
cout
<<
pro_var
<<
endl
;
// OK
}
}
;
// 3. Private Inheritance - everything becomes private
class
ChildC
:
private
Parent
{
public
:
void
test
(
)
{
cout
<<
pub_var
<<
endl
;
// OK, but in ChildC's view, it is private
cout
<<
pro_var
<<
endl
;
// OK, but in ChildC's view, it is private
}
}
;
int
main
(
)
{
ChildA a
;
cout
<<
a.
pub_var
<<
endl
;
// OK, accessible from outside
// cout << a.pro_var << endl; // error: protected is not accessible from outside
ChildB b
;
// cout << b.pub_var << endl; // error! due to protected inheritance, pub_var becomes protected outside
ChildC c
;
// cout << c.pub_var << endl; // error! due to private inheritance, pub_var becomes private outside
return
0
;
}
Summary
- Public: usable by anyone. Used as an interface (API).
- Private: only usable by itself. Used for storing data (the default safe option).
- Protected: only usable by the family (inheritance chain). Used to leave a backdoor for subclasses.
inheritance:
- Public inheritance: the parent class's attributes remain unchanged (most common).
- Private/Protected inheritance: tightens the permissions of the parent class's members, typically used in special implementation scenarios.
C++ Classes & Objects