c++ Program – illustrate the use of difference operators using friend function

by Nideesh C on April 14, 2011 · 0 comments

in C++




A program to illustrate the use of difference operators to access the class member using friend function.

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

Output:

Sum=30
Sum=70



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ program – illustrate the static member function
  2. Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..
  3. Sample – Program to find the sum of either of the diagonals of a 4 x 4 matrix.
  4. C++ program – illustrate a Single Inheritance
  5. C program to accept two matrices and find the sum and difference of the matrices

Leave a Comment

Previous post:

Next post: