Friday, 10 August 2012

Pointer to members using C++

#include<iostream.h>
#include<conio.h>
class M
{
int x,y;
public:
void setxy(int a,int b)
{
x=a;
y=b;
}
friend int sum(M m);
};
int sum(M m)
{
int M::*px;
px=&M::x;
int M::*py;
py=&M::y;
M *pm;
pm=&m;
int s,s1,s2;
s1=m.*px;
s2=pm->*py;
s=s1+s2;
return s;
}
int main()
{
M n;
void (M::*pf)(int,int)=&M::setxy;
(n.*pf)(10,20);
n.setxy(10,20);
cout<<"the sum is"<<sum(n);
M *op;
op=&n;
(op->*pf)(30,40);
cout<<sum(n);
getch();
return 0;
}

No comments:

Post a Comment

There was an error in this gadget