August 1999
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 1

(a) (i) Describe one advantage of using the object oriented approach to programming.

(ii) Describe one disadvantage of using the object oriented approach to programming.

[2]

[2]

(i)
  • New data types can be created by inheriting from existing types;
  • Thus promoting reuse and more reliable code.

 

(ii)
  • Object oriented languages require more run time checks;
  • which can result in a program running slower.

 

(b) (i) Explain the reasons for dividing classes into public and private areas.

(ii) Define a class called Triangle, which contains three private member integer variables, called side_a, side_b and side_c, which represents the sides of the triangle. Your class should also contain a default constructor and a default destructor.

(iii) Assume your Triangle class contains another constructor function, which instead takes three parameters. Give the O-O name for this situation.

 

[3]

[3]


[1]

(i) The distinction provides data hiding
  • as we can restrict access to data and methods
  • to the object itself
  • or to other objects

 

(ii) A sample definition of triangle follows:

class Triangle {
public:
    Triangle ( );

    ~Traingle ( );

private:
    int side_a, side_b, side_c
} ;

 

(iii)
Functions overloading or overloading

 

(c) (i) Explain the differences between passing arguments to a function by value and by reference.

(ii) Write a function which takes an array of characters and an integer specifying the length of the array, and reverses the order of characters in the array.

 

[7]
(i)
  • In passing by value, changes are not reflected in the calling code;
  • In passing by reference, changes are reflected in the calling code;

 

 

 

(ii) A sample definition of reverse follows:

void reverse (char a[ ], int n) {
    for(int i= 0; i< n/2; i+) {
       char m;
       m= a [i];
       a[i]= a[n-i-1];
       a[n-i-1]= m;
    }
}