previous | start | next

Self Test, problem #9

  1. The class Money has two data members: dollars and cents. A function that overloads operator+ that adds two Money values and returns a Money value representing their sum is to be written. If this function is declared in the Money class at (*) below, which of the declarations shown would be correct?

                class Money
                {
                   int dollars;
                   int cents;
                public:
                   Money();
                   Money(int d, int c);
    
                   // (*) Declare an overloaded operator+
                   ...
                };
             
    
                (1) Money operator+(const Money& m) const;
                (2) Money operator+(const Money& m1, const Money& m2);
                (3) friend Money operator+(const Money& m1, const Money& m2);
                (4) friend Money operator+(const Money& m1, const Money& m2) const;
             
    
    1. Any one of these would be correct
    2. Only (1)
    3. Only (2)
    4. Only (1) or (3)
    5. Only (2) or (3)
    6. Only (1) or (4)
    7. Only (2) or (4)

 



previous | start | next