April 2000
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 4

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for 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 ]

(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 ]

(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 ]