April 1999
OP216: OBJECT-ORIENTED PROGRAMMING

QUESTION 4

Total Marks: 20 Marks

Click here to access other questions

Click to access
SAMPLE STUDENT'S SOLUTIONS
for Question 4

 

(a) Explain what is meant by inheritance in object-oriented programming and give one benefit of it.

 

[3]
Singapore Post requires a program to simulate its postal system. You are to write such a program.
(b) An item of mail contains a postage cost (an integer representing cents) and the type of the mail (a pointer to an array of characters). Give an implementation of the class Mail that contains: (1) these private data items; (2) an accessor for the postage cost; and (3) a method display that prints the mail and the postage cost in the form $x.xx, e.g. the cost 100 would be displayed as $1.00.

 

[5]
(c) A package is an item of mail that has postage $3.50 and a integer weight. Use inheritance to implement the class Package that contains a constructor that takes a weight and initialises all its variables appropriately.

 

[6]
(d) A mailbox is a collection of mail items. Consider the following declaration of the class Mailbox.

class MailBox {
private:
...                // Some private declarations
public:
   MailBox();       // The constructor
   Mail current(); // Returns the current mail item
                    // in the collection
   void next();     // Move to the next item in the
                    // collection
    int empty();    // Return 1 if all the mail items
                    // have been looked at, or 0
                    // otherwise.

Give the implementation of a function total_postage that takes a reference to a mailbox and returns the total postage of all mail items in it.

[6]