Thursday, October 16, 2014

write a program for the use of dereferancing operator to access the class member in C++

Prog 31: write a program for the use of dereferancing operator to access the class member.

#include<iostream.h>
#include<conio.h>
class A
{

public:

int x,y;

void getdata(int a,int b)
{

x=a;
y=b;
}
void display()
{
cout<<"\nx = "<<x;
cout<<"\ny = "<<y;
}

};
int sum(A a)
{

friend int sum(A);

int A :: *px=&A::x;

int A :: *py=&A::y;

int s=a.*px+a.*py;

return s;
}
void main()
{
clrscr();

A a1;

void(A::*pf)(int,int)=&A::getdata;

A *op=&a1;
(op->*pf)(5,10);

a1.display();

cout<<endl<<sum(a1);

getch();
}

Output:

x = 5
y = 10
15



No comments: