which of the following operations are legal? If so, why? If not, why not?

We reproduce the class Money here, in part:
```
class Money
{
public:
Money( ); Money(int theDollar, int theCents);
Money(int theDollars); Money(double amount); // other public members
int getCents( ) const; int getDollars( ) const; private: int dollars; int cents; // other private members };
```

Note that * is not overloaded in the class, but operator + is overloaded using an operator function with the following declaration:
```
Money const operator+(const Money& amt1, const Money& amt2)
```
The question is, given the declarations,
```
Money baseAmount(100, 60); // $100.60
Money fullAmount;
```

a) BaseAmount + 25;
b) 25 + BaseAmount;
c) baseAmount = 2 * baseAmount;
d) baseAmount+baseAmount.


All except c) are legal.

Part a) The compiler notes the left hand argument for + is a Money object, and the right hand object is an int. It looks for an operator overloading with a Money left hand argument. It finds the overloading with two Money parameters. So it then looks for a way to convert an int object to a Money object. It finds the Money(int) constructor, which it uses. Then it has the argument-parameter type match necessary to invoke the operator+(const Money&,const MoneyADD&) overloaded operator.
Part b) is quite similar to a)
Part c) is illegal, not because we couldn’t overload operator*, but because we did not do so.
Part d) is a straightforward call to operator+(baseAmount,baseAmount)

Computer Science & Information Technology

You might also like to view...

Ubuntu's GUI is known by what name?

A) Xfce B) Unity C) GNOME D) KDE

Computer Science & Information Technology

Both “ignoring the exception” and “aborting the program” are error-handling techniques that:

Cannot be used if the error is fatal. b. Always result in a resource leak. c. Should not be used for mission-critical applications. d. Allow program execution to proceed as if no error had occurred.

Computer Science & Information Technology