August 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) Briefly explain what is meant by each of the following terms in object oriented
programming.
(i) Constructor; [1 mark]
(ii) A public member of a class; [1 mark]
(iii) Function overloading; [1 mark]
(iv) Static member; [1 mark]
(v) A friend function; [1 mark]
(a) (i) A constructor is a function called when an object is created. [1 mark]
(ii) A public member is one which can be seen and called by any external
object. [1 mark]
(iii) Overloading is when a function or method can have several signatures, with different numbers or types of parameters. [1 mark]
(iv) A static member is one where a single instance of a member is shared by all instantiations of the class. [1 mark]
(v) A friend function is one which has access to the private areas of another class. [1 mark]

(b) (i) State the number of destructors a class may have. [1 mark]
(ii) Briefly describe what is meant by an abstract class. [2 marks]
(iii) Briefly explain how polymorphism can promote extensibility. [2 marks]
(b) (i) A class can have only one destructor. [1 mark]
(ii) • An abstract class is one which contains virtual members; (1 mark)
• which do not necessarily have implementations to instantiate. (1 mark)
[2 marks]
(iii) It can promote extensibility because:
• Different types can be used in place of others; (1 mark)
• Promoting reuse of existing class libraries. (1 mark)
[2 marks]

(c) (i) Write a class, called Motherboard, which contains the following:
• The private data members CPU, which is of type String, and the memory, which
is an integer (in megabytes).
• A constructor function which takes two parameters, which initialise the private
data members. [4 marks]
(ii) Write an accessor method, which should be declared as a member of the Motherboard class, which will return the amount of memory available. [2 marks]
(iii) Write a deep copy constructor for your Motherboard class. which takes a constant reference to an object of type Motherboard as an argument, and will create a new Motherboard object by performing a deep copy. [4 marks]
(c) (i) A sample definition of Motherboard follows:
class Motherboard {
private:
  String CPU;
  int memory;
  public:
  Motherboard(string s, int m) {
    CPU= s;
    memory= m;
  }
}
And the following marking scheme should be used:
• A correctly structured class; (1 mark)
• Declaring the CPU to be of type string (char* is not acceptable) and private; (1 mark)
• Declaring the memory to be of type int, and private; (1 mark)
• An appropriate constructor function (1 mark)
[4 marks]
(ii) A sample definition follows:
int Motherboard::GetMemory() {
   return memory;
}
And the following marking scheme should be used:
• A correct signature, including the Motherboard:: component; (1 mark)
• Correctly returning the value of memory; (1 mark)
[2 marks]
(iii) A sample definition of the constructor follows:
Motherboard(const Motherboard& m) {
  memory= m.Memory;
  CPU= m.CPU;
}
And the following marking scheme should be used:
• Correctly passing in a reference to the existing object, using &; (1 mark)
• Declaring this to be a const; (1 mark)
• Correctly copying the members - noting that they are accessible to the copy constructor; (1 mark)
• Correctly structuring the function; (1 mark)
[4 marks]

(d) Give the output of the following C++ program:
void main(void) {
  int xs[]={1,2,3},*p1,*p2,*p3, *p4;
  p1 =xs;
  p2 =p1;
  p3 =p1++;
  *p2=30;
  p4 =&xs[2];
  printf("p1 is %d\n",*p1);
  printf("p2 is %d\n",*p2);
  printf("p3 is %d\n",*p3);
  printf("p4 is %d\n",*p4);
}
[4 marks]
(d) The output is as follows:
p1 is 2
p2 is 30
p3 is 30
p4 is 3
And the following marking scheme should be used:
• Correct value for p1; (1 mark)
• Correct value for p2; (1 mark)
• Correct value for p3; (1 mark)
• Correct value for p4; (1 mark)
[4 marks]

(e) Write a template function, called IsMember, which takes three arguments. The first argument is an array of template type T, the second is a datum of template type T, and the third is an integer specifying the length of the array. The function should return the index of the first element in the array matching the datum, or 0 if there is no matching entry. You may assume that overloadings of any necessary operators exist. [6 marks]
(e) A sample definition of IsMember follows:
template <T>
int IsMember(T[] a, T b, int s) {
  int i;
  for(i= 0; i< s; i++)
  if (a[i]= b)
  return i;
  return 0;
}
And the following marking scheme should be used:
• Specifying the template declaration correctly; (1 mark)
• Specifying the correct types for the parameter list; (1 mark)
• Declaring a loop to iterate along the array; (1 mark)
• Bound by s; (1 mark)
• Comparing the current element on each iteration; (1 mark)
• Returning the correct value when appropriate; (1 mark)
[6 marks]