Access Specifier in C++
Access specifiers are used to instruct the compiler how the members like attributes and methods of a class can be accessed. It is also known as the Access Modifiers.
There are 3 types of access modifiers available in C++:
- Public
- Private
- Protected
Public Access Specifier
In C++, public specifier is used to make data members and functions accessible from anywhere in the program. When functions and variables are declared as public, they become accessible from anywhere including other classes and functions. The public members of a class can be accessed from anywhere in the program using the direct member access operator "." with the object of that class.
The public keyword is used to declare a variable or function as public as shown in the example below:
#include <iostream>
using namespace std;
// class definition
class Shop
{
public:
double discountPrecentage = 2;
string item = "Coffee";
string getShopName()
{
return "Best Coffee Shop";
}
};
// main function
int main()
{
//Creating Shop class object
Shop obj;
// accessing public data member outside class
cout << "Default item : " << obj.item << "\n";
cout << "Default item discount : " << obj.discountPrecentage << "%\n";
cout << "Shop name : " << obj.getShopName() << "\n";
// updating public data member outside class
obj.item = "Tea";
obj.discountPrecentage = 1;
cout << "Updated item : " << obj.item << "\n";
cout << "Updated item discount : " << obj.discountPrecentage << "%\n";
return 0;
}
Output
Default item discount : 2%
Shop name : Best Coffee Shop
Updated item : Tea
Updated item discount : 1%
Private Access Specifier
In C++, the private access specifier is used to limit the access of data members and functions. When data members and functions are declared as private, they are not allowed to be accessed by any object or function outside the class.
The private keyword is used to declare a variable or function as private as shown in the example below:
#include <iostream>
using namespace std;
class Student
{
private:
int age;
public:
void setAge(int age)
{
this->age = age;
}
int getAge() {
return this->age;
}
};
int main()
{
int ageInput;
// declare an object
Student obj1;
// call function and pass age
int age = 27;
obj1.setAge(age);
// call getAge() function to display age
cout << "Student age is : " << obj1.getAge();
return 0;
}
Output
The example below will show error because we are trying to call private class variable and function from outside the class where they are declared:
#include <iostream>
using namespace std;
// class definition
class Student
{
private:
string name = "Tom";
int age = 28;
string getName()
{
return this->name;
}
};
// main function
int main()
{
// Creating Student class object
Student obj;
// accessing public data member outside class
cout << "Print student name from within same class : " << obj.name << "\n";
cout << "Print student aget from within same class : " << obj.age << "%\n";
cout << "Print student name from within same class using function : " << obj.getName() << "\n";
return 0;
}
Protected Access Specifier
In C++, protected access specifier is used to limit direct accessibility of variables and functions unless with the help of a friend class. The protected members can be accessed by any derived class of that class.
The protected keyword is used to declare a variable or function as protected as shown in the example below:
#include <iostream>
using namespace std;
// Creating parent class
class Animal
{
protected:
string name;
};
// Creating subclass or derived class from parent class
class Dog : public Animal
{
public:
void setName(string name)
{
// Child class can access protected data members of parent class
this->name = name;
}
void displayName()
{
cout << "Dog name is: " << name << endl;
}
};
// main function
int main()
{
Dog dogObj1;
// member functions of subclass class can
// access the protected data members of parent class
dogObj1.setName("Tommy");
dogObj1.displayName();
return 0;
}