Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Using a vector within an object - C++

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
C234 - C and C based languages - Further C++ Object Oriented features
  [4559] When do I use the this keyword in C++? - (2015-10-29)
  [4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3057] Lots of things to do with and within a C++ class - (2010-11-16)
  [2849] What are C++ references? Why use them? - (2010-07-02)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2576] What does const mean? C and C++ - (2010-01-15)
  [2005] Variables and pointers and references - C and C++ - (2009-01-23)
  [2004] Variable Scope in C++ - (2009-01-22)
  [1819] Calling base class constructors - (2008-10-03)
  [1159] It can take more that one plus one to get two. - (2007-04-22)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [802] undefined reference to typeinfo - C++ error message - (2006-07-15)
  [801] Simple polymorphism example - C++ - (2006-07-14)

C238 - C and C based languages - Templates
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3388] Templates in C++ - defining a family pattern of methods / functions - (2011-08-12)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)


Back to
Melksham - a town of some remarkable people
Previous and next
or
Horse's mouth home
Forward to
Upcoming courses at Well House Manor - Ruby, Python, Perl
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 Wiltshire
20 minutes in to our 15 minutes of fame
Upcoming courses at Well House Manor - Ruby, Python, Perl
Using a vector within an object - C++
Melksham - a town of some remarkable people
Well House Manor in the snow
Extended and Associated objects - what is the difference - C++ example
Teaching OO - how to avoid lots of window switching early on
A variety of continental breakfasts
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3982_Usi ... ct-C-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat