December 1999
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 2

 
template <class T>
class Queue {
private:
  //Some private declarations
public:  
  Queue();  
  void AddToBack(T); //Add an item to the end of the queue.
  T RemoveFromFront(); //Remove an item from the front of the queue.
  int Length(); //Return the length of the queue.
  int Empty(); //Return 1 if the queue is empty,otherwise return 0.
};  

 

 
(a)

Template classes are an example of polymorphism .Explain what is meant by polymorphism .

 

[2]
(b)

Declare a pointer to an object of type Queue ,with an integer template type.

 

[1]
(c)

Show how this object can be allocated using the new operator.

 

[1]
(d)

Write a polymorphic iterative function,called RemoveItemN ,the signature of which is given below,which takes a reference to a queue Q and an integer x ,and will remove and return the x ’th item from the queue Q .The rest of the queue should remain unchanged.You should assume that counting queue elements starts at zero,and you may use the functions which have been declared in the class Queue .

template <class T>
T RemoveItemN(Queue<T>&Q);

 

[6]
(e)

Write a polymorphic recursive function,called Reverse ,the signature of which is given below,which takes a reference to a queue Q and reverses the queue.To reverse a queue,items should be recursively removed from the queue,and then added to the queue as the recursive stack is unwound.You may use the functions which have been declared in the class Queue .

template <class T>
T Reverse(Queue<T>&Q);

[5]