August 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 5

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 5

(a) Briefly explain the difference between virtual functions and overloading. [2 marks]

(b) Consider the following definition of the class String:
class String {
  char* str;
  int length;
  public:
  String();
  String(char s[]);
  int Length();
  char* CharAt(int index);
}
(i) Add to the class a member function, called UpperCase, the signature of which is given below, which converts all of the lower case elements of the string to upper case, leaving upper case characters untouched. You may wish to use the library function ToUpper, the signature of which is given below, which takes in a single character and returns its upper case equivalent. You are not required to implement ToUpper.
char ToUpper(char* x);
void String::UpperCase(); [3 marks]
(ii) Implement the function, StrCaseCompare, the signature of which is given below, which takes in two references to string objects and returns the result of comparing the two string objects, ignoring the case of respective characters. If the two strings are the same irrespective of case, the function should return 1, or 0 otherwise.
int StrCaseCompare(String a, String b); [5 marks]
(iii) Give an overloading of the operator ==, such that it compares the two strings given to it as operands, and returns a pointer to the first character in the strings which differ. Note that this test should not be case independent. [5 marks]