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
 
C++ - unknown array size, unknown object type. Help!

Question MarkThere are time when you're writing a program where you say "I don't know".

• I don't know how many records / object I'm going to read from a data file into an array - so I can't set the size of my array

• I don't know what sort of object I'll be creating from a line I've read from a data file until I analyse it, so I can't call the constructor directly.

You could write a program to read take up to a certain maximum number of records / objects, but one day you'll find that someone want to run that program with more records than you've allowed for. The solution (C++ answer / term) is to use a vector - which is line an array or objects in many ways, but can dynamically expand. It saves you writing you own code with realloc calls.

And you could write a program which analyses incoming data lines before calling the appropriate constructor - but by doing so you're moving code which really should be in the class into each application. The solution is to provide a static method in the base class of all possible objects, which analyses the data and decides which constructor to call. Such a method is known as a factory method.

An example showing these techniques - from the C++ course I ran the week before last - is [here]. It also shows some file and string handling - a little bit of everything as I pulled together elements of the C++ language into a very straightforward application, but one covering many aspects.

I might not know how big my array should be, or what type of data each line will contain - but I do know that our next series of C and C++ courses starts at the very end of May - see [here]. If you've missed those, there are further courses in August, and in the autumn - use the same link as we'll update our page.

(written 2011-04-17)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C235 - C and C based languages - I/O in C++
  [4563] Formatting and outputting your own classes in C++ - (2015-10-30)
  [4562] Left shift operator on an output stream object - C++ - (2015-10-30)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3807] Reading (and writing) files in C++ - (2012-07-18)
  [3390] Printing objects in C++ - (2011-08-13)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [1675] Comparing Objects in C++ - (2008-06-13)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)

C238 - C and C based languages - Templates
  [3982] Using a vector within an object - C++ - (2013-01-19)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3388] Templates in C++ - defining a family pattern of methods / functions - (2011-08-12)
  [3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)

C239 - C and C based languages - Putting it all together
  [4559] When do I use the this keyword in C++? - (2015-10-29)
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
  [2851] Further C++ material - view new or old - (2010-07-04)
  [2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2646] Compile but do not run PHP - syntax check only - (2010-02-22)
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)
  [945] Code quality counts - (2006-11-26)
  [925] C++ - just beyond the basics. More you can do - (2006-11-14)
  [836] Build on what you already have with OO - (2006-08-17)


Back to
C++ - objects that are based on other objects, saving coding and adding robustness
Previous and next
or
Horse's mouth home
Forward to
Is this number between? Does this list include? - Ruby
Some other Articles
Displaying a directory or file system tree - Linux
Process every member of an array, and sort an array - Ruby
Multiple inputs, multiple out, ruby functions
Is this number between? Does this list include? - Ruby
C++ - unknown array size, unknown object type. Help!
C++ - objects that are based on other objects, saving coding and adding robustness
C++ - how we teach the language and the concepts behind the language
Events - Spring and Early Summer 2011 in Melksham
What have these pictures in common?
Light and dark at Green Park
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/3252_C-u ... Help-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat