Accessing members of a class using pointers (pointers to object members of a class) in C++

Accessing members of a class using pointers (pointers to object members of a class)

Pointers to Object members
We have seen use of pointers, to point to a variable. Pointers can also be used to point object (members of a class):

Program :-
#include<iostream.h>
class ABC
{
public:
int data;
void getdata()
{
cout<<"Enter data:";
cin>>data;
cout<<"The number entered is :"<<data;
}
};
void main()
{
ABC obj;
int *p;
obj.data=3
p = &obj.data;
cout<<*p;
ABC *q;
q =&obj.data;
q->getdata();
}


Output :-
3

Entered the number : 5
The number entered is : 5 

Comments

Popular posts from this blog

How to make a snake game using notepad using html [ with pictures ]

Program to find Addition of "n" numbers useing for loop

how to speed up your internet using cmd [command prompt] with pictures [100% working]