I am newbie in c++. In the below code example there is a & before operator<< but there is no & before operator==. When should i use & and why? I know this is reference sign but it really hard to understand for me.
class Money {
int dollars;
int cents;
public:
Money(int dollars, int cents);
Money(int total);
friend std::ostream &operator<<(std::ostream &os, const Money &rhs);
bool operator==(const Money &rhs) const;
};
Money::Money(int dollars, int cents) : dollars{dollars}, cents{cents} {}
Money::Money(int total) : dollars {total/100}, cents{total%100} {}
std::ostream &operator<<(std::ostream &os, const Money &rhs){
os << "dollars: " << rhs.dollars << " cents: " << rhs.cents;
return os;
}
bool Money::operator==(const Money &rhs) const{
if(dollars == rhs.dollars && cents == rhs.cents)
return true;
else
return false;
}
int main(){
Money money1{3,12};
Money money2{3,1};
std::cout << money1 << std::endl; // dollars: 3 cents: 12
if(money1==money2)
std::cout << "These are equal" << std::endl;
else
std::cout << "These are not equal" << std::endl;
return 0;
}