December 1999
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)Explain the difference between a class and an object .[2 marks ]
•A class is an implementation of an abstract data type;(1 mark)
•And an object is an instance of a class.(1 mark)

(ii)State what a friend function is in C++.[2 marks ]
•A friend function permits non-member functions of a class;(1 mark)
•To access the private section of that class.(1 mark)

(iii)Give one example of when a friend function may be used.[1 mark ]
Either
•In operator overloading;(1 mark)
•When functions require access to two separate classes;(1 mark)
•Any alternative sensible answer.(1 mark)

(iv)Explain the difference between multiple and single inheritance.
[2 marks ]
•In multiple inheritance ,classes inherit from more than one chain of inheritance;(1 mark)
•In single inheritance ,classes inherit from one and only one chain of inheritance.(1 mark)

(v)Describe what is meant by a pure virtual function in C++.[2 marks ]
A pure virtual function is
•One which is dynamically (run time)bound;(1 mark)
•Allowing different implementations of functions in each derived class. (1 mark)

(vi)Declare a pure virtual function,called foobar ,which takes no parameters and has an int return type.[1 mark ]
virtual int foobar();

 

[10]
(b)

(i)Explain the difference between pass by value and pass by reference parameter passing,and describe how pass by reference is implemented in C++.[3 marks ]
•In pass by value changes made to the value passed in by the called code are not reflected in the calling code;(1 mark)
•In pass by reference changes made to the value passed in by the called code are reflected in the calling code;(1 mark)

•In C++,pass by reference is implemented using pointers.(1 mark)

(ii)Write a function,called avswap ,the signature of which is given below,which takes in two reference parameters x and y ,and returns the average of the two parameters.On exit,the values of x and y should be exchanged.int avswap(int* x, int* y); [5 marks ]
A sample definition of avswap follows:

int avswap(int*x,int*y)
{  
  int temp;
  temp=*x;
  *y=*x;
  x*x=temp;
   
  return (int)((*x)*(*y))/2;
}  

And the following marking scheme should be used:
•Declaring a local temporary variable;(1 mark)
•Initialising it to the value of one of the parameters;(1 mark)
•Swapping the value of that parameter to the other;(1 mark)
•Swapping the other to the temporary;(1 mark)
•Returning the sum of the two values divided by two correctly cast to an integer;(1 mark)

 

[8]
(c)

(i)Define the purpose of a class constructor .[1 mark ]
A constructor is called when an object is created to initialise data members of the object.

(ii)Write a class,called StudentMarks ,which is to be used to store sets of student marks.Your class should contain the following member declarations:

•A default constructor;
•A default destructor;
•An array of integers,called Marks ,which is private .The array should be dynamically allocated.
•An integer,called NumMarks ,which can be used to store the length of the integer array. [4 marks ]
A sample definition of StudentMarks follows:

class StudentMarks {
{  
public:
  StudentMarks();
  ˜StudentMarks();
  x*x=temp;
private:  
  int**Marks;
int NumMarks;
};  

And the following marking scheme should be used:
•A correctly formed class structure ;(1 mark)
•A correct public constructor;(1 mark)
•A correct public destructor;(1 mark)
•A correct dynamic integer array declaration and integer declaration.
(1 mark)

(iii)Add to your class the declaration of an overloaded constructor,which takes two parameters x which is an array of integers and y ,which is the length of the array.[1 mark ]
StudentMarks : : StudentMarks(int**x,int y);

(iv)Add to your class an inline accessor function,called MarkAt ,which takes in a single parameter x ,and returns the value stored in the array at that index. [2 marks ]
A sample definition of MarkAt follows;

inline int StudentMarks::MarkAt(int x){
  return *Marks [x ];
}  

And the following marking scheme should be used:
•Correctly returning the array element;(1 mark)
•Correct use of inline .(1 mark)

(v)Add to your class a function,called ReadPass ,the signature of which is given below,which will return the index of the first element of the array to have a mark greater than 30,or -1 if there is none.
int ReadPass(); [4 marks ]

sample definition of ReadPass follows:

int StudentMarks::ReadPass()
{    
  int i;  
  for(i=0;i<NumMarks;i++)
   
if (*Marks [i ]>30)return i;
  return -1;  
}    

And the following marking scheme should be used:
•Declaring an iterative structure to loop along the array index;(1 mark)
•Correctly bound by the size of the array;(1 mark)
•Returning the index value if the test is true;(1 mark)
•Returning -1 if the end has been reached without the condition returning true.(1 mark)

[12]