![]() |
In C++, Private members of a class are accessible only within the class they are declared and by friend functions, they are not accessible outside the class not even by derived classes. In this article, we will learn how to access private variables in C++. Accessing Private Variables From Class in C++In C++, we can access private variables through public getter function. These public methods provide us with controlled access to private variables. The getter function returns the value of the private variable, and the setter function sets the value of the private variable. Syntax of Getter
Here,
C++ Program to Access a Private VariableThe below example demonstrates the use of getter to access a private variable in C++.
Output Private Variable Value : 11 Time Complexity: O(1)
|
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |