When 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].
(written 2011-04-17)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C232 - C and C based languages - Defining and using classes in C++ [1925] Introduction to Object Oriented Programming - (2008-12-06)
[2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
[2578] Where are your objects stored in C++? - (2010-01-16)
[2579] Creating, setting up and using objects in C++ - (2010-01-16)
[3716] Learning C++ - a design pattern for your first class - (2012-05-02)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[3978] Teaching OO - how to avoid lots of window switching early on - (2013-01-17)
[4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
[4372] Template / design pattern for C++ constructor and accessors - (2014-12-29)
[4565] Allocation of memory for objects in C++ - Stack v Heap - (2015-10-31)
C231 - C and C based languages - Introduction to C++ [317] Programming languages - a comparison - (2005-05-20)
[318] Choosing a theme - (2005-05-20)
[336] Targetted Advertising - (2005-06-05)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[2004] Variable Scope in C++ - (2009-01-22)
[2169] When should I use OO techniques? - (2009-05-11)
[2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
[3052] Getting your C++ program to run - (2010-11-15)
[3053] Make - automating the commands for building and installing - (2010-11-16)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[4466] Moving from C to C++ - Structured to Object Oriented - a lesson for engineers - (2015-03-28)
[4561] Hello World in C++ - a first program, with the process explained - (2015-10-30)
[4562] Left shift operator on an output stream object - C++ - (2015-10-30)
C051 - C and C based languages - C++ - General [2370] C++, Python, and other training - do we use an IDE - (2009-08-21)
[2504] Learning to program in ... - (2009-11-15)
[2536] All the Cs ... and Java too - (2009-12-13)
[2763] Our C and C++ training course are on Open Source platforms - (2010-05-13)
[2851] Further C++ material - view new or old - (2010-07-04)
[3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
[3129] Extra courses - C and C++ - (2011-01-12)
[3155] Rake - a build system using code written in Ruby - (2011-02-03)
[3587] C++ Courses - do I get official certification at the end of my Well House course? - (2012-01-20)
[3809] Dwarf Exception Unwind Info - (2012-07-20)
[4335] Flexible public courses - residential or commuting, programming newcomer or experienced, C or C++ - (2014-11-30)
[4355] C++ in 2 days - (2014-12-18)
Some other Articles
Multiple 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 ManorCollections in C and C++ - arrays, vectors and heap memory blocks