Q:

Transform code from member function to non member function

#include <iostream>
using namespace std;

class family
{
private:
    double weight;
    double height;
public:
    family(double x,double y); 
    ~family();
    double getWeight();
    double getHeight();
    double setWeight();
    double setHeight();
    bool operator==(const family &)const; 
};

bool family::operator ==(const family &b)const
{
         return weight==b.weight;
}     

family::family(double x, double y)
{
    weight = x;
    height = y;
}

double family::getWeight()
{
    return weight;
}

double family::getHeight()
{
    return height;
}

family::~family(){}

int main()
{
 family a(70.0,175.2);
 family b(68.5,178.2);

 if(a==b)
 cout << "A is bigger than B" << endl;
 else
 cout << "A is smaller than B" << endl;

return 0;
}

Above the code, I can implement operator overloading with member function. However, I failed to implement operator overloading with non member function. How should i modify this code b.b
Please help me..

A:

Basically, the only difference between a member function and a non-member function is that it's passed an implicit this pointer as well as any other arguments and it has access to private/protected members.  So to transform any member function to a non-member function is to simply factor it out of the class definition. make it a friend of that class and add a parameter that's a reference to that class.  Pass in an object of that class when you call it.  You can also do a const& of the function.

