explain why "this->dollars = dollars_part; this->dollars += cents_part / 100; this->cents = cents_part % 100;" is used
I am trying to understand a part of a code
code
// To do: Implement class Money constructor
// (Hint: Use either : initializer form or assignment,
// EXTRA CREDIT to incorporate cents over 99)
Money::Money(int dollars_part, int cents_part)
{
this->dollars = dollars_part;
this->dollars += cents_part / 100;
this->cents = cents_part % 100;
}
// Return true if the invoking object has less value than m
bool Money::isLessThan (const Money &m)
{
if (this->dollars == m.dollars) {
return this->cents < m.cents;
}
return this->dollars < m.dollars;
}
// Output the Money object
void Money::output()
{
cout << "$" << dollars << ".";
if (cents < 10) {
cout << "0";
}
cout << cents;
}
Can someone explain why "this->dollars = dollars_part;
this->dollars += cents_part / 100;
this->cents = cents_part % 100;" is used when its says "To do: Implement class Measurement constructor
// (Hint: Use either : initializer form or assignment,"?
A constructor is a special member function of a class, whose name is similar to the class name and calls automatically when the object of the class is created. Generally. a constructor is used to initializing the class data member by passing values through objects.
this pointer is a compiler-generated pointer. It is an implicit parameter to all member functions. It is used to refer to the invoking object.
Step by step
Solved in 2 steps