Strings: C-strings vs. strings as objects

C-style strings

Library features

We have some features in the standard C++ libraries available to help us work more easily with C-style strings:

The DOWN side of C-strings

C-strings are pretty efficient, since they are just character arrays. If you need a fixed size string that works fast, this is a great option. But the user of the c-string has the responsibility for calling the library functions correctly. They are not foolproof!

Some potential downsides of c-strings:

The Almighty Null Character?

One of the larger pitfalls... Library functions for dealing with c-strings are usually based on the expectation of a null-character to stop the loop that is processing the c-string's contents. Is this ideal?

A string wish list

As we enter the fantasy realm where only ideal strings abide, we ask: how should they behave?

Different developers might have different notions, but here are some basic properties that it would be nice to have for strings:

Building a String class

We can make the fantasy a reality! Just build a class to create a new string type, which incorporates any desired features.

An example String class

Here's a link to the start of a string class -- partial only (in progress). This reflects the code we did from scratch in class.

As an exercise, try and fill in the other functions that are not yet defined here. Also add test calls into the driver program to test the other features.