C++ - objects that are based on other objects, saving coding and adding robustness
In 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.
Having covered object basics, we moved on to look at related types of objects. I had taken trains as my first example, and I wanted to move on to cover public transport in general. There are more similarities than there are differences between the use of a bus and a train (and a ferry!) and so it would be a great shame to duplicate program code. The duplication would be quick and easy enough, but then any future updates / corrections / enhancements would have to be made to to each copy, and that's going to get ver inefficient. So we create what's known as a "base class" which includes all the common stuff, and then a series of "subclasses" each of which contains the the extras or the stuff that varies.
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.
Where we want to hold a whole array of objects of similar type (i.e. based on the same base class), we can do so. And we've an example of that in source code [here]
. But there are a couple of extras you need in C++.
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:
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. (written 2011-04-17)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesC233 - C and C based languages - OO in C++ - beyond the basics 
Extended and Associated objects - what is the difference - C++ example - (2013-01-18) 
Associated Classes - using objects of one class within another - (2012-07-21) 
Destructor methods in C++ - a primer - (2011-11-05) 
C and C++ - preprocess, compile, load, run - what each step is for - (2011-04-12) 
Private and Public - and things between - (2011-01-22) 
C++ - putting the language elements together into a program - (2011-01-08) 
C++ objects - some short, single file demonstrations - (2011-01-07) 
C++ - a complete example with polymorphism, and how to split it into project files - (2010-11-16) 
Objects and Inheritance in C++ - an easy start - (2010-07-01) 
Complete teaching example - C++, inheritance, polymorphism - (2010-01-15) 
Calling base class constructors - (2008-10-03) 
What a lot of files! (C++ / Polymorphism demo) - (2008-06-12) 
C - structs and unions, C++ classes and polymorphism - (2008-03-13) 
What are factory and singleton classes? - (2007-06-04) 
C++ - just beyond the basics. More you can do - (2006-11-14) 
Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13) 
Simple polymorphism example - C++ - (2006-07-14) 
References and Pointers in C++ - (2006-07-10)Q909 - Object Orientation and General technical topics - Object Orientation: Composite Objects 
How do classes relate to each other? Associated Classes - (2012-02-12) 
Jargon busting - (2011-01-30) 
Getting the OO design write - with PHP a example - (2010-08-14) 
Relationships between Java classes - inheritance, packaging and others - (2010-07-10) 
Object Oriented Programming in PHP - (2010-02-19) 
Designing a heirarcy of classes - getting inheritance right - (2009-05-11) 
Screw it or Glue it? Access to Object variables - a warning - (2007-09-12) 
Perl and Shell coding standards / costs of an IT project - (2007-09-11) 
NOT Gone phishing - (2006-02-05) 
Class, static and unbound variables - (2005-10-25)
Some other Articles
Process every member of an array, and sort an array - RubyMultiple inputs, multiple out, ruby functionsIs this number between? Does this list include? - RubyC++ - unknown array size, unknown object type. Help!C++ - objects that are based on other objects, saving coding and adding robustnessC++ - how we teach the language and the concepts behind the languageEvents - Spring and Early Summer 2011 in MelkshamWhat have these pictures in common?Light and dark at Green ParkMelksham - the way forward. 26th April, Well House Manor