C++ - how we teach the language and the concepts behind the language
Archive - Originally posted on "The Horse's Mouth" - 2011-04-17 09:42:56 - Graham EllisWhen I'm teaching an object oriented language such as C++, I like to write code and demonstrate from scratch to my class, getting them to 'feed' me with application data and information so that I can show them how programs are really developed using the OO approach - both in general, and in the specific language. To teach someone how to code without teaching them how to design would be like teaching someone how to operate a car, without teaching them the rules of the road.
Much of OO design is about keeping the data and code that are specific to a particular type of thing you're writing code about ("particular class of object") in its own code area and segment so that it can be specified, written, tested, maintained and later altered with ease, and without having to modify the overarching applications. Which normally means that all the "class stuff" would go into separate files, to be looked after independently and called into multiple applications. However, for a first demonstration I usually choose to write it all into one file - it helps get the concepts across, and it avoids having lots of source code windows to manage while teaching. See [here] for such a first demonstration.
In my second example on this course - source code [here] - I then start to extend the envelope. On the recent course, I chose to introduce the fact that you can have several methods of the same name in the same class in C++ (the compiler and loaded tell them apart by the number and type of parameters), and to demonstrate that a constructor can do more than simply save values that are passed in. The example also introduced the word "this" to allow the program to use the same word for a parameter and a variable within an object.
The third example in this series introduces an internal (private) method - such methods are very commonly used to share code between a group of user-callable methods which need to share logic. In this example, that sharing is of the setup code for two different constructors. Within the main application of this demosntration we've moved forward too - the example now has a whole array of objects of class "Train", so that the code can remain essentially the same if it's being run with 1 or 2 objects ... or if we've increased it to a complete fleet of trains. The source code is [here]. We've also defined the array on the heap rather than the stack - that makes a significant difference to the dynamics of the program (i.e. how flexible it can be at runtime, and also how long the objects exist for).
There comes a point as I'm writing demonstrations that I "refactor" - revise and refresh the work. This is helpful for delegates as by the time I've taken a short program and added all sorts of demonstration pieces to it, it's getting a bit unwieldy. So when I came to taka about creating new types (classes) of objects that were based on other more fundamental objects, I started a fresh set of examples. Probably a good point to start a new posting and chapter here, then!
We offer three different public C++ courses - and they're so popular that we've just added extra dates. The courses are:
• Learning to program in C++ (for delegates who are new to programming) - see [here]
• C and C++ Programming (for delegates who have programmed before, but NOT in C) - see [here]
• C++ Programming (for delegates who have programmed in C before and want to add C++ skills) - see [here]
Public courses run at our Melksham, Wiltshire, England training centre - see [here] for course details. We also have accommodation available for delegates (and non-delegates are welcome to stay too), which makes us into a hotel. The hotel website is [here].