August 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 3

Total Marks: 15 Marks

Click here to access other questions

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

(a) Briefly explain the difference between multiple and single inheritance. [2 marks]
(a) • Multiple inheritance is when a class inherits from two or more base class trees; (1 mark)
• Single inheritance is when a class only inherits from one base class tree; (1 mark) [2 marks]

(b) State the scope of each of the public, protected and private areas of a class in a derived class where the derived class is declared to inherit protected from the base class. [3 marks]
(b) • The public members become protected; (1 mark)
• Protected members stay protected; (1 mark)
• Private members stay private; (1 mark)
[3 marks]

(c) The Rover Car company require a new system to aid them in the design of new products. One of the products they wish to design is a Tractor. A Tractor is both a Vehicle, and a Tool. Consider the following definitions of the classes Vehicle and Tool:
class Vehicle {
  int NumberPassengers, EngineCapacity;
  public:
  Vehicle(int Passengers, int Capacity);
  int GetPassengers();
}
class Tool {
  char *Purpose, *PowerSource;
  public:
  Tool(char* p, char* s);
  char* GetPurpose();
}
(i) Using inheritance, define the class Tractor such that it inherits all the properties of both the Vehicle and the Tool class. Your class should include the variable RoofType, which is a boolean variable of type boolean, set to true if the Tractor
has a roof, and false otherwise. In addition to this, your class should include a
constructor function, which takes all the parameters necessary to initialise all the
member variables. [5 marks]
(i) A sample definition of Tractor follows:
class Tractor : public Vehicle, public Tool {
  public:
  boolean RoofType;
  Tractor(int NumPass, int EngCap, char* Pur, char* Power, boolean Roof);
}
And the following marking scheme should be used:
• Declaring the class to inherit from Vehicle; (1 mark)
• Declaring the class to inherit from Tool; (1 mark)
• Including parameters in the constructor to initialise the members inherited from Vehicle; (1 mark)
• Including parameters in the constructor to initialise the members inherited from Tool; (1 mark)
• Including parameters in the constructor to initialise the native members; (1 mark) [5 marks]

(ii) Define a class, called Component, which contains the attributes ComponentName, which is a string, and ComponentPartNumber, which is an integer. You should ensure that these attributes are accessible to all other classes. [3 marks]
(ii) a sample definition of Component follows:
class Component {
  pubic:
  string ComponentName;
  int ComponentPatrNumber;
}
And the following marking scheme should be used:
• Including the ComponentName of type string; (1 mark)
• Including the ComponentPartNumber of type int; (1 mark)
• Specifying that these are public; (1 mark)
[3 marks]

(iii) When a Tractor is built, it is not known how many components will be required until it is finished. Add to your Tractor class an array of type Component, the length of which is decided at allocation time, such that this array is not accessible outside of any further derived classes. [2 marks]
(iii) The following definition should be added:
private:
Component a[];
And the following marking scheme should be used:
• Declaring the array; (1 mark)
• Stating it to be private; (1 mark)
[2 marks]