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))
Second C++ demo - heap v stack, char handling, this, static
Further C++ Object Oriented features example from a Well House Consultants training course
More on Further C++ Object Oriented features [link]

This example is described in the following article(s):
   • Lots of things to do with and within a C++ class - [link]

Source code: act_02.cpp Module: C234
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 {
        public:
                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();
        private:
                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) {
        Activity::options_count++;
        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) {
        Activity::options_count++;
        this->mins = mins;
        this->cals = cals;
        this->description = description;
        // we need to copy the string! ...
        strcpy(this->location,where);
        }

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 on the following public courses:
 * Learning to program in C and C++
 * C++ for C Programmers
 * C and C++ Programming
 * Learning to program in C and C++
 * C and C++ Programming
Also available on on site courses for larger groups

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 "Further C++ Object Oriented features" training module. You'll find a description of the topic and some other closely related examples on the "Further C++ Object Oriented features" 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., 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/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb