From the course: Complete Guide to C++ Programming Foundations

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

The string class

The string class

- [Instructor] Now let me show you how the string class from the C++ standard library provides a more flexible and safer way to handle strings when compared to cstrings. Well, C++ provides a class to represent strings as objects with properties and functions. This is the object oriented approach to strings that's embedded in the language. Here we have the same code we created for reporting the winner in a race game using C style strings, which are just a race of characters. I will replace some lines of code in this file to use the string class instead. Let's start by replacing the cstring header with the string header, which contains the definition of the string class. Now, I'll remove the declaration of the length constant because the string class manages the length of its strings automatically. There's no need for you to worry about that. Now let's replace our cstring declarations with std string objects. We'll declare racer 1 and racer 2 as std strings. Now be aware that we can…

Contents