April
1999 QUESTION 4 Total Marks: 20 Marks |
Click here to access other
questions
GRADE A
|
(a) | Explain what is meant by inheritance in object-oriented programming and give one benefit of it. | [3] |
Inheritance It is the derivation of a new data type based on an existing data type. For instance, a derived class can be created according to a base class. Benefit
|
||
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] |
class Mail { private: int postage_cost; char *type_of_mail; public: int access_cost() { return postage_cost;} void display() { cout << "Type of mail: " << type_of_mail << endl; cout << "Cost: $" << postage_cost; } };
|
||
(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] |
class Package:
public Mail { private: int weights; public: Package(int a): Package.weight(a), Package.postage_cost, Package.type_of_mail, { weight = a; type_of_mail = "Package"; postage_cost = 350; } };
|
||
(d) | A mailbox is a collection of mail items.
Consider the following declaration of the class Mailbox. class MailBox { 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] |
Assumption: Th
collection of mail items is stored in an array of type Mail. Declaration: Mail array[100]; int MailBox::total_postage(MailBox &a) |