My first technical post for 2015 - looking back at some of last year's new C++ examples.
Oh - OK - I'm writing this on 1st January and these examples were written and uploaded yesterday, so they're nothing like as old as it sounds.
On Monday and Tuesday, I wrote classes from scratch and looked at basic design - see
[here]. I then added dynamic memory management via the
new keyword for heap allocation, and using a
vector to store references or pointers to my objects - see
[here].
Yesterday, I wrote an example in which in had a base class that contained the common specifications and code for a number of similar types of objects, andthen looked at how that can be extended to provide similar types of objects without code duplication. For the whole course, I was very much stressing
Test Driven Development and
Behaviour Driven Development, and so my very first piece of code -
[here] - provides a test and a specification of the API (application programmer interface) to ensure it compiles. It won't load into a complete program yet.
In the second example -
[here] - I have implemented the base class (called "shape") and the subclasses ("ball" and "box") together with "cube" which further derives from "shape". You'll notice how hardly any code is repeated from one section to another, as the common code is shared as far up the tree as possible. Colour and density don't change with the change of shape, so they're in the common code, whereas the volume does change for a ball and a box. The volume for a cube is simply the volume for a box, as a cube (by our definition) is simply the special case of a box.
When working out whether incoming data (from a file, for example) relates to boxes, cubes or balls, some logic is required. Naturally, that logic falls within the class code which is looked after by our data type programmer, rather than by our application programmer who just wants to say "here is some data - please work out what object type it is and make the object for me". I have provided a solution to this requirement in the next program -
[here] via a static (class) method called "factory"). Nothing special in that name - it's just a convention as raw material is passed in (an incoming string, for example) and manufactured produce (or, rather, and object) is returned.
Further testing was added in the next stage, using the text harness from previous days (
[here] for the code
[here] for the test harness headers and
[here] for the test harness code. And the final example
[here] shows operator overloading in C++ ... that may not have been a logical extenion to my demonstration, but it's a subject needed to be covered on our
C++ courses and it fitted in here during the presentation.
(written 2015-01-01)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
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)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[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)
[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)
Q908 - Object Orientation and General technical topics - Object Orientation: Design Patterns [485] North, Norther and Northest - PHP 5 Objects - (2005-11-04)
[1224] Object Relation Mapping (ORM) - (2007-06-09)
[2322] Looking for a practical standards course - (2009-08-05)
[2741] What is a factory? - (2010-04-26)
[2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
[3608] Design Patterns - what are they? Why use them? - (2012-02-12)
[3716] Learning C++ - a design pattern for your first class - (2012-05-02)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[3843] Caching Design Patterns - (2012-08-20)
[4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[4096] Perl design patterns example - (2013-05-20)
[4098] Using object orientation for non-physical objects - (2013-05-22)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
[4359] How to avoid too many recalculations within an object - (2014-12-21)
[4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
[4421] How healthy are the stars of stage and screen? - (2015-02-09)
[4581] Thin application, thick objects - keep you main code simple. Example in Ruby - (2015-11-21)
[4626] Singleton design pattern - examples and uses - (2016-01-20)
[4663] Easy data to object mapping (csv and Python) - (2016-03-24)
[4673] Separating detailed data code from the main application - Ruby example - (2016-05-16)
Q909 - Object Orientation and General technical topics - Object Orientation: Composite Objects [477] Class, static and unbound variables - (2005-10-25)
[592] NOT Gone phishing - (2006-02-05)
[1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
[1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
[2170] Designing a heirarcy of classes - getting inheritance right - (2009-05-11)
[2641] Object Oriented Programming in PHP - (2010-02-19)
[2865] Relationships between Java classes - inheritance, packaging and others - (2010-07-10)
[2922] Getting the OO design write - with PHP a example - (2010-08-14)
[3152] Jargon busting - (2011-01-30)
[3609] How do classes relate to each other? Associated Classes - (2012-02-12)
[4394] Philosophy behind object design - and how I applied in to a Java example - (2015-01-14)
[4450] Deciding whether to use parameters, conditional statements or subclasses - (2015-03-05)
Some other Articles
Installing Cucumber (Ruby)Behaviour Driven Development / Ruby and CucumberWell House Consultants / Well House Manor - Prices for 2015What FGW passengers want to talk about / and PHP programming to find outDesigning a base class and subclasses, and their extension, in C++Well House Consultants, Well House Manor, First Great Western Coffee shop, TransWilts / 2014 web site reportsTest driven development, and class design, from first principles (using C++)A long and disappointing eveningTemplate / design pattern for C++ constructor and accessors