Chapter 15

C++ Programming: From Problem Analysis to Program Design · 22 exercises

Problem 1

Mark the following statements as true or false. a. In \(\mathrm{C}++,\) all operators can be overloaded for user-defined data types. b. In \(\mathrm{C}++,\) operators cannot be redefined for built-in types. c. The function that overloads an operator is called the operator function. d. \(\mathrm{C}++\) allows users to create their own operators. e. The precedence of an operator cannot be changed, but its associativity can be changed. f. Every instance of an overloaded function has the same number of parameters. g. It is not necessary to overload relational operators for classes that have only int member variables. h. The member function of a class template is a function template. i. When writing the definition of a friend function, the keyword friend must appear in the function heading. j. Templates provide the capability for software reuse. k. The function heading of the operator function to overload the preincrement operator \((++)\) and the post-increment operator \((++)\) is the same because both operators have the same symbols.

11 step solution

Problem 2

What is a friend function?

4 step solution

Problem 3

What is the difference between a friend function of a class and a member function of a class?

5 step solution

Problem 5

Suppose that the operator \(<<\) is to be overloaded for a user-defined class mystery. Why must \(<<\) be overloaded as a friend function?

5 step solution

Problem 6

Suppose that the binary operator \(+\) is overloaded as a member function for a class strange. How many parameters does the function operator+ have?

3 step solution

Problem 7

When should a class overload the assignment operator and define the copy constructor?

4 step solution

Problem 10

Find the error(s) in the following code: class mystery //Line 1 \\[ \\{ \\] bool operator<=(mystery) ; //Line 2 \(y\) bool mystery: : \(<=\) (mystery rightobj) //Line 3 \\[ \begin{array}{l} \\{ \\ \\} \end{array} \\]

4 step solution

Problem 11

Find the error(s) in the following code: class mystery //Line 1 \\{ bool operator<= (mystery, mystery); //Line 2 \\[ y \\]

3 step solution

Problem 12

Find the error(s) in the following code: class mystery //Line 1 \\{ friend operator+(mystery); //Line 2 //overload binary + . . . };

1 step solution

Problem 13

In a class, why do you include the function that overloads the stream insertion operator, \(<<,\) as a friend function?

4 step solution

Problem 14

In a class, why do you include the function that overloads the stream extraction operator, \(>>,\) as a friend function?

4 step solution

Problem 17

What is the purpose of a dummy parameter in a function that overloads the post-increment or post-decrement operator for a class?

4 step solution

Problem 19

How many parameters are required to overload the pre-increment operator for a class as a member function?

5 step solution

Problem 20

How many parameters are required to overload the pre-increment operator for a class as a member function?

4 step solution

Problem 21

How many parameters are required to overload the pre-increment operator for a class as a friend function?

4 step solution

Problem 22

How many parameters are required to overload the post-increment operator for a class as a friend function?

4 step solution

Problem 25

Find the error(s) in the following code: template //Line 1 class strange //Line 2 { . . . }; strange s1; //Line 3 strange s2; //Line 4

4 step solution

Problem 26

Consider the following declaration: template class strange { . . . private: type a; type b; }; a. Write a statement that declares sObj to be an object of type strange such that the private member variables a and b are of type int. b. Write a statement that shows the declaration in the class strange to overload the operator == as a member function. c. Assume that two objects of type strange are equal if their corresponding member variables are equal. Write the definition of the function operator== for the class strange, which is overloaded as a member function.

3 step solution

Problem 27

Consider the definition of the following function template: template Type surprise(Type x, Type y) { return x + y; } What is the output of the following statements? a. cout << surprise(5, 7) << endl; b. string str1 = "Sunny"; string str2 = " Day"; cout << surprise(str1, str2) << endl;

3 step solution

Problem 28

Consider the definition of the following function template: template Type funcExp(Type list[], int size) { int j; Type x = list[0]; Type y = list[size - 1]; for (j = 1; j < (size - 1)/2; j++) { if (x < list[j]) x = list[j]; if (y > list[size – 1 – j]) y = list[size – 1 – j]; } return x + y; } Further suppose that you have the following declarations: int list[10] = {5, 3, 2, 10, 4, 19, 45, 13, 61, 11}; string strList[] = {"One", "Hello", "Four", "Three", "How", "Six"}; What is the output of the following statements? a. cout << funExp(list, 10); b. cout << funExp(strList, 6) << endl;

6 step solution

Problem 29

Write the definition of the function template that swaps the contents of two variables.

5 step solution

Problem 30

a. Overload the operator + for the class newString to perform string concatenation. For example, if \(s 1\) is "Hello " and \(s 2\) is "there", the statement: \(s 3=s 1+s 2 i\) should assign "Hello there" to s3, in which \(s 1, s 2,\) and \(s 3\) are newString objects. b. Overload the operator \(+=\) for the class newString to perform the following string concatenation. Suppose that \(s 1\) is "Hello " and s2 is "there". Then, the statement: \(s 1+s 2\) should assign "Hello there" to s1, in which s1 and s2 are newString objects.

7 step solution

Show/ page
Chapter 15 - C++ Programming: From Problem Analysis to Program Design Solutions | StudyQuestionHub