I have just started learning c++ and I have a question.
In the following example is confusing to me why function Add have return type Cents and why are c1 and c2 are passed by reference.
Code:
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
int GetCents() { return m_nCents; }
};
Cents Add(Cents &c1, Cents &c2)
{
Cents cTemp(c1.GetCents() + c2.GetCents());
return cTemp;
}
int main()
{
Cents cCents1(6);
Cents cCents2(8);
Cents cCentsSum = Add(cCents1, cCents2);
std::cout << "I have " << cCentsSum.GetCents() << " cents." << std::endl;
return 0;
}