Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming COP3330 / CGS5409

Similar presentations


Presentation on theme: "Object Oriented Programming COP3330 / CGS5409"— Presentation transcript:

1 Object Oriented Programming COP3330 / CGS5409
Recitation Week 10 Object Oriented Programming COP3330 / CGS5409

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

3 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 C- strings

4 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.

5 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 str1[40]; cout << str1; // insertion operator for strings cin >> str1; // extraction, reads up to white space cin.get(str1, 40, ','); // reads to delimeter (comma) cin.getline(sr1, 40); // reads to delimiter (default delimiter // is newline), discards delimiter

6 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. 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:

7 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 (str1 == str2) // For c-strings, use strcmp: if (strcmp(str1, str2) == 0) // then strings are the same

8 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

9 example String class BString class

10 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

11 Questions?


Download ppt "Object Oriented Programming COP3330 / CGS5409"

Similar presentations


Ads by Google