
On the final day of the
C++ course yesterday, I demonstrated a number of advanced features and pulled together various strands that we had been learning through the week. At one level, each feature of the language can be explained and taught, but there's a further level that's need to show how they work together, and how they can best be made use of in combination.
Within the Object Oriented design area, we started the day by writing a series of classes - a base class, several that inherit from it, and then a further subclass of a subclass, and creating an array of the various types - see
[here]. The base class is a "shape"; subclasses are "rectangle" and "circle" and there's a further subclass of rectangle that's a "square".
The data in the first example was stored on the stack - meaning that it was lost / out of scope at the end of the routine in which it was set up, and that's hardly good news for a piece of code that wants to call a method to set up a whole load of objects. So in the following example, we modified that code - seen
[here] to switch the storage to somewhere more controllable / permanent. As something of an aside on the demonstration, I added a method to let me add circles together by overloading the "+" operator - showing operator overloading in C++. Source is
[here].
On a training course, where a single file of course code makes for a clean and shareable demonstration, the examples above work well. However, in extending the example into something that leads the way towards real life applications we want to split the code across multiple files. That way, each section is a manageable chunk. That way, each section can be on a different maintenance cycle and looked after by a different team member. And that way, some of the code sections can be pulled into various different applications in your program suite without being duplicated.
So the next demonstration (and the next thing done by the delegates on the course for their practical too) was to split the code for squares, rectangles, and the main application (a test program in this case) into separate files. In C++, that's slightly more complex, in that source files need to know about the API of the objects they use, via class definitions and function prototypes, which are stored into separate header files loaded by the pre-preocessor. And then we knit the whole thing together using a build environment; there are many of these, but for teaching / learning purposed we use make ... and there's a makefile that controls the build, the test, and I added a cleanup action too. You can see ALL of the files on a single page of source -
[here].
When I'm running a
C Course, I know that one of the things that's going to take a while to go through with the delegates is going to be pointers. They're good, they're needed, but they're also quick tricky and a bit complex. They're actually there in Perl too and behind everything in Python, but those more modern and higher level languages use them in a way that does not provide the same steep hill to climb during courses.
C++ builds on top of C. So it has pointers. But it also has a lighter-weight alternative, which are references. There's more about them
[here] and a complete worked example from yesterday
[here].
In training delegates on C++, much of the concentration is (and has to be) on Objects and the OO nature of the language, and it's easy to overlook some of the more basic things such as input and output in the examples that we use. So - pulling the various aspects toghther again - I wrote a more complete application that included data file handling, transferring incoming data records from a file into objects in the program. The source of the "base example" is
[here].
Moving forward from that base application, I added a factory method to my code to place the logic that converts a file to objects into the class itself. That's usually a natural thing to do, as the data file format wil be shared between all the applications that use the data, and the way it's interpreted should be hidden within ("encapsulated in") the class, rather than being left to each individual application. The final example is
[here] ((it's also using a vector rather than an array since we'll rarely know who much data there will be and can't used a fixed size memory block, and it has file checking, usage line if called up incorrectly, etc)). Data for this example is
[here].
(written 2011-01-08)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C235 - C and C based languages - I/O in C++ [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[1675] Comparing Objects in C++ - (2008-06-13)
[3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
[3390] Printing objects in C++ - (2011-08-13)
[3807] Reading (and writing) files in C++ - (2012-07-18)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[4562] Left shift operator on an output stream object - C++ - (2015-10-30)
[4563] Formatting and outputting your own classes in C++ - (2015-10-30)
C234 - C and C based languages - Further C++ Object Oriented features [801] Simple polymorphism example - C++ - (2006-07-14)
[802] undefined reference to typeinfo - C++ error message - (2006-07-15)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[1159] It can take more that one plus one to get two. - (2007-04-22)
[1819] Calling base class constructors - (2008-10-03)
[2004] Variable Scope in C++ - (2009-01-22)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2576] What does const mean? C and C++ - (2010-01-15)
[2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2849] What are C++ references? Why use them? - (2010-07-02)
[3057] Lots of things to do with and within a C++ class - (2010-11-16)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3982] Using a vector within an object - C++ - (2013-01-19)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
[4559] When do I use the this keyword in C++? - (2015-10-29)
C233 - C and C based languages - OO in C++ - beyond the basics [798] References and Pointers in C++ - (2006-07-10)
[925] C++ - just beyond the basics. More you can do - (2006-11-14)
[1217] What are factory and singleton classes? - (2007-06-04)
[1572] C - structs and unions, C++ classes and polymorphism - (2008-03-13)
[1674] What a lot of files! (C++ / Polymorphism demo) - (2008-06-12)
[2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
[2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
[3056] C++ - a complete example with polymorphism, and how to split it into project files - (2010-11-16)
[3123] C++ objects - some short, single file demonstrations - (2011-01-07)
[3142] Private and Public - and things between - (2011-01-22)
[3244] C and C++ - preprocess, compile, load, run - what each step is for - (2011-04-12)
[3251] C++ - objects that are based on other objects, saving coding and adding robustness - (2011-04-17)
[3508] Destructor methods in C++ - a primer - (2011-11-05)
[3811] Associated Classes - using objects of one class within another - (2012-07-21)
[3979] Extended and Associated objects - what is the difference - C++ example - (2013-01-18)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4375] Final examples for 2014 - and a look at our 2015 training course options - (2014-12-31)
[4560] Variables, Pointers and References - C and C++ - (2015-10-29)
Some other Articles
How does your browser find out about itself?Burger me!Car Parking in Melksham - thoughts on the proposed scheme, and wider thoughts tooThe family is defunct. Long live the family.C++ - putting the language elements together into a programWhen is a program complete?New year, new C CourseLearning to write good programs in C and C++ - separating out repeated codeTransWilts - some things to see and do