December 1998
OP216: OBJECT ORIENTED PROGRAMMING

QUESTION 4

Total Marks: 20 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 4

 

(a) You are to design a C++ program for creating and manipulating entries in a telephone book. Consider the following class definition for a Person entry in a telephone book.

class Person {
public:
  char *name;   // the last name of the person in
                   the book
  int number;   // the telephone number of the
                   person

  Person()  {number = 0; name = 0;}  // Constructor
  Person(char *s, int n) {          // Constructor
  number = n;
  name = s;
  }

void display() {   // Display a person
  cout << name << " has number " << number << endl;
  }
};

 

(a) The telephone book is an array of 10,000 Person entries and also an integer that records the number of entries in the telephone book.

Give the definition of a class Telephone_book that contains: (1) the private data items listed above and (2) a default constructor that initialises the number of entries to 0.

 

[3]
(b) Give the implementation of a method display that displays the details of every person in the telephone book.

 

[3]
(c) Give the implementation of a method add_person that reads a last name and a telephone number from the user, creates a new person, and adds the person to the end of the array if there is a space.

 

[8]
(d) Give the implementation of a method find that takes a telephone number and displays the person with the corresponding telephone number. If the telephone number does not exist in the telephone book, give an appropriate error message. [6]