2adb Learning C++ - a design pattern for your first class
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Learning C++ - a design pattern for your first class

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)


Back to
Changing face - Filton
Previous and next
or
Horse's mouth home
Forward to
Returning extra results from a function in C
Some other Articles
Melksham ATC - freedom of the town
Strawberry Cream Teas, Well House Manor, Melksham, starting this weekend
Splitting a record into individual data values in C
Returning extra results from a function in C
Learning C++ - a design pattern for your first class
Changing face - Filton
Just 12 winners? Or 371 or more? Town Centres.
Verticals ... last month
Some unpublished and historic pictures - Museum of Melksham
Kiss
4084 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page
3ec6

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3716_Lea ... class.html • PAGE BUILT: Sat Feb 23 12:39:13 2013 • BUILD SYSTEM: wizard
0