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)
235a
Associated topics are indexed under
C232 - C and C based languages - Defining and using classes in C++ [3978] Teaching OO - how to avoid lots of window switching early on - (2013-01-17)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
[3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[2579] Creating, setting up and using objects in C++ - (2010-01-16)
[2578] Where are your objects stored in C++? - (2010-01-16)
[2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
Q908 - Object Orientation and General technical topics - Object Orientation: Design Patterns [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[3843] Caching Design Patterns - (2012-08-20)
[3608] Design Patterns - what are they? Why use them? - (2012-02-12)
[2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
[2741] What is a factory? - (2010-04-26)
[2322] Looking for a practical standards course - (2009-08-05)
[1224] Object Relation Mapping (ORM) - (2007-06-09)
[485] North, Norther and Northest - PHP 5 Objects - (2005-11-04)
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