April 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 4

Total Marks: 15 Marks

Click here to access other questions

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

An activity is something people invest their spare time in.An activity has a name and a number of participants Consider the following definition of the class Activity

public class Activity {
  protected:
  String Name;
  int NumberParticipants;
public:
  Activity();
  ~Activity();
   void InitiateActivity(); // Starts the activity.
}

(a)Define what is meant by the term inheritance with respect to object oriented
programming.[2 marks ]
Inheritance is:
•The mechanism of creating a new class;(1 mark)
•that includes the member variables and methods of an existing class.(1 mark)
[2 marks ]

(b)An exercise is a particular form of activity,with the added properties of a
PainThreshold which is an integer in the range 1 to 100 and a number of
Repetitions which is also an integer.Using inheritance,define a class exercise
which incorporates all the attributes of an activity,as well as the properties specific to exercises.The attributes should be made available to subsequent derived classes, but not to unrelated classes.Your class should also include a constructor which takes all the member attributes as parameters.[4 marks ]
A sample definition of exercise follows:
class excercise : public activity {
public:
  excercise(int p, int r);
protected:
  int PainThreshold;
  int Repetitions;
}
And the following marking scheme should be used:
•Declaring the class to inherit from activity (1 mark)
•Declaring the members to be protected (1 mark)
•Including suitable declarations of the members;(1 mark)
•Including an appropriate public constructor.
[4 marks ]

(c)A hobby is also a particular form of Activity with the added attribute of a Cost which is an integer.An implementation of the hobby class would include these members,along with a constructor which took the member attributes as parameters. If a sport is both a hobby and an exercise then the class sport can be derived from the combination of the two classes exercise and activity
(i)State the object oriented name for this situation.[1 mark ]
(ii)List,with their types,the members variables (properties)which are inherited by the class sport [5marks ]
(iii)Implement a constructor function for the class sport such that it takes as
parameters all the member variables,and calls immediate parent constructors
with the appropriate parameters.In addition to this,you should initialise the
number of participants appropriately.[3 marks ]
(i)Multiple inheritance [1mark ]
(ii) •Name (String);(1 mark)
•NumberParticipants (int);(1 mark)
•PainThreshold (int);(1 mark)
•Repetitions (int);(1 mark)
•Cost (int).(1 mark)
[5 marks ]
(iii)A sample definition of the constructor follows:
sport(String Name,
    int NumberOfParticipants,
    int PainThreshold,
    int Repetitions,
    int Cost) {
  excercise(Name, PainThreshold, Repetitions);
  hobby(Name, Cost);
  NumberParticipants= NumberOfParticipants;
}
And the following marking scheme should be used:
•Calling the exercise constructor with the appropriate parameter list;
(1 mark)
•Calling the hobby constructor with the appropriate parameter list;(1 mark)
•Initialising the NumberParticipants member.(1 mark)
[3 marks ]