Exam 8: Operator Overloading,friends,and References

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

Consider this operator overloading for class Money,modified from Display 8.1 by omitting the const modifier on the return type: Money operator+(const Money& amt1,const Money& amt2); Is the following expression legal? If legal,what could it mean,and where does the information that is assigned go? Money m1(17.99),m2(23.57)m3(15,22); (m1 + m2)= m3;

Free
(True/False)
4.9/5
(44)
Correct Answer:
Verified

True

Which operators can be overloaded only as non-static class members?

Free
(Short Answer)
4.8/5
(39)
Correct Answer:
Verified

= [] -> and ().

What do you need to add to the class definition to overload operator < so that it applies to the type Money from Display 8.1? Given this extract from the class Money from Display 8.1 of the text. class Money { \quad public: \quad\quad Money( ); \quad\quad // other constructors \quad\quad // other public members \quad\quad int getCents( )const; \quad\quad int getDollars( )const; \quad private: \quad\quad int dollars; \quad\quad int cents; \quad\quad // other private members };

Free
(Essay)
4.8/5
(35)
Correct Answer:
Verified

For non-member,non-friend operator overloading.add the following declaration and definition as appropriate file.The syntax here is for a definition outside the class.
bool operator < (const Money& amt1,const Money& amt2);
bool operator < (const Money& amt1,const Money& amt2)
{
\quad int dollars1 = amt1.getDollars( );
\quad int dollars2 = amt2.getDollars( );
\quad int cents1 = amt1.getCents( );
\quad int cents2 = amt2.getCents( );
\quad return ((dollars1 < dollars2)||
\quad ((dollars1==dollars2)&&(cents1<NOT<cents2)));
}

If I need to build an object for return from a function,I can construct that function directly in the return statement.

(True/False)
4.8/5
(29)

Consider the class definition: class IntPair { int first; int second; public: IntPair(int firstValue,int secondValue); // prefix operator++ here // postfix operator ++ here int getFirst( )const; int getSecond( )const; }; a)Give declarations for prefix and postfix versions of operator++ b)Give definitions for prefix and postfix versions of operator++

(Essay)
4.9/5
(36)

When overloading an operator,you cannot change the number of arguments an operator takes.

(True/False)
4.8/5
(37)

When overloading an operator,you can create a new operator different from the more usual operators.

(True/False)
4.7/5
(27)

Overloading a binary operator as a stand-alone function requires two arguments.

(True/False)
4.8/5
(34)

C++ allows overloading of the function application operator ( ).Explain.

(True/False)
4.9/5
(52)

Given the class definition: class A { public: //constructors // other members private: int x; int y; }; Give declarations of operator functions for each of the following ways to overload operator + You must state where the declaration goes,whether within the class in the public or private section or outside the class.The operator + may be overloaded a)as friend function b)as member function c)as non-friend,non-member function

(Essay)
4.9/5
(37)

What are some reasons for using friend operator overloading?

(Essay)
4.9/5
(30)

The operator prefix operator ++ is an l-value,and the text asserts that we should emulate this with operator overloads by returning a reference from prefix versions of operator++.

(True/False)
4.9/5
(35)

It is impossible to get short-circuit behavior for overloaded operator && or operator ||

(True/False)
4.9/5
(35)

If a unary operator is overloaded as a stand-alone function,the argument may be any type.

(True/False)
4.9/5
(31)

When overloading an operator,you can change the behavior of the operator,making + do things that feel like multiplication,but this is unwise.

(True/False)
4.8/5
(31)

Explain the similarities and differences between (binary)operators such as +,*,-,/ and a function.

(Essay)
4.7/5
(25)

A friend function has access only to the private members and member functions of the class (and all objects of that class)of which it is a friend.

(True/False)
4.8/5
(37)

One can guarantee left to right evaluation of the arguments to a comma expression overloading.

(True/False)
4.9/5
(43)

Which operators cannot be overloaded?

(Essay)
4.8/5
(33)

An overloaded operator= function must be a non-static class member.

(True/False)
4.8/5
(36)
Showing 1 - 20 of 31
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)