Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

C++ - putting the language elements together into a program

C++ training course - January 2011On 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
C233 - C and C based languages - OO in C++ - beyond the basics
  [3979] Extended and Associated objects - what is the difference - C++ example - (2013-01-18)
  [3811] Associated Classes - using objects of one class within another - (2012-07-21)
  [3508] Destructor methods in C++ - a primer - (2011-11-05)
  [3251] C++ - objects that are based on other objects, saving coding and adding robustness - (2011-04-17)
  [3244] C and C++ - preprocess, compile, load, run - what each step is for - (2011-04-12)
  [3142] Private and Public - and things between - (2011-01-22)
  [3123] C++ objects - some short, single file demonstrations - (2011-01-07)
  [3056] C++ - a complete example with polymorphism, and how to split it into project files - (2010-11-16)
  [2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
  [2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
  [1819] Calling base class constructors - (2008-10-03)
  [1674] What a lot of files! (C++ / Polymorphism demo) - (2008-06-12)
  [1572] C - structs and unions, C++ classes and polymorphism - (2008-03-13)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [925] C++ - just beyond the basics. More you can do - (2006-11-14)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [801] Simple polymorphism example - C++ - (2006-07-14)
  [798] References and Pointers in C++ - (2006-07-10)

C234 - C and C based languages - Further C++ Object Oriented features
  [3982] Using a vector within an object - C++ - (2013-01-19)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3057] Lots of things to do with and within a C++ class - (2010-11-16)
  [2849] What are C++ references? Why use them? - (2010-07-02)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2576] What does const mean? C and C++ - (2010-01-15)
  [2005] Variables and pointers and references - C and C++ - (2009-01-23)
  [2004] Variable Scope in C++ - (2009-01-22)
  [1159] It can take more that one plus one to get two. - (2007-04-22)
  [802] undefined reference to typeinfo - C++ error message - (2006-07-15)

C235 - C and C based languages - I/O in C++
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3807] Reading (and writing) files in C++ - (2012-07-18)
  [3390] Printing objects in C++ - (2011-08-13)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [1675] Comparing Objects in C++ - (2008-06-13)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)


Back to
C++ objects - some short, single file demonstrations
Previous and next
or
Horse's mouth home
Forward to
The family is defunct. Long live the family.
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 too
The family is defunct. Long live the family.
C++ - putting the language elements together into a program
When is a program complete?
New year, new C Course
Learning to write good programs in C and C++ - separating out repeated code
TransWilts - some things to see and do
4292 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, 83, 84, 85, 86 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

© WELL HOUSE CONSULTANTS LTD., 2014: 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/3124_C-p ... ogram.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat