December 1999
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 Cycle Shop requires a program to manage details of all the cycles which they stock.They stock three types of cycle:mountain cycles ,racing cycles and hybrid cycles .All of the cycles they store have a price ,which is an int expressed in Singapore Dollars,and a name ,which is a String .

 

(a)

Define a suitable enumerated type,called CycleType ,which can take on any one of the three values mountain ,racing and hybrid .

typedef enum CycleType {mountain,racing,hybrid};

 

[1]
(b)

Declare a class,called Cycle ,used to store the details of a particular cycle in the store,which contains the following:

•The three private members mentioned above,type ,value and name ;
•A constructor function,which takes the type ,the value and the name as parameters;
•An accessor method,called Value ,which returns the value ;
•An accessor method,called Name ,which returns the name ;
•An accessor method,called Type ,which returns the type ;

A sample definition of CycleType follows:

class Cycle
{ public: Cycle(CycleType type,int price,String name);
int Price();
String Name();
CycleType Type();
   
private:  
  CycleType type;
int value;
String name;
};  

And the following marking scheme should be used:

•A correct constructor function with parameter list;(1 mark)
•Correct private member variables;(1 mark)
•Suitable accessor method for the price ;(1 mark)
•Suitable accessor method for the name ;(1 mark)
•Suitable accessor method for the type .(1 mark)

 

[5]
(c)

The shop wishes to keep a record of all the cycles currently in stock.Give a definition of a class Stock ,which contains two members -a dynamic array of cycles,called CurrentStock ,and an integer specifying the size of the array, SizeOfCurrentStock .

A sample definition of Stock follows:

class Stock {
public:
Cycle*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]
(d)

The shop manager decides that she would like a catalogue of all the cycles,arranged by cycle type.Write a function,called SortCycle ,which takes a reference to a Stock object s ,and three other Stock objects m ,r and h ,and places copies of all of the mountain cycles in m ,the racing cycles in r and the hybrid cycles in h .You can assume that the three objects m ,r and h have su .cient space allocated in their arrays. void SortStock(Stock& s,Stock& m,Stock& r,Stock& h);

A sample definition of SortStock follows:

void SortStock(Stock& s,Stock& m,Stock& r,Stock& h)
{

int i;
int m_index=0;
int r_index=0;
int h_index=0;

  for(i=0;i<s.SizeOfCurrentStock;i++){
if(s.CurrentStock [i ].Type()==mountain){
m.CurrentStock [m_index ]=s.CurrentStock [i ];
m_index++;
   

}else
if(s.CurrentStock [i ].Type()==racing){
r.CurrentStock [r_index ]=s.CurrentStock [i ];
r_index++;
}else
if(s.CurrentStock [i ].Type()==hybrid){
h.CurrentStock [h_index ]=s.CurrentStock [i ];
h_index++;

}

  }  
}.    

 

And the following marking scheme should be used:
•Declaring three local variables to index each of the arrays;(1 mark)
•Declaring a suitable iterative structure;(1 mark)
•Bound by the size of the array;(1 mark)
•Declaring a suitable test for one of mountain ,racing or hybrid .(1 mark)
•Adding the item to the correct array based on the result of the test;(1 mark)
•and incrementing the relevant array counter;(1 mark)
•Correctly using object references and pointers.(1 mark)

[7]