December 1999
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 confectionery shop requires a system to maintain details of products which it vends.You are to implement such a system.

 

 
(a)

Identify the difference between private and protected members of a class.

•Private members can only be accessed by other members and friends of the class;(1 mark)
•Protected members can also be accessed by members and friends of derived classes.(1 mark)

 

[2]
(b)

An item of confectionery contains a calorie count (an integer)and a name (a pointer to an array of characters). Declare the class Confectionery that contains:

•These private data items;
•An accessor method called Calories ,which returns the calorie count;
•A method,called Display ,the signature of which is given below,which prints out the name and the calorie count on a single line.
void Display();

A sample definition of Confectionery follows:

class Confectionery {
protected:
  int calories;
char*name;
public:  
  int Calories(){return calories;}
void Display(){
cout <<name <<""<<Calories()<<endl;
}
};  

And the following marking scheme should be used:
•Correct private member variables calories and name ;(1 mark)
•Definition of Calories ;(1 mark)
•Definition of Display ;(1 mark)

 

[3]
(c)

Minstrels are a type of confectionery that have a calorie count of 100,and an integer value specifying the number of minstrels in a packet.Using inheritance,declare a class Minstrels ,which contains a constructor which takes a number of Minstrels and initialises all of its variables appropriately.

A sample definition of Minstrels follows:

class Minstrels :public Confectionery {
private:
  int NumberInPacket;
public:  
  Minstrels(int x){
calories=100;
name="Minstrels";
NumberInPacket=x;
}
};  

And the following marking scheme should be used:
•Use of public inheritance from Confectionery ;(1 mark)
•Declaration of NumberInPacket ;(1 mark)
•Initialising calories in the constructor;(1 mark)
•Initialising name in the constructor;(1 mark)
•Initialising NumberInPacket in the constructor;(1 mark)

 

[5]
(d)

A SelectionBox is a collection of di .erent types of Confectionery .Consider the following de .nition of the class SelectionBox :

class SelectionBox {
private:
//Some private declarations
public:
SelectionBox(); //Return current confectionery item in list
Confectionery current(); //Move to next confectionery item in list
void next(); //Return 1 if all items have been looked
int empty(); //at,0 otherwise
     
}    

Write a function,called TotalCalories ,the signature of which is given below,which takes a reference to a SelectionBox object and returns the sum of all the Calories which are contained within that SelectionBox .
int TotalCalories(SelectionBox&s);

A sample definition of TotalCalories is given below:

int TotalCalories(SelectionBox&s)
{
  int total=0;
for(;!(s.empty());s.next())
total+=s.current().Calories();
return total;
}  

And the following marking scheme should be used:
•Declaring a suitable iterative structure;(1 mark)
•Bound by the non empty list condition;(1 mark)
•With a suitable s.next()increment;(1 mark)
•Correctly incrementing the total on each iteration;(1 mark)
•Returning the total when the loop has terminated.(1 mark)

[5]