| |||||||||||
| |||||||||||
Object Extras. Vector, delete, factory, destructor, etc
Further C++ Object Oriented features example from a Well House Consultants training course
More on Further C++ Object Oriented features [link]
Source code: act_03.cpp Module: C234
using namespace std;
/* Lots of Object Extras Factories, Destructors, delete, Vector Direct variable access, overriding operators Third example - really showing much more that should be shown in a single demo! */ #include <iostream> #include <string> #include <vector> // 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); ~Activity(); float getCalpm(); char *getDesc(); char *getLocation(); Activity *moreEnergetic(Activity *that); Activity operator+(Activity that); static Activity *mostEnergetic(Activity *that[], int syze); static int getCount(); // Factory Method - concept demo! static Activity *myFactory(char *descriptor,float minutes,float cpm); // Note - next is direct access to a variable (Yuk!) // (Yuk because we can't put code between value and // caller later on if we need to. Nailed not screwed!) float cals; private: float mins; char *description; // Option 1 - pointer char location[21]; // Option 2 - hold it here static int options_count; }; Activity::Activity(char *description, float mins, float cals) { Activity::options_count++; this->mins = mins; this->cals = cals; this->description = description; *(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); } // Factory method // Note // - factories can also return arrays of objects // - factories can choose to return objects of different derived types Activity * Activity::myFactory(char *descriptor,float minutes,float cpm) { float totcal = minutes * cpm; return new Activity(descriptor, minutes, totcal); } // Destructor - automatic for stack variables (heap - beware - use delete!) Activity::~Activity() { cout << this->description << " completed" << endl; } float Activity::getCalpm() { return cals / mins; } char * Activity::getDesc() { return description; } char * Activity::getLocation() { return location; } Activity Activity::operator+(Activity that) { float length = this->mins + that.mins; float totcal = this->cals + that.cals; // Careful - do not return something that's // about to go out of scope and become garbage! return Activity("Combo",length,totcal,"Various"); } Activity * Activity::moreEnergetic(Activity *that) { // Call to internal methods to avoid problems with // inheritance - i.e. take advantage of Polly Morphism. if (this->getCalpm() < that->getCalpm()) return that; return this; } Activity * Activity::mostEnergetic(Activity **that, int syze) { Activity *so_far = that[0]; int k; for (k=1; k<syze; k++) { so_far = so_far->moreEnergetic(that[k]); } return so_far; } int Activity::getCount() { return options_count; } int Activity::options_count = 0; // Main test program - what do want to be able to do with activities? // ------------------------------------------------------------------ int main () { Activity * Weekend[4] ; int k; Weekend[0] = new Activity("Walk da dog",60.0,1000.0," in da field"); Weekend[1] = new Activity("With Widdy",3.0,400.0); // Weekend[2] = new Activity("With Bruce",2.0,200.0); Weekend[3] = new Activity("Walk the goldfish",20.0,50.0,"pondelled"); // Factory (little more than repackaging at the gate!) // Normally - would pass in a string with all the data from a file Weekend[2] = Activity::myFactory("With Brucella",2.0,100.0); for (k=0; k<4; k++) { Activity *Current = Weekend[k]; cout << Current->getDesc() << " burns up " << Current->getCalpm() << " c.p.m at " << Current->getLocation() << endl; } cout << "Object count " << Activity::getCount() << endl ; // Comparator - "more" Activity *MoreEnergy = Weekend[0]->moreEnergetic(Weekend[2]); cout << "more - " << MoreEnergy->getDesc() << endl; // Comparator - "most" Activity *MostEnergy = Activity::mostEnergetic(Weekend,4); cout << "most - " << MostEnergy->getDesc() << endl; // Overriding Operators Activity Saturday = *Weekend[1] + *Weekend[2]; cout << Saturday.getDesc() << " burns up " << Saturday.getCalpm() << " c.p.m at " << Saturday.getLocation() << endl; // Direct variable access cout << Saturday.cals << " Calories in total" << endl; // Getting rid of the heap objects: for (k=0; k<4; k++) { delete Weekend[k]; } // And lets finish this one with a vector! // Used if we don't know how many items there will be vector <Activity *> Todos(0); Todos.push_back(new Activity("eating",45.,-500.)); Todos.push_back(new Activity("sleeping",300.,200.)); Todos.push_back(new Activity("working",400.,1200.)); for (k=0; k<Todos.size(); k++) { Activity * Current = Todos.at(k); cout << Current->getDesc() << " burns up " << Current->getCalpm() << " c.p.m at " << Current->getLocation() << endl; } return 0; } /* Sample Run wizard:cppcsr graham$ ./act_03 Walk da dog burns up 16.6667 c.p.m at in da field With Widdy burns up 133.333 c.p.m at With Brucella burns up 100 c.p.m at Walk the goldfish burns up 2.5 c.p.m at pondelled Object count 4 more - With Brucella most - With Widdy With Brucella completed Combo burns up 120 c.p.m at Various 600 Calories in total Walk da dog completed With Widdy completed With Brucella completed Walk the goldfish completed eating burns up -11.1111 c.p.m at sleeping burns up 0.666667 c.p.m at working burns up 3 c.p.m at Combo completed 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
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. |
| ||||||||||
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 |