April
1999 QUESTION 1 (Compulsory) Total Marks: 20 Marks |
Click here to access other
questions
GRADE A
|
(a) | State two components of an Abstract Data Type in object-oriented programming. | [2] |
Two
components of an Abstract Data Type:
|
||
(b) | Explain the difference between a class and an object in C++. | [2] |
Class An abstract data type. It contains attributes and methods which define its structure and operations respectively. Object
|
||
(c) | Explain the purpose of a pure virtual function in C++. Give an example declaration of one. | [3] |
Pure virtual functions
allow the association of a name to a function at run-time, rather than compile-time. This
is known as dynamic binding. Example:
|
||
(d) | Consider the following program fragment: class Person { class Telephone_book {
|
|
(i) Explain what a deep copy of objects is in C++ and give an example of when it is most likely to be used. | [2] | |
(ii) Give the implementation of a deep copy constructor for the Telephone_book class. | [5] | |
(i) A deep copy of an
object is when a copy constructor is written, to override the default constructor, that
creates a copy of the data by allocating it a new data block of memory. This results in
separate copies of data for the two objects. It is most likely to be used when a copy of an object is being made through a copy constructor. (ii)
|
||
(e) | Consider the following function in C++; void foo(int *y, int *z) {
|
|
(i) Trace the output of the function foo(&a, &b), assuming that int a = b =0 and the following values are input for x: 1,4,2,1,-9. | [4] | |
(ii) Explain the behaviour of foo. | [2] | |
(i) Ouput: *y = 1; *z = 1 *y = 2; *z = 5 *y = 3; *z = 7 *y = 4; *z = 8 (ii) The behvaiour of foo exhibits that it increments the contents of y and that it sums the values entered of x, until the first negative value of x. |