April 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

Consider the following abstract data type for a String type:

class String {
private:
  char* s;
  int n;
public:
  String(int length, char* c) // Create the string
  int Strlen() {return n;} // Return the length of the string

  void UpdateAt(int i, char c); s[i]= c;
  }

  char CharAt(int i) {
      if(i>= 0 && i<= Strlen()) return s[i];
  }
};

(a)Explain the difference between deep and shallow copying of objects.[2 marks ]

(b)Implement the member function UpdateAt such that it will update the character in the string at position i with character c if and only if the string is long enough.[3 marks ]

(c)Write a constructor function for the String class which performs a deep copy of the object.You may not use any library functions.[5 marks ]

(d)Write a function,called Reverse the signature of which is given below,which takes a String object and returns a new String which is the reverse of the original object. You may not use any library functions.[5 marks ]