Object-Oriented Programming, as opposed with Object-Based Programming, is when the programmers uses Inheritance between classes to implement the application. In many cases, inheritance makes the code easier to extend and maintain. Furthermore, in the real world, inheritance exists (in the sense that a Dog and a Cat are both Animals and, consequently, they share some behaviours) and, by properly using inheritance, the program becomes much clearer. Chapter 17 of the book explains how to use inheritance.
Work to do
We are now going to enhance our race simulation program by using inheritance to make it fully object-oriented.
Up to now, a race was won by the driver who has the best ability and, if there are many such drivers, the one driving the fastest car. This is artificial in the sense that it’s always the same drivers (those with maximum ability) who win. In real-life, the winner is not always the best driver:
- Sometimes, because of luck, a lesser driver wins. This can be modeled, for example, by simulating an actual race over a distance and assuming that a car travels along a distance which is proportional to the performance of the car but biased with an element of randomness.
- Some races might be handicap-based where those drivers with more ability start behind others. Naturally, here also it is good to have an element of randomness to make things more interesting.