Basics Of Overloading Operators in C++

Ever wanted to compare two Dogs? Well now you can!

class Dog
{
  private:
    int m_age;
    string m_name;
  public:
    Dog(const int age = 1, const string name = "Un-named")
    {
      m_age = age;
      m_name = name;
    }

    //Here's where it gets interesting...
    //Basic syntax
    bool operator ==(const Dog rhs); //rhs = right-hand side

};

bool Dog::operator ==(const Dog rhs)
{
  return (m_age == rhs.m_age && m_name == rhs.m_name);
}

//Implementation...

Dog spike(3, "Spike"), brownie(7, "Brownie");
//Notice that brownie is on the right-hand side
//of the == operator
bool dogs_are_equal = (spike == brownie); //false