It is possible to assign the address of the members of a class and assign it to pointers.

We can use pointers to access the data members of a class if that pointer has the data type which is the same as the class type.

The address of the data member of the class can be obtained using the & operator.

The class member pointer should be declared using the operator. In order to refer directly to a member of an object pointed by a pointer, we can use here the arrow operator (->) of indirection in C++.

C++ Program For Accessing Classes Members through Pointers program

using namespace std;
class Sample{ 
int a;
public: void read(){ 
cout<<"Enter a number :";cin>>a;
} void print(){ 
cout<<"n The entered value is :"<<a<<endl;
} }; 
int main(){ 
Sample s, *p;
p= &s;;
return 0; 

Output:-Enter a number : 29
The entered value is :29

Explanation of the program

In this program, p is a pointer which is the class type Sample. The pointer p points to the address of the object Also, the pointer p can access the printf() function directly and freely.

This article is written by our awesome writer
Comments to: How to Access Classes Members through Pointers in C++

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories


Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page


Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
    Strength indicator
    Log In | Lost Password