April 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 30 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 1

(a)(i)Identify one advantage of using the object oriented approach to programming, and give a reason why it is an advantage.[2 marks ]
(ii)State the difference between a class and an object [2marks ]
(iii)Give the reasons for dividing classes into public and private
areas.[2 marks ]
(iv)Explain the difference between a method and a message [2marks ]
(v)Describe the difference between passing arguments by value and by reference and explain how both are achieved in C++[4 marks ]

(b)(i)Consider the following definition of the function foo in C++:
void foo(int *a, int *b) {
  int i;
  cin >> i;
  if(i >= 1) {
    (*b)++;
    (*a) += i;
    cout << "*a = " << *a << ";" << " *b= " << *b << endl;
    foo(a, b);
  }
}
Trace the output of calling foo(&a, &b);,assuming a= b= 1 and the following
numbers are input in order:1 6 3 10 0 [4marks ]
(ii)Write a function,called sort the signature of which is given below,which takes a reference to two integer parameters.The procedure should ensure that the parameters are sorted in ascending order,i.e,the first parameter is smaller than the second,and should return the total of the two parameters.
int sort(int* a, int* b); [4 marks ]
(iii)Write an iterative function,called CharCount the signature of which is given
below,which takes two parameters,a null terminated string s and a character c
The function should return the number of times c appears in s
int CharCount(char s[], char c); [4 marks ]

(c)(i)Write a class,called MyClass which contains the following:
•A default constructor;
•a default destructor;
•A protected array of real numbers,100 elements long.
[4 marks ]
(ii)Add to your class an inline function ValAt which takes a single integer
parameter,and returns the alue stored in the array at that index.[2 marks ]