cs31 lecture 17

109 reminder imperative programs x an imperative

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: xample: std::string mystring; 106 Key differences x A C string is nothing more than an array All limitations apply: size is fixed for statically allocated C strings Works as an array, need to implement functions to manipulate Works individual elements (which are char)
 individual x A C++ string is a class, with methods and attributes The character at position 42 of the string s can be accessed with The s[42] (that is, the 43rd character)
 s[42] The declaration is different. To declare a string of 512 elements, The do: do: std::string mystring(512, ‘\0’)
; std::string 107 The 0 character x A string must be null-terminated, that is, the last useful string character of the string is the first character with value 0 character Example: char foo[4] = {‘b’, ‘a’, ‘r’, ‘\0’ }; x The character ‘\0’ has value 0, and so is the null- terminating character x When declaring a string, must be careful to keep space for When the null character! the Example: a string for the word ‘foo’, 3 characters, must be of size Example: 108 at least 4 at Object-Oriented Programming (OOP) x Finally! Finally! x Object-Oriented Progra...
View Full Document

Ask a homework question - tutors are online