Can we access private data members of a class without using a member or a friend function?

The idea of Encapsulation is to bundle data and methods (that work on the data) together and restrict access of private data members outside the class. In C++, a friend function or friend class can also access private data members.

Is it possible to access private members outside a class without friend?
Yes, it is possible using pointers. See the following program as an example.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class Test {
private:
    int data;
  
public:
    Test() { data = 0; }
    int getData() { return data; }
};
  
int main()
{
    Test t;
    int* ptr = (int*)&t;
    *ptr = 10;
    cout << t.getData();
    return 0;
}

chevron_right


Output:

10

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

/*Program to initialize the private members and 
display them without using member functions.*/
  
#include <bits/stdc++.h>
using namespace std;
  
class A {
private:
    int x;
    int y;
};
  
int main()
{
    A a;
    int* p = (int*)&a;
    *p = 3;
    p++;
    *p = 9;
    p--;
    cout << endl
         << "x = " << *p;
    p++;
    cout << endl
         << "y = " << *p;
}

chevron_right


Output:



x = 3
y = 9

Explanation:
In the above program, a is an object of class A. The address of the object is assigned to integer pointer p by applying typecasting. The pointer p points to private member x. Integer value is assigned to *p, that is, x. Address of object a is increased and by accessing the memory location value 9 is assigned to y. The p– statement sets the memory location of x. Using the cout statement contains of x is displayed.

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

/*Program to initialize and display
private members using pointers.*/
#include <bits/stdc++.h>
using namespace std;
  
class A {
private:
    int x;
    int y;
};
  
class B : public A {
public:
    int z;
  
    void show(int* k)
    {
        cout << "x = " << *k << " y = " 
            << *(k + 1) << " z = " << *(k + 2);
    }
};
  
int main()
{
    B b; // object declaration
    int* p; // pointer declaration
    p = &b.z; // address of z is assigned to p
    *p = 3; // initialization of z
    p--; // points to previous location
    *p = 4; // initialization of y
    p--; // points to previous location
    *p = 5; // initialization of x
    b.show(p); // passing address of x to function show()
  
    return 0;
}

chevron_right


Output:

x = 5 y = 4 z = 3

/*This code is contributed by Shubham Sharma.*/

Note that the above way of accessing private data members is not at all a recommended way of accessing members and should never be used. Also, it doesn’t mean that the encapsulation doesn’t work in C++. The idea of making private members is to avoid accidental changes. The above change to data is not accidental. It’s an intentionally written code to fool the compiler.

This article is contributed by Ashish Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Improved By : auspicious_boy

Article Tags :
Practice Tags :


14


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.