August 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

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

Consider the following definition of a polymorphic queue, and the class PrintJobs:
template <class T>
class Queue {
  private:
  int size;
  T *first, *last;
  public:
  void Add(T); // Add item to the back of the queue
  T Remove(); // Remove and return the item at the front of the queue
  int Size(); // Return the number of elementes in the queue
}
class PrintJobs {
  char* JobName;
  void* Job;
  PrintJobs* next;
}
(a) Explain what is meant by a template class in C++, and give an example of where it may be useful to employ a template class. [2 marks]
(a) • A template class is a parameterizable component of a class definition; (1 mark)
• It should be used where a particular class can be used with a number of different component types. (1 mark)

(b) Construct an object MyQueue, which is an instantiation of the Queue class with template type PrintJobs. [2 marks]
(b) Queue MyQueue<PrintJobs>;
• Instantiating MyQueue; (1 mark)
• With the correct template; (1 mark)

(c) Implement the function Add, such that it takes an object of template type T and adds it to the back of the queue. [4 marks]
(c) A sample definition of Add follows:
void Queue::Add(<T>& datum) {
  last.next = datum;
  datum.next= null;
  last= datum;
}
And the following marking scheme should be used:
• The function signature, with the correct type and reference; (1 mark)
• Updating the next pointer in the last element in the queue; (1 mark)
• Ensuring the next pointer in the new element doesn’t point anywhere inappropriate; (1 mark)
• Updating the last pointer at the end of the queue; (1 mark)
[4 marks]

(d) Implement the function SplitQueue, such that it takes in a reference to a queue and separates it into two queues a and b, such that any PrintJob with the name "priority" is put into queue a, and the others into queue b. [5 marks]
(d) A sample definition of SplitQueue follows:
void SplitQueue(Queue& Q, Queue& a, Queue& b) {
  PrintJobs temp;
  while(Q.Size()!= 0) {
    temp= Q.Remove();
    if(strcmp( temp.JobName, "priority")== 0) {
     a.Add(temp);
     } else {
     b.Add(temp);
      }
   }
}
And the following marking scheme should be used:
• Correct function signature; (1 mark)
• Declaring a local variable to hold the next element in the queue (as first and last are
not accessible); (1 mark)
• Looping over the non empty queue; (1 mark)
• Sensibly comparing the JobName in the current element; (1 mark)
• And adding it to the appropriate queue depending on the results of this comparison; (1 mark)
[5 marks]

(e) Give the name for the form of parameter passing used in the above SplitQueue function, and explain what effect this type of parameter passing has. [2 marks]
(e) • Reference parameters; (1 mark)
• It means that the actual object itself is operated on, not a copy; (1 mark)
[2 marks]