implement c++ class equal operator code example

Example 1: c++ operator overloading

// money.h -- define the prototype
class Money
{
   public:
      Money & operator += (const Money &rhs);
}

// money.cpp -- define the implementation
Money& Money :: operator += (const Money &rhs)
{
   // Yadda Yadda
  
   return *this;
}

Example 2: c++ operator overloading not equal

lass Fred {
public:
  // ...
};
if 
  // Without operator overloading:
  Fred add(const Fred& x, const Fred& y);
  Fred mul(const Fred& x, const Fred& y);
  Fred f(const Fred& a, const Fred& b, const Fred& c)
  {
    return add(add(mul(a,b), mul(b,c)), mul(c,a));    // Yuk...
  }
else
  // With operator overloading:
  Fred operator+ (const Fred& x, const Fred& y);
  Fred operator* (const Fred& x, const Fred& y);
  Fred f(const Fred& a, const Fred& b, const Fred& c)
  {
    return a*b + b*c + c*a;
  }
#endif

Tags:

Cpp Example