From today's
C++ Course - a new, clear example of a design pattern for a simple class, its headers, and a test harness -
[here].
When using an Object Oriented language, design patterns are really useful. Essentially, they're a known and standard way of combining the features of the language in a convenstional way. By using a design pattern, you're going to be writing along the same lines as others have written before, so you'll:
• avoid some common code pitfalls
• find it easier to write your code as it will be familiar
• produce code that's easy for others to pick up and follow later on
My "SimpleClass" design pattern (as I've chosen to call it) shows a class definition, definition of each of the members, and definition of a calling program. It's very much a starting point within the course, and I went on after this example to add:
• private access for member variables, and public accessor functions
• use of
this rather than using different names in the constructor
• multiple methods of the same name (I provided several constructors)
Going one stage further, I divided the code into three separate files:
• a header file which defines the API to the class -
[here]
• a file which defines all the methods within the class -
[here]
• and the test harness -
[here].
This is becoming a much more realistic use of a class - with the test program being separated from the detail of how the class works, and the header file in a third place and shared betweenthe two program files we already have, and other programs which choose to make use of our class.
Completing this example - compile and run:
munchkin:rcpp grahamellis$ g++ -o tabby table.cpp tabletest.cpp
munchkin:rcpp grahamellis$ ./tabby
Size of table 1.8
1200
Size of table 3.38
munchkin:rcpp grahamellis$
We offer three different C++ courses - for delegates who haven't programmed before (5 days), for delegates who have programmed before, but not in C (4 days), and for delegates who have prior programming experience in C. See
[here]. A public course series ran last week, and the next public course series runs in mid-July - already booking well, but there are still spaces left at all levels. We run private courses as required for single customer groups; as I write, just one week is available in May and you would then be looking forward several months ...
(written 2012-05-02, updated 2012-05-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
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)
[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)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4359] How to avoid too many recalculations within an object - (2014-12-21)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
[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)
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)
[3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
[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)
Some other Articles
Melksham ATC - freedom of the townStrawberry Cream Teas, Well House Manor, Melksham, starting this weekendSplitting a record into individual data values in CReturning extra results from a function in CLearning C++ - a design pattern for your first classChanging face - FiltonJust 12 winners? Or 371 or more? Town Centres.Verticals ... last monthSome unpublished and historic pictures - Museum of MelkshamKiss