C++ Overloading Relational Operators

When you add two integers in C++, you use the plus (+) operator. This operator, as with the other operators in C++, can be overloaded, meaning you can create your own definition of the operator. To be able to overload an operator can become handy with your user-defined types. Take the class Person that I created below as an example. The class stores a persons age and allows a programmer to check if a persons age is less than, greater than or equal to another. It also allows addition and subtraction of two persons ages, by just typing person + person or person - person

#include <iostream>

class Person {
public:
	Person(int age) : age(age) {};

	bool operator<(const Person& rhs) const {
		return age < rhs.age;
	}

	bool operator>(const Person& rhs) const {
		return age > rhs.age;
	}

	bool operator==(const Person& rhs) const {
		return age == rhs.age;
	}

	int operator+(const Person& rhs) const {
		return age + rhs.age;
	}

	int operator-(const Person& rhs) const {
		return age - rhs.age;
	}

private:
	int age;
};

int main() { 
	Person alfred(15);
	Person donald(10);

	std::cout << "Alfred < Donald: " <<  (alfred < donald) << "\n";
	std::cout << "Alfred > Donald: " << (alfred > donald) << "\n";
	std::cout << "Alfred == Donald: " << (alfred == donald) << "\n";
	std::cout << "Alfred + Donald: " << (alfred + donald) << "\n";
	std::cout << "Alfred - Donald: " << (alfred - donald) << "\n";
}

Output:

Alfred < Donald: 0
Alfred > Donald: 1
Alfred == Donald: 0
Alfred + Donald: 25
Alfred - Donald: 5

The first three rows in the output indicates false, true and false, which I think we can agree on if you read the code. The following two adds 15 + 10 = 25 and subtracts 15 - 10 = 5.

Overloaded operators follows the syntax:

<return type> operator<symbol for the operator>(const <type>&) const;

where the type in the parameter is an rhs, i.e the operand to the right side of an operator.

int operator+(const Person& rhs) const {
    return age + rhs.age;
}

suggests that we want an int returned when we add two persons, the sum of their age to be specific. The left hand side operator in the body of this function, age, is the age of the left operand, and rhs.age is the operand to the right of person + person.

Here is a table of the overloadable operators in C++

      +      -        *        /        %           ^
      &      |        ~        !         ,           =
      <      >       <=       >=        ++           --
     <<      >>       ==       !=        &&           ||
     +=      -=       /=      %=        ^=           &=
      |=      *=      <<=      >>=         []           ()
      ->      ->*      new      new []      delete      delete []

 

Author

authors profile photo

Articles with similar tags

thumbnail
C++ Inheritance - 1 - Base and Derived Classes

Inheritance is a form of software reuse in which a class can use another class’s methods and members and modify them.

By Frogitecture

thumbnail
C++ Inheritance – 2 – Protected Members

Two very common access specifier in C++ are public and private. Here I introduce the access specifier protected and give you an example of how you can use it.

By Frogitecture

thumbnail
C++ Inheritance – 3 – Constructors and Destructors

When a derived-class object is instantiated, a chain of constructor calls begins. Similarly, when a derived-class object is destroyed, destructor calls begins

By Frogitecture