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

14

Click here to load reader

description

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

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

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

1

CISC181 Introduction to Computer Science

Dr. McCoy

Lecture 26Clicker QuestionsDecember 3, 2009

Page 2: 1 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:+

2

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

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.

3

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

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

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

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

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

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.

6

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

An overloaded + operator takes a class object 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.

7

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

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:

8

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

(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)9

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

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 given10

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

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 )

11

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

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.

12

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

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

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

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!

14