Tutorial 6 Exercise
Tutorial 6 Exercise
Tutorial 6
2024/09/27 - 2024/10/04
i n t main ( ) {
Animal ∗ myDog = new Dog ;
Animal ∗ myCat = new Cat ;
myDog−>makeSound ( ) ;
myCat−>makeSound ( ) ;
d e l e t e myDog ;
d e l e t e myCat ;
}
2.1 [2 points] The provided program code does not produce the correct output. What will the
output of the main function be?
Page 1 of 2
2.2 Rewrite only the following parts of the program, so that the program code will produce the
correct output:
a) [1 point] The destructor of the Animal class.
b) [2 points] The makeSound function in the Animal class. We wish to guarantee that ev-
ery class derived from the Animal class provides an implementation of the makeSound
function. It also does not make sense for an implementation for the makeSound function
to be provided in the Animal class.
c) [1 point] Once you have made the correction described under b) above, what is the
term used to describe the kind of class that the Animal class is?
2.3 [2 points] Write a function template named add, which receives two parameters of the
same generic template type T, and returns the result of adding these two parameters.
2.4 [3 points] Consider the following classes:
class C o n t a i n e r {
private :
i n t value ;
public :
i n t getValue ( ) const { r e t u r n v a l u e ; }
void s e t V a l u e ( i n t v ) { v a l u e = v ; }
};
Rewrite the Container class to be a class template, where the type of value is the generic
template type T.
Page 2 of 2