(Note this is not the final post, there will be more implementation later.)
Think following scenario: We are trying to track our money. So we create a class for it.

#include <string> #include <iostream> class Money { public: std::string currency; int amount; }; int main() { Money inBank; inBank.currency = "dollar"; inBank.amount = 10; std::cout << "Total money I have: " << inBank.amount << " " << inBank.currency << std::endl; return 0; } // Total money I have: 10 dollar 

It looks a bit hard to print total money. It could be so great if we could just write std::cout << inBank << std::endl. Let's try that first.

#include <string> #include <iostream> class Money { public: std::string currency; int amount; }; int main() { Money inBank; inBank.currency = "dollar"; inBank.amount = 10; std::cout << "Total money I have: " << inBank << std::endl; return 0; } // Error 

If you try to compile this code, you'll get a compilation error. The compiler doesn't know how to handle a Money object with the << operator. However, we can define how this operator should behave for our class. This is one of the powerful features of C++.

#include <string> #include <iostream> class Money { public: std::string currency; int amount; }; std::ostream& operator<<(std::ostream& out, Money m) { out << m.amount << " " << m.currency; return out; } int main() { Money inBank; inBank.currency = "dollar"; inBank.amount = 10; std::cout << "Total money I have: " << inBank << std::endl; return 0; } // Total money I have: 10 dollar 

There are two ways to overload operators: using member functions and non-member functions. Here, we used a non-member function. We said:

  • Take a ostream object in variable out, which will be std::cout in main function
  • Take a money object
  • Then print to the stream given as I say.
  • Finally return the stream so that I can chain the stream. (Like cout << "a" << "b" << ... will become cout << "b" << ... after printing a)

We basically defined a new form for the << operator. Now it can take money object as a parameter.

Looks much cleaner.

Let's another money object. It will be hard to manually define the attributes from now on, so let's create a constructor too.

#include <string> #include <iostream> class Money { public: std::string currency; int amount; Money(std::string currency, int amount) { this->currency = currency; this->amount = amount; } }; std::ostream& operator<<(std::ostream& out, Money m) { out << m.amount << " " << m.currency; return out; } int main() { Money inBank("dollar", 10); Money inPocket("dollar", 25); std::cout << "Money in bank: " << inBank << std::endl; std::cout << "Money in pocket: " << inPocket << std::endl; return 0; } // Money in bank: 10 dollar  // Money in pocket: 25 dollar 

Perfect. Let's calculate total money we have.

#include <string> #include <iostream> class Money { public: std::string currency; int amount; Money(std::string currency, int amount) { this->currency = currency; this->amount = amount; } }; std::ostream& operator<<(std::ostream& out, Money m) { out << m.amount << " " << m.currency; return out; } int main() { Money inBank("dollar", 10); Money inPocket("dollar", 25); std::cout << "Total money I have: " << inBank + inPocket << std::endl; return 0; } // Error 

It throws an error. The compiler doesn't know how to add 2 money objects together. We could create a function like add(Money m1, Money m2), but chaining multiple additions would become less convenient. Instead, let's overload the + operator for Money objects.

#include <string> #include <iostream> class Money { public: std::string currency; int amount; Money(std::string currency, int amount) { this->currency = currency; this->amount = amount; } }; std::ostream& operator<<(std::ostream& out, Money m) { out << m.amount << " " << m.currency; return out; } Money operator+(Money m1, Money m2) { Money ret("dollar", m1.amount + m2.amount); return ret; } int main() { Money inBank("dollar", 10); Money inPocket("dollar", 25); std::cout << "Total money I have: " << inBank + inPocket << std::endl; return 0; } // Total money I have: 35 dollar 

Here is what we said for + operator.

  • Take 2 money objects as parameters.
  • Create another money object with total money given.
  • Return the total money object. So we can chain it.

Let's try with other money objects.

#include <string> #include <iostream> class Money { public: std::string currency; int amount; Money(std::string currency, int amount) { this->currency = currency; this->amount = amount; } }; std::ostream& operator<<(std::ostream& out, Money m) { out << m.amount << " " << m.currency; return out; } Money operator+(Money m1, Money m2) { Money ret("dollar", m1.amount + m2.amount); return ret; } int main() { Money inBank("dollar", 10); Money inPocket("dollar", 25); Money inDrawer("dollar", 50); std::cout << "Total money I have: " << inBank + inPocket + inDrawer << std::endl; return 0; } // Total money I have: 85 dollar 

It works!

What about we try add different currencies together. It will be weird since we didn't check that anywhere. Let's add some check for currencies. Also change the string for dollar to $ .

#include <string> #include <iostream> class Money { public: std::string currency; int amount; Money(std::string currency, int amount) { this->currency = currency; this->amount = amount; } }; std::ostream& operator<<(std::ostream& out, Money m) { out << m.amount << m.currency; return out; } Money operator+(Money m1, Money m2) { if (m1.currency != m2.currency) throw std::runtime_error("Different currencies cannot be added"); Money ret(m1.currency, m1.amount + m2.amount); return ret; } int main() { Money inBank("$", 10); Money inPocket("$", 25); Money inDrawer("$", 50); std::cout << "Total money I have: " << inBank + inPocket + inDrawer << std::endl; return 0; } // Total money I have: 85$ 

Looks great. Until now, we only used non-member functions for operator overloading. Let's define ++ pre-increment operator for money objects. Because it should change the original object, we better define it as a member function.

#include <string> #include <iostream> class Money { public: std::string currency; int amount; Money(std::string currency, int amount) { this->currency = currency; this->amount = amount; } Money operator++() { this->amount++; return *this; } }; std::ostream& operator<<(std::ostream& out, Money m) { out << m.amount << m.currency; return out; } Money operator+(Money m1, Money m2) { if (m1.currency != m2.currency) throw std::runtime_error("Different currencies cannot be added"); Money ret(m1.currency, m1.amount + m2.amount); return ret; } int main() { Money inBank("$", 10); Money inPocket("$", 25); Money inDrawer("$", 50); Money total = inBank + inPocket + inDrawer; std::cout << "Total money I have: " << total << std::endl; ++total; std::cout << "Total money I have: " << total << std::endl; return 0; } // Total money I have: 85$  // Total money I have: 86$ 

Works perfect!

By the way, some operators like =, [], (), -> must be defined as member functions. So check before typing.

Here, we could've define pre-incerement operator as a non-member function like this:

#include <string> #include <iostream> class Money { public: std::string currency; int amount; Money(std::string currency, int amount) { this->currency = currency; this->amount = amount; } }; std::ostream& operator<<(std::ostream& out, Money m) { out << m.amount << m.currency; return out; } Money operator+(Money m1, Money m2) { if (m1.currency != m2.currency) throw std::runtime_error("Different currencies cannot be added"); Money ret(m1.currency, m1.amount + m2.amount); return ret; } Money operator++(Money& m) { m.amount++; return m; } int main() { Money inBank("$", 10); Money inPocket("$", 25); Money inDrawer("$", 50); Money total = inBank + inPocket + inDrawer; std::cout << "Total money I have: " << total << std::endl; ++total; std::cout << "Total money I have: " << total << std::endl; return 0; } 

We used a reference for the money object so we could be able to change it. If both approaches work, what's the point of choosing between member and non-member functions? There is no point, but here is the difference:

  • When we call the pre-increment operator as a member function it gets the object as this parameter. So if the attributes were private, it could still work.
  • When we call it as non-member function, it doesn't get this as a parameter. So if the attributes are private it won't work.

Because we generally make attributes private, we could define operators as member functions.

#include <string> #include <iostream> class Money { private: std::string _currency; int _amount; public: Money(std::string currency, int amount) { _currency = currency; _amount = amount; } }; std::ostream& operator<<(std::ostream& out, Money m) { out << m._amount << m._currency; return out; } Money operator+(Money m1, Money m2) { if (m1._currency != m2._currency) throw std::runtime_error("Different currencies cannot be added"); Money ret(m1._currency, m1._amount + m2._amount); return ret; } Money operator++(Money& m) { m._amount++; return m; } int main() { Money inBank("$", 10); Money inPocket("$", 25); Money inDrawer("$", 50); Money total = inBank + inPocket + inDrawer; std::cout << "Total money I have: " << total << std::endl; ++total; std::cout << "Total money I have: " << total << std::endl; return 0; } // a bunch of private attribute errors 

Let's define + and ++ operator as private.

class Money { private: std::string _currency; int _amount; public: Money(std::string currency, int amount) { _currency = currency; _amount = amount; } Money operator+(Money other) { if (this->_currency != other._currency) throw std::runtime_error("Different currencies cannot be added"); Money ret(this->_currency, this->_amount + other._amount); return ret; } Money& operator++() { this->_amount Source:https://dev.to/yunemak/c-operator-overloading-1pi

Related posts