Assignment #8: Constructor for Type Casting
Operator - Complete two classes
USD and Euro to support type casting between Euro and USD and the following C++
statements may run using constructors;.
class USD {
int dollars;
float cents; //
0.0<=cent<100
public:
friend USD operator+(const
USD&, const USD&);
// complete here
};
Class
Euro {
int euros;
float cents; // 0.0<=cent<100
public:
friend Euro operator+(const
Euro&, const Euro&);
// complete here
};
int main()
{
Euro myMoneyEuro;
USD myMoenyUSD;
myMoneyUSD=Euro(10,20.0)+USD(20,20.0);
myMoneyEuro=USD(100,30.0)+Euro(300,20.0);
}
USD
operator+(const USD& a, const USD& b) {
USD temp;
//
complete here. See lecture slides
return temp;
}
Euro
operator+(const Euro& a,
const Euro& b) {
Euro temp;
//
complete here. See lecture slides
return temp;
}
We
assume that 1 USD is 0.81 Euros.
Oral Test at the lab on April 26.
[Hint 1]. To facilitate the conversion, you may use
the following assignment operator.
USD&
USD::operator=(const Euro&
pEuro){
cout
<< "operator USD = Euro" << endl;
float money = (pEuro.getEuros()) * (float)100.0 / (float)81.0;
this->dollars
= (int)money ;
this->cents = pEuro.getCents() * (float)100.0 / (float)81.0 + (money -
this->dollars) * 100;
while
(this->cents >= 100.0){
this->cents -= 100.0;
this->dollars += 1;
}
return *this;
}
[Hint 2]. You may need only one conversion
constructor!