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...

=IF(AND(D7="manager",F7

Indicate whether the statement is true or false

Computer Science & Information Technology

When you purchase or download a(n) ____ for an object-oriented programming language, it comes packaged with many predefined, built-in classes.

A. compiler B. IDE C. sunny day case D. interpreter

Computer Science & Information Technology

In the figure above, the number _____________________ refers to the slider you would use to enlarge or reduce the thumbnail size.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

The Hyperlink data type is assigned to fields that contain links to Web pages and email addresses that open when clicked. _________________________

Answer the following statement true (T) or false (F)

Computer Science & Information Technology