Can We Access Private Data Members of a Class without using a Member or a Friend Function in C++? Last Updated : 21 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. So, is it possible to access private members outside a class without friend? Yes, it is possible using pointers. Although it's a loophole in C++, yes it's possible through pointers. Example 1: CPP // CPP Program to initialize the private members and display // them without using member functions #include <bits/stdc++.h> 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; } Output10 Example 2: CPP // CPP 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; }; // Driver Code int main() { A a; int* p = (int*)&a; *p = 3; p++; *p = 9; p--; cout << endl << "x = " << *p; p++; cout << endl << "y = " << *p; return 0; } Outputx = 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. The 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: CPP // CPP 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() { // object declaration B b; // pointer declaration int* p; // address of z is assigned to p p = &b.z; // initialization of z *p = 3; // points to previous location p--; // initialization of y *p = 4; // points to previous location p--; // initialization of x *p = 5; // passing address of x to function show() b.show(p); return 0; } Output: x = 5 y = 4 z = 3 Note: In 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. Time Complexity : O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Can We Access Private Data Members of a Class without using a Member or a Friend Function in C++? K kartik Improve Article Tags : C++ cpp-class cpp-friend Practice Tags : CPP Similar Reads Using Non-Member Friend Functions With Vector in C++ STL In C++, Non-member buddy functions may access a class's private members while not being members. The class definition declares them friend. Vector containers, dynamic arrays in C++ Standard Library (STL), may dynamically resize as members are added or deleted. You may need to establish a new class w 2 min read What happens when more restrictive access is given to a derived class method in C++? We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive access to derived class methods. For example the following program compiles fine. C++ #include<iostream> using namespace std; class Base { public: virtual int fun(int i) { } }; class Derived: publi 2 min read How to Provide a Swap Function for My Class in C++? In C++, providing a swap function for the class allows efficient swapping of the objects of that class type which can be important for many algorithms like sorting algorithms or container manipulations. In this article, we will learn how to implement a swap function for our own user defined class.sw 3 min read Printing the Address of an Object of Class in C++ Prerequisite: Classes and Objects in C++ The location of an object in the memory is called its address. Addressing is a necessary part of C++, it enables us to use any element as a reference and maintains the uniqueness of all the elements whether it is any variable, object, or container. In this ar 3 min read Memory Allocation in Static Data Members in C++ C++ allows defining static data members within a class using the static keyword. When a data member is declared as static, then we must keep the following note in mind: Irrespective of the number of objects created, only a single copy of the static member is created in memory. All objects of a class 4 min read Like