Recitation Week 10 Object Oriented Programming COP 3330

Recitation Week 10 Object Oriented Programming COP 3330 / CGS 5409

Today’s Recitation � c-strings vs. concept of string class � Dynamic review � Assignment 5

C-style strings • Recall that a C-string is implemented as a null-terminated array of type char • No built-in string type in C. Must use character arrays • • NOT every character array is a C-string. Only when terminated with the null-character String literals in code ("Hello World", "John Smith") are implemented as constant Cstrings

The <cstring> library • • Contains functions for common string operations, such as copy, compare, concatenate, length, search, tokenization, and more strlen() strcpy(), strncpy() strcat(), strncat() strcmp(), strncmp() strstr() strtok() etc.

Special features in <iostream> • Special built-in functions for I/O handling of C-style strings, like the insertion and extraction operators, get(), getline(), etc char str 1[40]; cout << str 1; cin >> str 1; cin. get(str 1, 40, ', '); cin. getline(sr 1, 40); // // // insertion operator for strings extraction, reads up to white space reads to delimeter (comma) reads to delimiter (default delimiter is newline), discards delimiter

The DOWN side of C-strings � � Fixed length, when declared as a normal C-style array This is fine for things like database fields, where a fixed size on each field is necessary Not so flexible for storing just any old string name really just acts as a pointer, since it's the name of an array As such, always must be passed in and out of functions by address -- i. e. with pointer parameter type or return type Must be careful of array boundaries. Overflow of string boundary not automatically detected Less intuitive function calls for common operations, like comparison or copying:

The DOWN side of C-strings // Consider: I want to assign "Hello World" to a string called greeting: char greeting[40]; // can I now say the following? (Which would be pretty intuitive) greeting = "Hello World"; // Legal? NO! // Instead, I have to do it this way, with the strcpy function: strcpy(greeting, "Hello World"); // Comparison: This call doesn't compare contents, only pointers: if (str 1 == str 2) // For c-strings, use strcmp: if (strcmp(str 1, str 2) == 0) // then strings are the same

Building a String class • • Use dynamic allocation and resizing to get flexible capacity • Do all dynamic memory management inside the class, so the user of string objects doesn't have to! Use operator overloads if more intuitive notations are desired • insertion, extraction operators for easy I/O • comparison operators for easy comparisons, sorting, etc. • operator+ for concatenation, if desired • • • Build copy constructor and assignment operator to for correct assignment and pass-by-value abilities Build a conversion constructor to convert c-style strings to string objects, for automatic type conversions! Could include conversion constructors for converting other types to strings, too

example String class � BString class

Assignment 5 intro/issues • • • Don't wait until the last minute to start this one!!! Use good principles like we did in the BString example (and other DMA examples) separate out the memory management functions from the algorithmic features. • This means, avoid doing TONS of "new" and "delete" calls everywhere -- restrict those to a select set of functions. This will make your life easier as you code this • Decide on storage first, and then make sure all of your functions adhere to whatever rules you set up in advance for your storage

Questions?
- Slides: 11