C++: Can you access private data members of a class from outside the class?
Can you access private data members of a class from outside the class?
Yes. This program shows how.
#include <iostream.h>
class emp
private :
int i ;
public :
emp( )
{
i = 10 ;
}
} ;
void main( )
emp *p = new emp ;
int *pi = (int*) p ;
cout << *pi ;
*pi = 20 ;
cout << *pi ;
}
The pointer to the class is typecasted in an integer pointer. With the help of this pointer private data member 'i' is accessed in main( ).




Comments
Log in or create a user account to comment.