Exam 8: Operator Overloading,friends,and References
Exam 1: C++ Basics37 Questions
Exam 2: Flow of Control33 Questions
Exam 3: Function Basics30 Questions
Exam 4: Parameters and Overloading31 Questions
Exam 5: Arrays32 Questions
Exam 6: Structures and Classes37 Questions
Exam 7: Constructors and Other Tools32 Questions
Exam 8: Operator Overloading,friends,and References31 Questions
Exam 9: Strings37 Questions
Exam 10: Pointers and Dynamic Arrays29 Questions
Exam 11: Separate Compilation and Namespaces35 Questions
Exam 12: Streams and File IO43 Questions
Exam 13: Recursion40 Questions
Exam 14: Inheritance30 Questions
Exam 15: Polymorphism and Virtual Functions34 Questions
Exam 16: Templates27 Questions
Exam 17: Linked Data Structures30 Questions
Exam 18: Exception Handling29 Questions
Exam 19: Standard Template Library46 Questions
Exam 20: Patterns and Uml22 Questions
Select questions type
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:
True
Which operators can be overloaded only as non-static class members?
Free
(Short Answer)
4.8/5
(39)
Correct Answer:
= [] -> 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
{
public:
Money( );
// other constructors
// other public members
int getCents( )const;
int getDollars( )const;
private:
int dollars;
int cents;
// other private members
};
Free
(Essay)
4.8/5
(35)
Correct Answer:
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)
{
int dollars1 = amt1.getDollars( );
int dollars2 = amt2.getDollars( );
int cents1 = amt1.getCents( );
int cents2 = amt2.getCents( );
return ((dollars1 < dollars2)||
((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)
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)
An overloaded operator= function must be a non-static class member.
(True/False)
4.8/5
(36)
Showing 1 - 20 of 31
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)