How to Access Private Variables in C++?
How to Access Private Variables in C++?
Private members are only accessible to class members. It is performed to preserve the object-oriented paradigm's encapsulation, which ensures that data and the related methods are kept in a single unit and are only accessible by class members. Three distinct access specifiers are available in C++ to control how visible a class's variables are.
- Public: When a class variable has visibility public, it means that any other class can access it. It is the first of the three access specifiers.
- Private: Members of the class with private visibility can only be accessed from within the class.
- Protected: Only the class and its subclasses have access to protected class variables.
We will concentrate on using the class's private variables exclusively.
Using getter and setter methods for data variables:
A class's private variables can be accessed and changed using the getter and setter procedures. The functions known as getter and setter are responsible for returning the data variables and "setting" or modifying the data variables. In order to grasp the idea further, we will look at two examples. First, the fundamental syntax is provided below.
Syntax:
It has the following syntax:
Getter/ Accessor functions:
private: <datatype> value; public: <datatype> getterFunction() { return <datatype> this->value; }
Setter/Mutator functions:
private: <datatype> value; public: void setterFunction(<datatype> _value) { this->value = _value; }
Example:
Let us take an example to illustrate how to access private variables in C++.
#include <iostream> using namespace std; class Test{ private: int k; public: int getk() { return this->k; } void setk(int _k) { this->k = _k; } }; int main(){ Test test; test.setk(20); cout << "The value we set is: " << test.getk() << endl; return 0; }
Output:
The value we set is: 20
Variable function access from within another function:
When we are utilizing a private variable function, the same rules apply. Just as with a data variable, we must access it from within a class variable method. The 'this' pointer can be used to prevent name collisions.
Syntax:
It has the following syntax:
private: <returntype> function_name(params) {}; public: <returntype> another_function(params) { <datatype> var = this->function_name(arguments); }
Public declaration is required for the function that calls the private variable function. The public function must be called from the class object in order for the function to be executed.
Example:
#include <iostream> using namespace std; class Test{ private: int v; void multiplyV() { this->v = this->v * 10; } public: int multiplyAndGetV() { this->multiplyV(); return this->v; } void setV(int _value) { this->v = v; } }; int main(){ Test test; test.setV(15); cout << "The value after setting and multiplying is: " << test.multiplyAndGetV() << endl; return 0; }
Output:
The value after setting and multiplying is: 150
Using friend classes:
In C++, a class that has access to a class's secret and protected variables, which are hidden from view by other classes, is referred to as a friend class. The term "friend" must be used to designate a class as another class' friend. Let's examine its operation.
Syntax:
It has the following syntax:
class A{ private: ..... friend class B; }; class B{ //class body };
Example:
#include <iostream> using namespace std; class Test1{ private: int e; public: Test1(int _e) { this->e = _e; } friend class Test2; }; class Test2{ public: void display(Test1 &t) { cout << "The value of Test1 object is: " << t.e; } }; int main(){ Test1 test1(15); Test2 test2; test2.display(test1); return 0; }
Output:
The value of the Test1 object is: 15
Using friend functions:
In C++, friend functions and friend classes are comparable. Here, we can designate a certain function that isn't a class variable as a "friend," granting it access to the private variables of the class. Let's examine the syntax for defining a function as "friend".
Syntax:
class A{ private: ..... friend <return_type> function_name(params); }; <return_type> function_name(params) { }
Example:
#include <iostream> using namespace std; class Test1{ private: int w; public: Test1(int _w) { this->w = _w; } friend void display(Test1); }; void display(Test1 t) { cout << "The value of Test1 object is: " << t.w; } int main(){ Test1 test1(55); display(test1); return 0; }
Output:
The value of the Test1 object is: 55
Other object-oriented languages do not have friend functions because they do not always preserve the object-oriented encapsulation feature. Friendships are not reciprocal; if class A adds class B as a friend, class B will have access to all of A's variables, but class A won't have access to all of B's private variables.
Conclusion:
In conclusion, we should use the mutator/setter and accessor/getter procedures to gain access to a class's private data variables. It is the class's safest method of accessing the data variables. It is important always to remember that functions that access private variables need to be marked as public.