August 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 4

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 4

A sales manager requires a program to maintain details of his customers. He wishes to split his customers up into four categories, depending on their location, which can be one of Singapore, India, Malaysia or China. In addition to this, for each customer, he wishes to store their name, which is a string, the customer’s average expenditure, in Singapore Dollars, which is an integer, and a transaction record, which is an array of objects of type Transaction.

(a) Define a suitable enumerated type, called Location, which can take on any of the values of location given above. [1 mark]
(a) typedef enum Location { Singapore, India, Malaysia, China } ; [1 mark]

(b) Define a class, called Customer, which can be used to store all of the customer’s details given above. [5 marks]
(b) A sample definition of Customer follows:
class Customer {
  public:
  string name;
  int expenditure;
  Transaction trans[];
  Location loc;
}
And the following marking scheme should be used:
• Correct class structure; (1 mark)
• Declaring the name to be of type string (char* is not acceptable); (1 mark)
• Declaring the expenditure; (1 mark)
• Declaring an array of transactions; (1 mark)
• Declaring the location; (1 mark)
[5 marks]

(c) Add to your class an inline method, called GetName which returns a copy of a customer’s name. [3 marks]
(c) A sample definition of the GetName follows:
inline string Customer::GetName() {
  string *t = new string(&name);
  return *t;
}
And the following marking scheme should be used:
• Declaring a new string object; (1 mark)
• Copying the name into the new object; (1 mark)
• Returning the new object; (1 mark)
[3 marks]

(d) Briefly explain what is special about an inline method in C++, and give an example of where it may be useful to declare a method inline. [2 marks]
(d) • If a method is declared inline, when the program is compiled, the method body is inserted directly into the program where it is called; (1 mark)
• It should be used when the overhead of calling a function or method is a greater cost than the work that function or method attempts to do; (1 mark)
[2 marks]

(e) Implement a constructor for the Customer class, such that it takes as parameters the customer’s location, the customer’s name, the customer’s average expenditure and the customer’s Transaction record, and initialises them appropriately. [4 marks]
(e) A sample definition of the constructor follows:
Customer::Customer(Location l, int e, Transaction t[]) :
loc(l), expenditure(e), trans(t) {};
And the following marking scheme should be used:
• A correct constructor signature; (1 mark)
• A correct initializer for the location; (1 mark)
• A correct initializer for the expenditure; (1 mark)
• A correct initializer for the transactions; (1 mark)
[4 marks]