Chapter 8 (Operator Overloading) - Questions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Repeat Question 8, but allow statements like the following: dist2 = dist1++;

Distance Distance::operator ++ () { int f = ++feet; float i = inches; return Distance(f, i); }

When used in prefix form, what does the overloaded ++ operator do differently from what it does in postfix form?

It increments the variable prior to use, the same as a non-overloaded ++operator.

Write the complete definition of an overloaded ++ operator that works with the String class from the STRPLUS example and has the effect of changing its operand to uppercase. You can use the library function toupper() (header file CCTYPE), which takes as its only argument the character to be changed and returns the changed character (or the same character if no change is necessary).

String String::operator ++ () { int len = strlen(str); for(int j=0; j<len; j++) str[j] = toupper( str[j] ) return String(str); }

Navigability from class A to class B means that a. an object of class A can call an operation in an object of class B. b. there is a relationship between class A and class B. c. objects can go from class A to class B. d. messages from class B are received by class A.

a

Operator overloading is a. making C++ operators work with objects. b. giving C++ operators more than they can handle. c. giving new meanings to existing C++ operators. d. making new C++ operators.

a, c

True or false: The compiler won't object if you overload the * operator to perform division.

true, but it will be hard for humans to understand

In the UML, member data items are called _________ and member functions are called ___________.

attributes, operations

To convert from a basic type to a user-defined class, you would most likely use a. a built-in conversion operator. b. a one-argument constructor. c. an overloaded = operator. d. a conversion operator that's a member of the class.

b

When you overload an arithmetic assignment operator, the result a. goes in the object to the right of the operator. b. goes in the object to the left of the operator. c. goes in the object of which the operator is a member. d. must be returned.

b, c

Assume a class C with objects obj1, obj2, and obj3. For the statement obj3 = obj1 - obj2 to work correctly, the overloaded - operator must a. take two arguments. b. return a value. c. create a named temporary object. d. use the object of which it is a member as an operand.

b, d

Here are two declarators that describe ways to add two string objects: void add(String s1, String s2) String operator + (String s) Match the following from the first declarator with the appropriate selection from the second: function name (add) matches _________. return value (type void) matches _________. first argument (s1) matches _________. second argument (s2) matches _________. object of which function is a member matches _________. a. argument (s) b. object of which operator is a member c. operator (+) d. return value (type String) e. no match for this item

c, e, b, a, d

If objA is in class A, and objB is in class B, and you want to say objA = objB;, and you want the conversion routine to go in class A, what type of conversion routine might you use?

constructor

In a UML class diagram, an association arises whenever a. two classes are in the same program. b. one class is descended from another. c. two classes use the same global variable. d. one class calls a member function in the other class.

d

To convert from a user-defined class to a basic type, you would most likely use a. a built-in conversion operator. b. a one-argument constructor. c. an overloaded = operator. d. a conversion operator that's a member of the class.

d

True or false: rectangles that symbolize classes have rounded corners.

false

True or false: The statement objA=objB; will cause a compiler error if the objects are of different classes.

false if there is a conversion routine; true otherwise

How many arguments are required in the definition of an overloaded unary operator?

none

True or false: An overloaded operator always requires one less argument than its number of operands.

true

True or false: If you've defined a constructor to handle definitions like aclass obj = intvar; you can also make statements like obj = intvar;.

true

True or false: The >= operator can be overloaded.

true

Write a complete definition for an overloaded ++ operator for the Distance class from the ENGLPLUS example. It should add 1 to the feet member data, and make possible statements like dist1++;

void Distance::operator ++ () { ++feet; }

Write a complete definition for an overloaded operator for the Counter class of the COUNTPP1 example that, instead of incrementing the count, decrements it.

void operator -- () { count--; }

Assuming that class X includes a routine to overload the - operator, write a statement that would perform the same task as that specified in Question 2.

x3 = x2 - x1;

Assuming that class X does not use any overloaded operators, write a statement that subtracts an object of class X, x1, from another such object, x2, and places the result in x3.

x3.subtract(x2, x1);


Kaugnay na mga set ng pag-aaral

Chapter 14 Personal Finance Review

View Set

Adjectives, Articles, and Adverbs

View Set

LSU BIOL 1202 (HRINCEVICH) CH. 26 HW

View Set

Grokking the System Design Interview

View Set

Customer Service Exam 1: Chapters 1-5

View Set

EA PART II - CORPORATIONS: 11. S Corp (Form 1120S)

View Set

Computer Literature test number one

View Set

Chapter 14: Annuities and Individual Retirement Accounts

View Set