April 1999
OP216: OBJECT-ORIENTED PROGRAMMING

QUESTION 3

Total Marks: 20 Marks

Click here to access other questions

Click to access
SAMPLE STUDENT'S SOLUTIONS
for Question 3

 

(a) One form of Polymorphism in C++ is operator overloading.
(i) Explain, using an appropriate example, the term operator overloading. [3]
(ii) Consider the polymorphic expression 5 == 5.0 in C++. What form of polymorphism is this an example of? Explain your answer.

 

[2]
Consider the declaration of the class String below. You are to write overloaded operator functions for strings.

class String {
public:
   int length();     // Return the length of the
                     // string
   char char_at(int);   // Return the character at
                        // the given position
   void update_at(int, char); // Update the character
                            // at the given position
}

 

(b) Give an implementation of the overloaded operator < that takes two strings by reference and returns 1 if the first is less than the second, or 0 otherwise.

 

[2]
(c) Give the implementation of the overloaded operator == that takes two strings by reference and returns 1 if they are equal, or 0 otherwise.

 

[6]
(d) Give an implementation of the function cmp that takes two strings by reference and returns -1 if the first is less than the second, 0 if they are equal, or 1 is the first is greater than the second. [4]