The Craft Of Coding

CPP 6

Posted by Pete Sat, 02 Jan 2016 21:56:00 GMT

C++ week 6

More on Classes in C++

Strings in C++ are instance of the string class and as such have a lot of specific behaviours that are implemented by the class.

Using the string Class

There are more string constructors than we needed for our Odometer class, but that is because there are many ways in which it can be useful to create a string.

  • default constructor to build an empty string
  • copy constructor
  • substring constructor - build string from part of another string
  • from C string char * s = "abc"; - either up to null terminator or n chars
  • fill constructor
  • range constructor (similar to substring)
  • 2011 C++ from initializer_list {'a', 'b', 'c'}
  • 2011 C++ move constructor

Coding Task

Use the string class to break up a sentence into separate strings for each word. Punctuation and white space should be considered as word delimiters. Use the Terry Pratchett quote at the bottom of this page as the sentence that you need to break up into individual words.

  1. What questions do you need to ask before starting this task?
  2. Hint: What is underspecified?
  3. Which string methods will you need to use?
  4. Do you need to ask any questions about those methods?
  5. A reminder of the control structures in C++ in case you have forgotten any of them.

© 2015 Pete McBreen. Some humans would do anything to see if it was possible to do it. If you put a large switch in some cave somewhere, with a sign on it saying 'End-of-the-World Switch. PLEASE DO NOT TOUCH', the paint wouldn't even have time to dry. — Terry Pratchett