C++ Access Modifiers
In this tutorial, we will learn about the access modifiers for C++ classes with the help of examples. The access modifiers of C++ are public, private, and protected.
Access Modifiers
Access modifiers are used to implement an important aspect of Object-Oriented Programming known as data hiding.
Data hiding refers to restricting access to data members of a class. This is to prevent other functions and classes from tampering with the class data.
However, it is also important to make some member functions and member data accessible so that the hidden data can be manipulated indirectly.
The access modifiers of C++ allows us to determine which class members are accessible to other classes and functions, and which are not.
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
For example,
class Patient {
private:
int patientNumber;
string diagnosis;
public:
void billing() {
// code
}
void makeAppointment() {
// code
}
};
Here, the variables patientNumber and diagnosis of the Patient
class are hidden using the private
keyword, while the member functions are made accessible using the public
keyword.
Types of Access Modifiers in C++
There are 3 types of Access Modifiers in C++:
1. public
2. private
3. protected
Let's take a look at these modifiers with examples:
Public Access Modifier
All the class members declared under the public specifier will be available to everyone. A public member can be accessed anywhere outside the class but within a program.
The data members and member functions declared as public can be accessed by other classes and functions too.As there are no restrictions in public modifier, we can use the (.)dot operator to directly access member functions and data.
- The
public
keyword is used to create public members (data and functions). - The public members are accessible from any part of the program.
Example 1: C++ public Access Modifier
#include <stdio.h>
using namespace std;
// define a class
class Circle {
// public elements
public:
double radius;
double displayArea() {
return 3.14 *radius*radius;
}
};
int main() {
// declare a class object
Circle obj;
// accessing public data member outside class
obj.radius = 4.5;
cout << "Radius of Circle: " << obj.radius << endl;
cout << "The Area of Circle: " << obj.displayArea();
return 0;
}
Output
Radius of Circle: 4.5 The Area of Circle: 63.585
In this program, we have created a class named Circle
, which contains a public
variable radius and a public
function displayArea()
.
In main()
, we have created an object of the Circle
class named obj. We then access the public elements directly by using the codes obj.radius
and obj.displayArea()
.
Private Access Modifier
A private modifier is one of the best Access Modifiers in C++. The class members declared as private can be accessed only by the member functions inside the class.
A friend’s function (as I said can use your things )can be used to access private data members of the class. You will get a compile-time error while accessing private data members from anywhere outside the class.
- The
private
keyword is used to create private members (data and functions). - The private members can be accessed only from within the class.
- However, friend classes and friend functions can access private members.
Example 2: C++ private Access Specifier
We can convert a char
array to double
by using the std::atof()
function.
For Example :-
#include <stdio.h>
using namespace std;
// define a class
class Age {
// private elements
private:
int age;
// public elements
public:
void printAge(int a) {
age = a;
cout << "The Age is = " << age << endl;
}
};
int main() {
int ageInput;
// declare an object
Age obj;
cout << "Enter your age: ";
cin >> ageInput;
// call function and pass ageInput as argument
obj.printAge(ageInput);
return 0;
}
Output
Enter your age: 22 The Age is = 22
In main()
, the object obj cannot directly access the class variable age.
// error
cin >> obj.age;
We can only indirectly manipulate age through the public function printAge()
, since this function assigns age to the argument passed into it i.e. the function parameter int a
.
Protected Access Modifier
Before we learn about the protected
access specifier, make sure you know about inheritance in C++.
Protected, is the last access specifier, and it is similar to private, it can’t be accessed outside of it’s class unless with the help of friend class. But they can be accessed by any subclass of that class. Protected data members or functions can’t be access directly from other classes.
There are some restrictions on the protected modifier. Members declared in protected can only be protected up to the next level then it becomes private.
- The
protected
keyword is used to create protected members (data and function). - The protected members can be accessed within the class and from the derived class.
Example 3: C++ protected Access Specifier
This function accepts a number (can be any data type) and returns the number in the desired string.
// C++ programming example to understand the increment operator as prefix and postfix.
#include <iostream>
using namespace std;
// declare parent class
class Age {
// protected elements
protected:
int age;
};
// declare child class
class ChildAge : public Age {
public:
void printAge(int a) {
age = a;
cout << "The Age of Child= " << age << endl;
}
};
int main() {
int ageInput;
// declare object of child class
ChildAge child;
cout << "Enter your age: ";
cin >> ageInput;
// call child class function & pass ageInput as argument
child.printAge(ageInput);
return 0;
}
Output
Enter your age: 20 Age = 20
Here, ChildAge
is an inherited class that is derived from Age
. The variable age is declared in Age
with the protected
keyword.
This means that ChildAge
can access age since Age
is its parent class.
We see this as we have assigned the value of age in ChildAge
even though age is declared in the Age
class.
Summary: public, private, and protected
public
elements can be accessed by all other classes and functions.private
elements cannot be accessed outside the class in which they are declared, except byfriend
classes and functions.protected
elements are just like theprivate
, except they can be accessed by derived classes.
Specifiers | Same Class | Derived Class | Outside Class |
---|---|---|---|
public |
Yes | Yes | Yes |
private |
Yes | No | No |
protected |
Yes | Yes | No |
Note: By default, class members in C++ are private
, unless specified otherwise.
We hope that this tutorial helped you develop better understanding of the concept of Increment-Decrement-operator-difference-prefix-postfix in C++.
Keep Learning : )