April 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 3

Total Marks: 15 Marks

Click here to access other questions

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

Singapore Shoe Shop require a program to maintain details of their stock.They stock three main types of shoes:sandal trainer and boot. Every item of footwear has a size which is an integer in Singapore Dollars,and a manufacturer which is a string.

(a)Define a suitable enumerated type,called ShoeType which can take on any of the three types of shoe.[1 mark ]
typedef enum ShoeType = {sandal, trainer, shoe};[1mark ]

(b)Declare a class Shoe used to store the details of a particular pair of shoes,which contains the following:
•The three private members above:type size and manufacturer
•A constructor function,which takes as a parameter the type of the shoe and
initialises the member appropriately;
•The signature of accessor functions,which returns each of the three private
members.
[5 marks ]
A sample definition of Shoe follows:
class Shoe {
private:
  ShoeType type;
  int Size;
  String manufacturer;

public:
  Shoe(ShoeType t) {
    type= t;
  }

  ShoeType GetType();
  Size GetSize();
  char* GetManufacturer();
}
And the following marking scheme should be used:
•A correct constructor function with parameter list;(1 mark)
•Which correctly initialises the private member type (1 mark)
•Correct private member ariables;(1 mark)
•Suitable accessor methods for the members;(1 mark)
•Correctly structured class.(1 mark)
[5 marks ]

(c)The shop wishes to keep a record of all the shoes in stock.Give a definition of a class Stock which contains two members,a dynamic array of Shoe objects,called CurrentStock and an integer n specifying the size of the array.[2 marks ]
A sample definition of Stock follows:
class Stock {
public:
  Shoe* CurrentStock;
  int SizeOfCurrentStock;
}
And the following marking scheme should be used:
•A correct public dynamic array definition;(1 mark)
•A correct public integer definition;(1 mark)
[2 marks ]

(d)The manager of the shop decides he would like to be able to search his stock records for particular shoes.Give a definition of the function Search the signature of which is given below,which takes four parameters,a Stock object to be searched,the type of the shoe and the size of the shoe to be searched for,and a reference to a Shoe which is used to return a reference to the first shoe found which matches the criteria.
void Search(Stock& CS, int size, ShoeType type, Shoe& result); [5 marks ]
A sample definition of Search follows:
void Search(Stock& CS, int size, ShoeType type, Shoe& result) {
  for(i= 0; i< CS.SizeOfCurrentStock; i++) {
    if (CS[i].GetSize() == size && CS[i].GetType()== type)) {
      result= CS.[i];
      return;
    }
  }
  return;
}
And the following marking scheme should be used:
•Declaring an iterative structure bound by CS.SizeOfCurrentStock (1 mark)
•Comparing the current array element with the parameters passed in on each
iteration;(1 mark)
•Correctly accessing the size of the object being compared by using GetSize();
(1 mark)
•Correctly accessing the type of the shoe being compared by using GetType();
(1 mark)
•Copying and returning the current reference when a match is found.(1 mark)
[5 marks ]

(e)Assuming the Search function above was required to return a copy of the object found,instead of a reference to it,identify the type of function which would be best suited to perform this operation,and give an example signature for it.[2 marks ]
•A Copy constructor (1 mark)
•Shoe(Shoe& s);;(1 mark)
[2 marks ]