August
1997 QUESTION 3 Total Marks: 20 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS |
3. | (a) Polymorphism is an important characteristic of object-oriented programming; it can be defined in C++ using virtual functions. Define a virtual function example that takes an integer parameter and returns void. | [3] | |
virtual void ExampleFunction(int); | |||
deduct one mark for each syntax error or omission, minimum zero. | |||
[3 marks] | |||
(b) The use of virtual functions allows dynamic binding. Explain what is meant by dynamic binding and explain how it is different from static binding. | [4] | ||
Dynamic binding is the association of a name to a specific function/item at runtime. | [2] | ||
Static binding is undertaken at compile time, whereas dynamic binding is done at runtime. | [2] | ||
[4 marks] | |||
(c) State one advantage and one disadvantage of dynamic binding. | [2] | ||
Advantages: | |||
Dynamic binding allows more freedom when an algorithm is specified. | |||
It permits the system to automatically determine the right function to call, based on the type of the object, at runtime. | |||
one mark for either of these | |||
Disadvantage: Performance penalty is incurred as it involves an extra lookup at runtime. | [1] | ||
[2 marks] | |||
(d) What is a friend function? | [2] | ||
A friend function is a function declared to have access to the protected and private members of a class. | [2] | ||
(e) In the context of friend functions, give a reasoned answer to the following questions. | |||
(i) if Dog class is a friend of Boy class, is Boy a friend of Dog? | [3] | ||
No | [1] | ||
friendship is not commutative. | [2] | ||
(ii) if Dog is a friend of Boy, and Terrier derives from Dog, is Terrier a friend of Boy? | [3] | ||
No | [1] | ||
friendship is not inherited. | [2] | ||
(iii) if Dog is a friend of Boy, and Boy is a friend of House, is Dog a friend of House? | [3] | ||
No | [1] | ||
friendship is not associative. | [2] | ||
in each case, one mark for a correct answer, and two marks for a suitable explanation | |||
[9 marks] |