April 1999
OP216: OBJECT-ORIENTED PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 20 Marks

Click here to access other questions

Click to access
SAMPLE STUDENT'S SOLUTIONS
for Question 1

 

(a) State two components of an Abstract Data Type in object-oriented programming.

 

[2]
(b) Explain the difference between a class and an object in C++.

 

[2]
(c) Explain the purpose of a pure virtual function in C++. Give an example declaration of one.

 

[3]
(d) Consider the following program fragment:

class Person {
private:
   ...                    // Some private definitions
public:
   Person();               // Constructor
   Person(const Person &); // Copy Constructor
...
};

class Telephone_book {
private:
   int entries; // The number of people in the book
   Person book [10000];  // A book is an array of
                         // 10000 persons.
public:
  ...
}

 

(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]
(e) Consider the following function in C++;

void foo(int *y, int *z) {
   int x;
   cin >> x;
   if(x>=0) {
      (*y)++;
      (*z) += x;
       cout << "*y = " << *y << ";" << " *z = " << *z
            << endl;
       foo(y,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]