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
4 step solution
Problem 26
Consider the following declaration:
template
3 step solution
Problem 27
Consider the definition of the following function template:
template
3 step solution
Problem 28
Consider the definition of the following function template:
template
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