#include <iostream>
class Number
{
//overload operator+ so that we can add two Number objects
friend Number operator+(const Number &lhs, const Number &rhs);
//overload operator<< so that we can use std::cout<< c
friend std::ostream& operator<<(std::ostream &os, const Number& num);
int m_value = 0;
public:
Number(int value = 0);
//overload operator+= . This operator will be used inside operator+ definition
Number& operator+=(const Number &rhs);
Number& operator++();
Number operator++(int);
};
Number::Number(int value): m_value(value)
{
}
Number operator+(const Number &lhs, const Number &rhs)
{
Number sum = lhs; // copy data members from lhs into sum
sum += rhs; // add rhs into sum using compound operator
return sum;
}
Number& Number::operator+=(const Number &rhs)
{
m_value += rhs.m_value;
return *this;
}
std::ostream& operator<<(std::ostream &os, const Number& num)
{
os << num.m_value;
return os;
}
Number& Number::operator++()
{
++m_value;
return *this;
}
Number Number::operator++(int)
{
Number old = *this; // copy old value
operator++(); // prefix increment
return old; // return old value
}
void prn(const Number& a, const Number& b, const Number& c)
{
std::cout << "c a b " << std::endl
<< c << " " << a << " " << b << std::endl;
std::cout << "--------------------" << std::endl;
}
int main()
{
Number a{ 10 }, b{ 20 }, c;
c = a + b;
prn(a, b, c);
c = ++a;
prn(a, b, c);
c = a += b;
prn(a, b, c);
c = b++;
prn(a, b, c);
return 0;
}