Write a C++ program to declare a class. Declare pointer to class. Initialize and display the contents of the class member.
#include <iostream>
using namespace std;
class Data
{
public:
int a;
void print()
{
cout << "\n" << "Value of 'a' is "<< a << "\n";
}
};
int main()
{
Data d, *dp;
dp = &d; // pointer to object
int Data::*ptr=&Data::a; // pointer to data member 'a'
d.*ptr=10;
d.print();
dp->*ptr=20;
dp->print();
return 0;
}
output:
0 Comments