C++ Class Access Modifiers
Data encapsulation is a fundamental feature of object-oriented programming, which prevents functions from directly accessing the internal members of a class type. The access restrictions for class members are specified by marking the various sections within the class body with public
, private
, and protected
. The keywords public
, private
, and protected
are known as access modifiers.
A class can have multiple sections marked as public, protected, or private. Each marked section remains effective until the next marked section begins or until the closing brace of the class body is encountered. The default access modifier for members and classes is private.
class Base {
public:
// Public members
protected:
// Protected members
private:
// Private members
};
Public Members
Public members are accessible outside the class in the program. You can set and get the values of public variables without using any member functions, as shown below:
Example
#include <iostream>
using namespace std;
class Line
{
public:
double length;
void setLength( double len );
double getLength( void );
};
// Member function definitions
double Line::getLength(void)
{
return length ;
}
void Line::setLength( double len )
{
length = len;
}
// Main function
int main( )
{
Line line;
// Set length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
// Set length without using member function
line.length = 10.0; // OK: because length is public
cout << "Length of line : " << line.length <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Length of line : 6
Length of line : 10
Private Members
Private member variables or functions are not accessible outside the class, not even visible. Only the class and friend functions can access private members.
By default, all members of a class are private. For example, in the following class, width is a private member, which means if you do not use any access modifier, the members of the class will be assumed to be private:
Example
class Box
{
double width;
public:
double length;
void setWidth( double wid );
double getWidth( void );
};
In practice, we typically define data in the private section and related functions in the public section so that these functions can also be called 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 definitions
double Box::getWidth(void)
{
return width ;
}
void Box::setWidth( double wid )
{
width = wid;
}
// Main function
int main( )
{
Box box;
// Set length without using member function
box.length = 10.0; // OK: because length is public
cout << "Length of box : " << box.length <<endl;
// Set width without using member function
// box.width = 10.0; // Error: because width is private
box.setWidth(10.0); // Use member function to set width
cout << "Width of box : " << box.getWidth() <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Length of box : 10
Width of box : 10
Protected Members
Protected member variables or functions are very similar to private members, but with one difference: protected members are accessible in the derived class (i.e., the subclass).
In the next chapter, you will learn about derived classes and inheritance. For now, you can see in the following example that we derive a subclass SmallBox from the parent class Box.
The following example is similar to the previous one, where the width member 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 member function
box.setSmallWidth(5.0);
cout << "Width of box : "<< box.getSmallWidth() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Width of box : 5
Characteristics of Inheritance
There are public, protected, and private inheritance methods, which correspondingly change the access attributes of the base class members.
-
- Public Inheritance: The access attributes of base class public members, protected members, and private members in the derived class become: public, protected, private
-
- Protected Inheritance: The access attributes of base class public members, protected members, and private members in the derived class become: protected, protected, private
-
- Private Inheritance: The access attributes of base class public members, protected members, and private members in the derived class become: private, private, private
However, regardless of the inheritance method, the following two points remain unchanged:
-
- Private members can only be accessed by members of the class (within the class) and friends, and cannot be accessed by derived classes;
-
- Protected members can be accessed by derived classes.
Public Inheritance
Example
#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
int a;
A(){
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun(){
cout << a << endl; // Correct
cout << a1 << endl; // Correct
cout << a2 << endl; // Correct
cout << a3 << endl; // Correct
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : public A{
public:
int a;
B(int i){
A();
a = i;
}
void fun(){
cout << a << endl; // Correct, public member
cout << a1 << endl; // Correct, base class public member, remains public in derived class.
cout << a2 << endl; // Correct, base class protected member, remains protected and can be accessed in derived class.
cout << a3 << endl; // Incorrect, base class private member cannot be accessed in derived class.
}
};
int main(){
B b(10);
cout << b.a << endl;
cout << b.a1 << endl; // Correct
cout << b.a2 << endl; // Error, protected members cannot be accessed outside the class
cout << b.a3 << endl; // Error, private members cannot be accessed outside the class
system("pause");
return 0;
}
Protected Inheritance
Example
#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
int a;
A(){
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun(){
cout << a << endl; // Correct
cout << a1 << endl; // Correct
cout << a2 << endl; // Correct
cout << a3 << endl; // Correct
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : protected A{
public:
int a;
B(int i){
A();
a = i;
}
void fun(){
cout << a << endl; // Correct, public member.
cout << a1 << endl; // Correct, base class public member, becomes protected in derived class, can be accessed by derived class.
cout << a2 << endl; // Correct, base class protected member, remains protected in derived class, can be accessed by derived class.
cout << a3 << endl; // Error, base class private member cannot be accessed by derived class.
}
};
int main(){
B b(10);
cout << b.a << endl; // Correct, public member
cout << b.a1 << endl; // Error, protected members cannot be accessed outside the class.
cout << b.a2 << endl; // Error, protected members cannot be accessed outside the class.
cout << b.a3 << endl; // Error, private members cannot be accessed outside the class.
system("pause");
return 0;
}
Private Inheritance
Example
#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
int a;
A(){
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun(){
cout << a << endl; // Correct
cout << a1 << endl; // Correct
cout << a2 << endl; // Correct
cout << a3 << endl; // Correct
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : private A{
public:
int a;
B(int i){
A();
a = i;
}
void fun(){
cout << a << endl; // Correct, public member.
cout << a1 << endl; // Correct, base class public member, becomes private in derived class, can be accessed by derived class.
cout << a2 << endl; // Correct, base class protected member, becomes private in derived class, can be accessed by derived class.
cout << a3 << endl; // Error, base class private member cannot be accessed by derived class.
}
};
int main(){
B b(10);
cout << b.a << endl; // Correct, public member
cout << b.a1 << endl; // Error, private members cannot be accessed outside the class.
cout << b.a2 << endl; // Error, private members cannot be accessed outside the class.
cout << b.a3 << endl; // Error, private members cannot be accessed outside the class.
system("pause");
return 0;
}