🎓 A1Learner
🔍
0 of 34 lessons completed

C++ Operator Overloading

Operator overloading is the practice of giving a class its own meaning for an operator sign. The sign then works on objects of that class in the same short form as on plain values.

Adding member by member

The total object starts at 0 dollars. Its member is filled from the first object and then raised by the member of the second object.

#include <iostream>

class Money {
public:
  int dollars;
  Money(int amount) {
    dollars = amount;
  }
};

int main() {
  Money first(12);
  Money second(8);
  Money total(0);
  total.dollars = first.dollars;
  total.dollars = total.dollars + second.dollars;
  std::cout << "Total: $" << total.dollars << "\n";
  return 0;
}

operator+ in one line

The operator+ function adds the dollars members and builds a new Money from the sum. The call in main is one short line, and the same total prints.

#include <iostream>

class Money {
private:
  int dollars;
public:
  Money(int amount) {
    dollars = amount;
  }
  Money operator+(const Money& other) const {
    return Money(dollars + other.dollars);
  }
  void print() {
    std::cout << "Total: $" << dollars << "\n";
  }
};

int main() {
  Money first(12);
  Money second(8);
  Money total = first + second;
  total.print();
  return 0;
}

operator== compares amounts

The operator== function compares the dollars members of two Money objects. Equal amounts print true and different amounts print false.

#include <iostream>

class Money {
private:
  int dollars;
public:
  Money(int amount) {
    dollars = amount;
  }
  bool operator==(const Money& other) const {
    return dollars == other.dollars;
  }
};

int main() {
  std::cout << std::boolalpha;
  Money first(20);
  Money second(20);
  Money third(15);
  std::cout << "First equals second: " << (first == second) << "\n";
  std::cout << "First equals third: " << (first == third) << "\n";
  return 0;
}

operator<< prints through cout

The friend declaration inside the class lets the outside function read the private dollars member. The object then prints through std::cout like a plain value.

#include <iostream>

class Money {
private:
  int dollars;
public:
  Money(int amount) {
    dollars = amount;
  }
  friend std::ostream& operator<<(std::ostream& out, const Money& money);
};

std::ostream& operator<<(std::ostream& out, const Money& money) {
  out << "$" << money.dollars;
  return out;
}

int main() {
  Money price(20);
  std::cout << "Price: " << price << "\n";
  return 0;
}

Example

#include <iostream>

class Money {
private:
  int dollars;
public:
  Money(int amount) {
    dollars = amount;
  }
  Money operator+(const Money& other) const {
    return Money(dollars + other.dollars);
  }
  bool operator==(const Money& other) const {
    return dollars == other.dollars;
  }
  friend std::ostream& operator<<(std::ostream& out, const Money& money);
};

std::ostream& operator<<(std::ostream& out, const Money& money) {
  out << "$" << money.dollars;
  return out;
}

int main() {
  std::cout << std::boolalpha;
  Money wallet(12);
  Money extra(8);
  Money total = wallet + extra;
  Money target(20);
  std::cout << "Total: " << total << "\n";
  std::cout << "Matches target: " << (total == target) << "\n";
  return 0;
}

Key points

  • A function named operator+ defines what the + sign does for objects of a class.
  • operator== returns a bool that tells whether two objects count as equal.
  • operator<< writes an object to an output stream.
  • A friend function is a function outside a class that may read the private members of the class.
  • operator<< is a friend function, so std::cout can stand on its left side.
  • Overloading changes the meaning of a sign for one class only.

In this example, wallet holds 12 dollars and extra holds 8 dollars. The + sign builds the total through operator+, and the total prints as $20 through operator<<. The == test compares the total with a target of 20 dollars and prints true.

Note

An overloaded operator keeps the same number of operands as the built-in sign.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which line adds two Money objects through the overloaded operator+?