April 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 2

Consider the following definition of a polymorphic Stack

template <class T>
class Stack {
private:
  T* TopOfStack;

public:
  Stack(); // Create a new stack.
  void Push(T); // Push an item onto the stack.
  T Pop(); // Remove the item from the top of the stack.
  int Size(); // Return the number of items in the stack.
  int IsEmpty(); // Return 1 if the stack is empty, 0
  otherwise.
};

(a)One form of polymorphism in C++is overloading Explain,with reference to an example,the term overloading [3marks ]

(b)Declare a pointer to the Stack class,called x instantiated with an int type,and show how this could be allocated using the new operator.[2 marks ]

(c)Implement the template function Reverse the signature of which is given below, such that it takes a reference to a stack of type T and reverses the stack.You may use any of the member functions in the class Stack necessary.
template<T>
void Reverse(Stack& T) [5 marks ]

(d)Implement the template function Search the signature of which is given below,such that it takes a reference to a Stack s and an object o and searches the stack for first occurrence of o If found,o should be removed from the stack,leaving the rest of the stack unchanged,otherwise the stack should remain unchanged.You may assume that suitable overloadings of the == operator are available.[5 marks ]