Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Second C++ demo - heap v stack, char handling, this, static
this example from a Well House Consultants training course
More on this [link]
Source code: act_02.cpp Module: C051
using namespace std;

#include <iostream>
#include <string>

Taking the earlier example, we have added four more significant
features to our C++ example:

1. Objects can be defined on the heap or on the stack. We used
the heap in the earlier example but this one uses the stack

2. We have added character handling (through pointers and also
through copying strings)

3. Rather than come up with lots of names for the same thing, we
have used "this" to indicate the object variables.

4. We have defined a static variable and method

5. We have shown overloading - multiple methods of the same name
but with different calling sequences.

// Second demo / test - will split into multiple files later

// Define the API to the class
// ---------------------------

class Activity {
                Activity(char *descriptor, float minutes, float cals);
                Activity(char *descriptor, float minutes, float cals, char *where);
                float getCalpm();
                char *getDesc();
                char *getLocation();
                        // Above, one per object.
                        // Below, one per class!
                static int getCount();
                float mins;
                float cals;
                char *description; // Option 1 - pointer
                char location[21]; // Option 2 - hold it here
                        // Above, one per object.
                        // Below, one per class!
                static int options_count;

// Define the class itself
// -----------------------

// We have modified the name of the variable within the object
// to be the same as the name used in the call (avoids the need
// for lots of different words for the same thing), and we can
// now refer to them as "mins" for the local variable and
// "this->mins" for the object variable

Activity::Activity(char *description, float mins, float cals) {
        this->mins = mins;
        this->cals = cals;
         // Remember that following copies just the POINTER and
         // the data remains in variables outside the object space
         // which may have a different persistance.
        this->description = description;
         // Alternative - set up and store string in the object
         // (a) String is with object [better than other approach]
         // (b) String has fixed length [worse than other approach]
        *(this->location) = '\0';

Activity::Activity(char *description, float mins, float cals, char *where) {
        this->mins = mins;
        this->cals = cals;
        this->description = description;
        // we need to copy the string! ...

float Activity::getCalpm() {
        return cals / mins;

char * Activity::getDesc() {
        return description;

char * Activity::getLocation() {
        return location;

int Activity::getCount() {
        return options_count;

/* Please note that using a static variable to count the number
of objects of a type you have is pretty nasty ... but it makes a
good demo. Typically, static variables will be internal things
like the name of a temporary work file or a database password. If
you use them as a counter, you have a problem when you user wants
fo count all of the object <i>with a certain characteristic!</i> */

int Activity::options_count = 0;

// Main test program - what do want to be able to do with activities?
// ------------------------------------------------------------------

int main () {

        // Set up objects on the stack / current area.
        // This means the object's data is LOST at function end!

        Activity Ramble = Activity("Walk da dog",60.0,1000.0," in da field");
        Activity Foxtrot = Activity("With Widdy",3.0,400.0);

        cout << Ramble.getDesc() << " burns up " <<
                        Ramble.getCalpm() << " c.p.m at " <<
                        Ramble.getLocation() << endl;
        cout << Foxtrot.getDesc() << " burns up " <<
                        Foxtrot.getCalpm() << " c.p.m at " <<
                        Foxtrot.getLocation() << endl;
        cout << "Object count " << Activity::getCount() << endl ;
        return 0;

/* Sample Output

wizard:cppcsr graham$ ./act_02
Walk da dog burns up 16.6667 c.p.m at in da field
With Widdy burns up 133.333 c.p.m at
Object count 2
wizard:cppcsr graham$


Learn about this subject
This module and example are covered as required on private courses. Should you wish to cover this example and associated subjects, and you're attending a public course to cover other topics with us, please see our extra topic program.

Books covering this topic
Yes. We have over 700 books in our library. Books covering C and C++ are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "this" training module. You'll find a description of the topic and some other closely related examples on the "this" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Web site author
This web site is written and maintained by Well House Consultants.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 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/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb