Using a vector within an object - C++
Archive - Originally posted on "The Horse's Mouth" - 2013-01-19 14:36:02 - Graham EllisBackground - 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!