Background - why?
It's very common in program to have a whole lot of objects which when collected together make another object of a different type. That different object isn't going to be part of the same inheritance tree either - it's simply an associated object type / class.
That's a bit of an abstact statement! Let's see a couple of examples:
1. I have
Request objects for each request for a piece of data / file / resource from a web server. I also have
visit objects which contain a series of one or more
request objects; methods I might want to run on my
visit objects include
getvisitlength and
gettotalbyes.
2. I have
Transport objects, subclassed to
Bus and
Train objects, each of which represents a bus / train route. I then collect a whole series of different
Transport objects into a
Flow object, in which I have all the various different routes in the same corridor ... from Chippenham to Trowbridge that would be the X34 bus, the 234 bus, perhaps the ZigZag bus and the occasional train.
As you see - a common requirement. How do I implement it?
If I knew ahead of time how many
requests I had in the biggest
visit, or how many
Transports I had on my biggest
Flow, I could use an array. Problem is - I'm unlikely to know ahead of time, and the numbers could end up quite high. You've got 16 regular bus routes on Oxford Street, plus night services, Central line tubes, and Crossrail to be added, and you can't go allocating memory into arrays "just in case".
So you should use a vector for your implementation.
How?
Here's my second example (Transport and Flow) as implemented on the
C++ for C Programmers course that concluded yesterday.
1. Within my
Flow class header:
class Flow {
public:
Flow(int);
void add(Transport *);
int getunits();
private:
vector<Transport *> route ;
} ;
2. My Flow constructor does
not require any code to initialise the vector as that happens when the member variable is created via the vector template. I have called my Flow constructor from the test program like:
Flow *chippenham_trowbridge = new Flow(6);
and note that the parameter "6" is simply maintaining compatability with a previous examples that used an array and had to set a maximum size.
3. Here's my method to add a new
Transport to a
Flow
void Flow::add(Transport * item) {
route.push_back(item);
}
and here's an example of code that's calling that add method
chippenham_trowbridge->add(new Train(1,69));
chippenham_trowbridge->add(new Bus(2,61));
4. When I get the number of vehicles in use on a flow, I write
int vehiclesonflow = chippenham_trowbridge->getunits();
and display it via
cout << "Vehicles on flow: " << vehiclesonflow << endl;
5. Here's the implementation of getunits:
int Flow::getunits() {
int so_far = 0;
for (int k=0; k<route.size(); k++) {
so_far += route.at(k)->getveh();
}
return so_far;
}
Complete code (handling a few more attributes)
[here] ... compiling and running that code, I got the following results:
trainee@brugges:~/cpp13$ g++ -o trans trans.cpp
trainee@brugges:~/cpp13$ ./trans
Seats on flow: 418
Vehicles on flow: 6
Drivers on flow: 5
trainee@brugges:~/cpp13$
I've documented this example quite fully, as I've found it very hard to locate an example of a vector member of an object in a straightforward online example. Hope the above helps! (written 2013-01-19)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C238 - C and C based languages - Templates [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)
[3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
[3388] Templates in C++ - defining a family pattern of methods / functions - (2011-08-12)
[3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
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)
[2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
[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)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[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)
[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)
[4559] When do I use the this keyword in C++? - (2015-10-29)
Some other Articles
Rail services in Wiltshire - up in the air? We need to say what is best required.Special weekend at Well House Manor - come and see Wiltshire20 minutes in to our 15 minutes of fameUpcoming courses at Well House Manor - Ruby, Python, PerlUsing a vector within an object - C++ Melksham - a town of some remarkable peopleWell House Manor in the snowExtended and Associated objects - what is the difference - C++ exampleTeaching OO - how to avoid lots of window switching early onA variety of continental breakfasts