C++ - objects that are based on other objects, saving coding and adding robustness
Archive - Originally posted on "The Horse's Mouth" - 2011-04-17 10:25:56 - Graham EllisIn a previous article ([here]), I talked about how we introduce the basic concepts of classes, methods and objects on our C++ courses. I'm now going to give you some links to the next series of example which I wrote during that same course.

How does this work at run time? When you call a method on an object, it looks within the functions defined for that object type. If the method isn't there, it looks in the parent class (i.e. the common base), an dif it's not there it looks in the parent's parent, and so on. Very neat, great to write, but you do need to document what you're doing fairly carefully so that you can find out where a particular functionallity has come from later on!.
There's a first example from the course [here]; my base class is called "Ptrans" for Public Transport, and I have got two subclasses (also know as extended classes) - one each for a Bus and a Train. A lot of the code can be common to Ptrans, but when it comes to working out the number of drivers required for a certain number of vehicles, it's one per vehicle on the road, but one per whole series of vehicles on the railway. A setup like this, where the code is selected as the program runs depending on the type of object it's being run on, is called polymorphism. You won't find the word declared in the source code - it's a useful side effect of the design method used.

1. If you're going to set up an array containing different object types, you need to declare the array to be of the type of a common parent. Thus - an array of pointers to Ptrans in our example
2. If you're calling any methods which ONLY exist in the subclasses (such as getting the number of drivers, where there is no common code but you'll have an algorithm for EVERY vehicle type), you need to declare virtual methods in your base class. You're turning it into what's known as an abstract class (i.e. you can't have any pieces of Public Transport which are NOT defined by a subclass)
You can have methods that work on an object (they're know as dynamic or object methods), and you can also have methods that work on the class as a whole (they're known as static method or class methods) - but what if you want to write a method which works on two objects - for example to compare them. "Which has the higher capacity?". That's something that I added to the third example in this series - source code [here].
The 'trick' - if you would like to call it a trick - is to pass in one object as a parameter to an object method - thus:
options[2]->compare(options[0])
to compare two specific members of an array of Ptrans object known as options. By careful codeing, you can use a method in the base class to compare two different objects based on a polymorphic criterion of your choice - our example returns the object with the higher passenger capacity in this example.
You may at first wonder why I used the word that for my parameter ... I did that simpley because the object on which the call is based is called this (a reserved word in C++), and it adds a neat understandability and symmetry to have the second thing that's being compared known as that.
The example goes on to include a static method (I called it mostyn for a reason that's already lost in time!) which looks for the highest capacity in a whole array of objects. Mostyn calls compare ... which calld getcapacity for each individual object. It's a neat tree which, once again, show how code is being shared and should only be written once. Algorithms can easily be changed later, at a single stroke, and retesting is easy. You'll score high in the coding stakes, and high in consistency and reliability too using this approach.
P.S. ... extra example, showing how to write a destructor in C++ - [here]. If you want to add some extra activities when you've finished with an object (such as flushing changes made to a copy of the data help in memory back to a database) you may do so via a destructor. Destructors are odd in that you don't necessarily have to call them directly - they may be called and run automatically when a variable goes out of scope.