0% found this document useful (0 votes)
11 views

Tutorial 6 Exercise

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Tutorial 6 Exercise

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COS110 (Program Design: Introduction)

Tutorial 6
2024/09/27 - 2024/10/04

Question 1 Theoretical Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (4 marks)


1.1 [2 points] Discuss/explain the difference between virtual and pure virtual functions.
1.2 [2 points] How many versions of a template class are created if objects of the class are
created for the template types int and float?
Question 2 Practical Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (11 marks)
Consider the following classes:
class Animal {
public :
Animal ( ) {}
˜ Animal ( ) { s t d : : c o u t << " Animal destructor called " << s t d : : e n d l ; }
void makeSound ( ) const { s t d : : c o u t << "No sound !" << s t d : : e n d l ; }
};

class Dog : public Animal {


public :
Dog ( ) {}
˜ Dog ( ) { s t d : : c o u t << "Dog destructor called " << s t d : : e n d l ; }
void makeSound ( ) const { s t d : : c o u t << "Woof!" << s t d : : e n d l ; }
};

class Cat : public Animal {


public :
Cat ( ) {}
˜ Cat ( ) { s t d : : c o u t << "Cat destructor called " << s t d : : e n d l ; }
void makeSound ( ) const { s t d : : c o u t << "Meow!" << s t d : : e n d l ; }
};

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

You might also like