Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Strings, Garbage Collection and Variable Scope in C++

In C, you'll handle strings as arrays of chars (type char *) and that does work - but with grumbles from the compiler about deprecation - in C++. The more modern (or shall we say "more OO") approach is to handle strings as objects - and those will be objects of type string, with headers loaded via
  #include <string>
See [here] for an (offsite) listing of the various applicable methods, and [here] for a sample in which I've set up a couple of strings, concatenated them, and printed them out to get you started.


The challenge on the C++ course was to write an example that involved Christmas, and the challenge was answered by an example concerning public holidays. Some public holidays are on fixed dates - Christmas day is always 25th December, and others are always fixed days of the week - the August Bank Holiday is always a Monday. There are a few other differences too (such as whether or not supermarkets are legally allowed to open), but much is the same.

So on that basis, we designed a base class called "Holiday" and then two subclasses called "Daybased" and "Weekbased", with the common code in the "Holiday" class and methods that changed in the subclasses.

The example - source code [here] - includes demonstrations of a number of features that go rather beyond a first example of polymorphism. You'll find a factory method called "builder" which creates an object of a type that it decides on internally. You'll find a call from the subclass constructor to a non-default base class constructor (by default, a no-parameter base class constructor is called). You'll find multiple inheritance demonstrated. You'll find a virtual method to allow us to handle all types of Holiday from within base class objects, even including the differing behavior, and you'll find all the public holidays for the year loaded into a vector rather than an array, as I wanted to use a construct that was extend-able.

Illustration - City Hall, Belfast provides the backdrop for the Christmas market - photo taken in the evening as I waited for my boat back to Birkenhead


C++ variables are scoped to the block in which they're declared, and they're usually stored on the stack, being lost upon exit from the block as the stack is reduced. Variables declared within an inner scope temporarily mask out access to variables of the same name declared in an outer scope - there's an example showing this [here].

That example's a real "tutor's example only". In a real live application, you would rarely set out to use the same name three times over within 20 lines, but the facility does have its uses. Are you familiar with lines like this:
  this->dom = dom;
which says "the dom parameter is stored into the dom variable within the current object". I could have written the code using two different names and the the this-> would have not been necessary, but the common idion is to use the same name, and it's an idiom I have to agree with as it saves the need for coming up with several names for the same parameter / attribute / property, and the confusion that would ensue if you did so.

Since variables are scoped in this way, and stored on the stack, the release of their memory is intrinsic on block completion and it can be automatically reused without them needing to be garbage collected. However, where variables are created / stored on the heap via new, that's not the case and you do need to think about garbage collection in your program. Internally, new uses malloc and you should use delete to free up object memory. That way you have control over your own storage, which is probably a fair balance for a language that's designed to give full and low level control whilst providing full OO facilities - but this balance is provided at the cost of potentially programs that take longer to write, and that keep growing as they run if you get them wrong.

There's a garbage collector for C++ which works with most compilers [here] if you want one - and you'll find the full technical stuff there. Note for newcomers o garbage collection ... most of the languages we teach such as Java, Perl and Python have their own garbage collectors - routines to gather up redundant memory for re-use - built in, so you're saved coding them yourself at the expense of slight and non-totally-predictable apparent pauses as your application runs.
(written 2010-11-25, updated 2010-12-04)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C239 - C and C based languages - Putting it all together
  [836] Build on what you already have with OO - (2006-08-17)
  [925] C++ - just beyond the basics. More you can do - (2006-11-14)
  [945] Code quality counts - (2006-11-26)
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)
  [2646] Compile but do not run PHP - syntax check only - (2010-02-22)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
  [2851] Further C++ material - view new or old - (2010-07-04)
  [3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4559] When do I use the this keyword in C++? - (2015-10-29)

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)
  [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)
  [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)
  [4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)

C231 - C and C based languages - Introduction to C++
  [317] Programming languages - a comparison - (2005-05-20)
  [318] Choosing a theme - (2005-05-20)
  [336] Targetted Advertising - (2005-06-05)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
  [3052] Getting your C++ program to run - (2010-11-15)
  [3053] Make - automating the commands for building and installing - (2010-11-16)
  [3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
  [4466] Moving from C to C++ - Structured to Object Oriented - a lesson for engineers - (2015-03-28)
  [4561] Hello World in C++ - a first program, with the process explained - (2015-10-30)
  [4562] Left shift operator on an output stream object - C++ - (2015-10-30)

Back to
Throwing your own exception in C++, and catching it
Previous and next
Horse's mouth home
Forward to
Belfast, on a cold November evening
Some other Articles
Customer Service - the boundary
Finding elements common to many lists / arrays
Well House Price list, January to March 2011
Belfast, on a cold November evening
Strings, Garbage Collection and Variable Scope in C++
Throwing your own exception in C++, and catching it
Separating groups of variables into namespaces
Pictures from the Birkenhead to Belfast crossing
On the way to the course this week
4759 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3069_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb