C++ Program to Access private members of a class


Private members of a class are only accessed by the members of the class. This is done to preserve the object-oriented paradigm encapsulation, which ensures data and its related functions are kept in one unit and only accessible only from a member of that class. C++ has three different access specifiers to specify the visibility of the members of a class. The three access specifiers are −

  • Public − If a member of a class has the visibility public, then the members can be accessed from any other class.

  • Private − Class members having private visibility can be accessed from within the class only.

  • Protected − protected class members can be accessed from with9in the class or from its subclasses only.

For this article, we will focus on accessing private members of the class only.

Using getter and setter methods for data members

Getter and setter functions are used to access and modify the private members of a class. As the name suggests, the getter functions return the data members, and the setter functions are used to ‘set’ or modify the data members. We take two examples to understand the concept further, but before that the basic syntax is given below.

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

#include <iostream>

using namespace std;

class Test{
   private:
      int value;
   public:
      //the getter function
      int getValue() {
         return this->value;
      }
      //the setter function
      void setValue(int _value) {
         this->value = _value;
      }
};

int main(){
   Test test;
   test.setValue(15);
   cout << "The value we set is: " << test.getValue() << endl;
   return 0;
}

Output

The value we set is: 15

Accessing member functions from inside another function

The same thing holds when we are accessing a private member function. We have to access it from inside a class member method in the same way as we did for a data member. We can use the ‘this’ pointer for avoiding name collisions.

Syntax

private:
   <returntype> function_name(params) {};
public:
   <returntype> another_function(params) {
     <datatype> var = this->function_name(arguments);
   }

The function that is calling the private member function should be declared public. The function will only execute if the public function is called from the object of that class.

Example

#include <iostream>

using namespace std;

class Test{
   private:
      int value;
      //multiplies the member value by 10
      void multiplyValue() {
         this->value = this->value * 10;
      }
   public:
      //the getvalue function calls the multiply value function
      int multiplyAndGetValue() {
         this->multiplyValue();
         return this->value;
      }
      //the setter function
      void setValue(int _value) {
         this->value = _value;
      }
};

int main(){
   Test test;
   test.setValue(15);
   cout << "The value after setting and multiplying is: " << test.multiplyAndGetValue() << endl;
   return 0;
}

Output

The value after setting and multiplying is: 150

Using friend classes

A friend class in C++ is a class that can access the private and protected members of a class that are not visible from any other class. To declare a class as a friend of another class, the keyword ‘friend’ is to be used. Let’s take a look at how it works.

Syntax

class A{
   private:
     .....
   friend class B;
};

class B{
   //class body
};

Example

#include <iostream>

using namespace std;

class Test1{
   private:
      int value;
   public:
      Test1(int _value) {
         this->value = _value;
      }
      //we declare another class as a friend
      friend class Test2;
};

class Test2{
  public:
   //displays the value of the other class object
   void display(Test1 &t) {
      cout << "The value of Test1 object is: " << t.value;
   }
};

int main(){
   //creating two class objects of the two classes
   Test1 test1(15);
   Test2 test2;

   //calling the friend class function
   test2.display(test1);
   return 0;
}

Output

The value of Test1 object is: 15

Using friend functions

Friend functions in C++ are similar to that of friend classes. Here, we can declare a particular function that is not a member of a class as a ‘friend’ and it will gain the access to a class’s private members. Let’s take a look at the syntax of how to define a function as ‘friend’.

Syntax

class A{
   private:
     .....
   friend <return_type> function_name(params);
};

<return_type> function_name(params) {
   //function body
}

Example

#include <iostream>

using namespace std;

class Test1{
   private:
      int value;
   public:
      Test1(int _value) {
         this->value = _value;
      }
      //we declare a friend function
      friend void display(Test1);
};

void display(Test1 t) {
   cout << "The value of Test1 object is: " << t.value;
}

int main(){
   //creating two class objects of the two classes
   Test1 test1(55);
   //calling the friend class function
   display(test1);
   return 0;
}

Output

The value of Test1 object is: 55

Conclusion

When we are accessing the private data members of a class, we preferably use the accessor/getter and the mutator/setter functions. That is the safest way to access the data members of the class. One thing to be always kept in mind is that the functions that are accessing the private members should be declared as public. Friend functions are not available in other object−oriented languages because this does not maintain the object−oriented encapsulation property always. Friends are not mutual, if class A has declared class B as a friend, then class B will have access to all the members of A but A will not have access to all the private members of B.

Updated on: 13-Dec-2022

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements