1 / 14

CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009

CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009. The correct function name for overloading the addition (+) operator is (a) operator+ (b) +operator (c) operator(+) (d) operator:+. To use an operator on class objects, operator overloading

dianeartis
Télécharger la présentation

CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CISC181 Introduction to Computer ScienceDr. McCoyLecture 26Clicker QuestionsDecember 3, 2009

  2. The correct function name for overloading the addition (+) operator is (a) operator+ (b) +operator (c) operator(+) (d) operator:+

  3. To use an operator on class objects, operator overloading (a) must always be used. (b) must always be used, with two exceptions. (c) must never be used, with two exceptions. (d) must never be used.

  4. Which statement about operator overloading is false? (a) New operators can never be created. (b) Certain overloaded operators can change the number of arguments they take. (c) The precedence of an operator cannot be changed by overloading. (d) Overloading cannot change how an operator works on built-in types.

  5. To overload the += operator (a) only the + operator needs to be overloaded. (b) only the + and = operators need to be overloaded. (c) the += operator must be explicitly overloaded. (d) the + and = operators need to be overloaded implicitly.

  6. Which situation would require the use of a non-member overloaded operator? (a) The overloaded operator is =. (b) The left most operand must be a class object (or a reference to a class object) of the operator’s class. (c) The left operand is an int. (d) The operator returns a reference.

  7. An overloaded + operator takes a classobject and a double as operands. For it to be commutative (i.e., a + b and b + a both work), (a) operator+ can be a member function of the class from which the objects are instantiated. (b) operator+ must be a non-member function. (c) the operator+ function that takes the object as the left operand can be a member function, and the other operator+ must be a non-member function. (d) both operator+ functions must be non-member friend functions of the class.

  8. Suppose you have a programmer-defined data type Data and want to overload the << operator to output your data type to the screen in the form cout << dataToPrint; and allow cascaded function calls. The first line of the function definition would be:

  9. (a) ostream &operator<<(ostream &output, const Data &dataToPrint) (b) ostream operator<<(ostream &output, const Data &dataToPrint) (c) ostream &operator<<(const Data &dataToPrint, ostream &output) (d) ostream operator<<(const Data &dataToPrint, ostream &output)

  10. Suppose the unary ! operator is an overloaded member function of class String. For a String object s, which function call is generated by the compiler when it finds the expression !s? (a) s.operator!() (b) s.operator!(default value1, default value2,...) (c) s->! (d) a compiler error results because no arguments are given

  11. y and z are user-defined objects and the += operator is an overloaded member function. The operator is overloaded such that y += z adds z and y, then stores the result in y. Which of the following expressions is equivalent to y += z? (a) y = (y.operator+=) + (z.operator+=) (b) y.operator+=( z ) (c) y = y + z (d) y.operator+=( z ) = y.operator+=( z ) + z.operator+=( z )

  12. A copy constructor (a) is a constructor with only default arguments. (b) is a constructor that initializes a newly declared object to the values of an existing object of the same class. (c) is a constructor that takes no arguments (d) none of the above.

  13. Copy constructors must use call by reference because (a) otherwise the constructor will only make a copy of a pointer to an object. (b) otherwise infinite recursion occurs. (c) the copy created using call-by-value has function scope. (d) the pointer needs to know the address of the original data, not a temporary copy of it.

  14. NOTE: I misspoke in class about this! Copy constructors must use call by reference because (b) otherwise infinite recursion occurs. NOTE: the copy constructor is going to be called any time there needs to be a copy of the object – like when it is a call-by-value argument to a function!

More Related