April 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 30 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
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 ]
(i)Object oriented programming:
•Allows for more code re-use;(1 mark)
•Which in turn makes code more reliable.(1 mark)
[2 marks ]
(ii) •A class is am implementation of an abstract data type (1 mark)
•An object is an instance of a class.(1 mark)
[2 marks ]
(iii) •Dividing classes into public and private areas provides data hiding (1 mark)
•Restricting access to the object itself or other objects.(1 mark)
(iv) •In pass by value changes made by a function are not re .ected in the caller; (1 mark)
•In pass by reference changes made by a function are reflected in the caller; (1 mark)
•Pass by value is achieved by passing in copies of parameters;(1 mark)
•Pass by reference is achieved by passing in references to (pointers to)
parameters.(1 mark)
(v) •A method is an action a class can perform on itself;(1 mark)
•A message is a request to a class to invoke a method (1 mark)
[2 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 ]
(i)
•*a =1;*b=1 (1 mark)
•*a =7;*b=2 (1 mark)
•*a =10;*b=3 (1 mark)
•*a =20;*b=4 (1 mark)
[4 marks ]
(ii)A sample definition of Sort follows:
int Sort(int* a, int* b) {
  int temp;

  if(*a > *b) {
    temp= *a;
    *a = *b;
    *b= temp;
  }
  return (*a + *b);
}
And the following marking scheme should be used:
•Correctly dereferencing parameters and pointers;(1 mark)
•Testing the object pointed to by a is less than that pointed to by b (1 mark)
•Correctly swapping the parameters around if they are not.(1 mark)
•Returning the total of the two parameters.(1 mark)
[4 marks ]
(iii)A sample definition of CharCount follows:
int CharCount(char* s, char c) {
int i, count;
  i= 0;
  count= 0;
  while(s[i]!= ’\0’)
    if(s[i]== c) count++;
  return count;
}
And the following marking scheme should be used:
•Declaring and initialising a local variable to hold the count;(1 mark)
•Declaring a loop over the string,bound by NULL (1 mark)
•Testing the character pointed to on each iteration to see if it matches the one of interest;(1 mark)
•Returning the count after the whole string has been examined.(1 mark) [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 ]
(i)A sample definition of MyClass follows:
class MyClass {
public:
  MyClass() {};
  ˜MyClass() {};
private:
  float MyNumbers[100];
}
And the following marking scheme should be used:
•A correctly structured class definition;(1 mark)
•A correct class constructor;(1 mark)
•A correct class destructor;(1 mark)
•A correct array declaration.(1 mark)
[4 marks ]
(ii)A sample definition of ValAt follows:
inline float MyClass::ValAt(int x) {
  return MyNumbers[x];
}
And the following marking scheme should be used:
•A correct function signature,including the inline definition;(1 mark)
•Correctly returning the element in the array.(1 mark)
[2 marks ]