In this article, you will learn about C++ access specifiers i.e. private, public and protected with example and explanation
Access Specifiers: Introduction |
Private Access Specifiers |
Protected Access Specifiers |
Public Access Specifiers |
C++ access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class.
For example, the class members are grouped into sections, private
protected
and public
. These keywords are called access specifiers which define the accessibility or visibility level of class members.
By default the class members are private
. So if the visibility labels are missing then by default all the class members are private
.
In inheritance, it is important to know when a member function in the base class can be used by the objects of the derived class. This is called accessibility and the access specifiers are used to determine this.
Access specifier can be either private or protected or public. In general access specifiers are the access restriction imposed during the derivation of different subclasses from the base class.
If private access specifier is used while creating a class, then the public and protected data members of the base class become the private member of the derived class and private member of base class remains private.
In this case, the members of the base class can be used only within the derived class and cannot be accessed through the object of derived class whereas they can be accessed by creating a function in the derived class.
Following block diagram explain how data members of base class are inherited when derived class access mode is private.
Note: Declaring data members with private
access specifier is known as data hiding.
// private access specifier.cpp
#include <iostream>
using namespace std;
class base
{
private:
int x;
protected:
int y;
public:
int z;
base() //constructor to initialize data members
{
x = 1;
y = 2;
z = 3;
}
};
class derive: private base
{
//y and z becomes private members of class derive and x remains private
public:
void showdata()
{
cout << "x is not accessible" << endl;
cout << "value of y is " << y << endl;
cout << "value of z is " << z << endl;
}
};
int main()
{
derive a; //object of derived class
a.showdata();
//a.x = 1; not valid : private member can't be accessed outside of class
//a.y = 2; not valid : y is now private member of derived class
//a.z = 3; not valid : z is also now a private member of derived class
return 0;
} //end of program
Output
x is not accessible value of y is 2 value of z is 3
[adsense1]
Explanation
When a class is derived from the base class with private
access specifier the private members of the base class can’t be accessed. So in above program, the derived class cannot access the
So in above program, the derived class cannot access the member x
which is private in the base class, however, derive
class has access to the protected and public members of the base class. So the
Hence the function showdata
in derived class can access the public and protected member of the base class.
private
member of that class results in an error.If protected access specifier is used while deriving class then the public and protected data members of the base class becomes the protected member of the derived class and private member of the base class are inaccessible.
In this case, the members of the base class can be used only within the derived class as protected members except for the private members.
Following block diagram explain how data members of base class are inherited when derived class access mode is protected.
// protected access specifier.cpp
#include <iostream>
using namespace std;
class base
{
private:
int x;
protected:
int y;
public:
int z;
base() //constructor to initialize data members
{
x = 1;
y = 2;
z = 3;
}
};
class derive: protected base
{
//y and z becomes protected members of class derive
public:
void showdata()
{
cout << "x is not accessible" << endl;
cout << "value of y is " << y << endl;
cout << "value of z is " << z << endl;
}
};
int main()
{
derive a; //object of derived class
a.showdata();
//a.x = 1; not valid : private member can't be accessed outside of class
//a.y = 2; not valid : y is now private member of derived class
//a.z = 3; not valid : z is also now a private member of derived class
return 0;
} //end of program
Output
x is not accessible value of y is 2 value of z is 3
If public access specifier is used while deriving class then the public data members of the base class becomes the public member of the derived class and protected members becomes the protected in the derived class but the private members of the base class are inaccessible.
Following block diagram explain how data members of base class are inherited when derived class access mode is public
// public access specifier.cpp
#include <iostream>
using namespace std;
class base
{
private:
int x;
protected:
int y;
public:
int z;
base() //constructor to initialize data members
{
x = 1;
y = 2;
z = 3;
}
};
class derive: public base
{
//y becomes protected and z becomes public members of class derive
public:
void showdata()
{
cout << "x is not accessible" << endl;
cout << "value of y is " << y << endl;
cout << "value of z is " << z << endl;
}
};
int main()
{
derive a; //object of derived class
a.showdata();
//a.x = 1; not valid : private member can't be accessed outside of class
//a.y = 2; not valid : y is now private member of derived class
//a.z = 3; not valid : z is also now a private member of derived class
return 0;
} //end of program
Output
x is not accessible value of y is 2 value of z is 3
This is all about C++ access specifiers.