Standard C++ Classes
Software development today is increasingly component based. C++ supports various kinds of software building blocks. The simplest of these is the function that we investigated in. A more powerful technique uses built-in and user designed software objects.
C++ is a object oriented programming language. An OO programming language allows the programmer to define, create, and manipulate objects.
String Objects
A string is a sequence of characters, most often used to represent words and names. The C++ standard library provides the class string which specifies string objects.
#include <string>
You may assign a literal character sequence to a string object via the familiar string quotation syntax:
string name = "joe";
std::cout << name << '\n';
name = "jane";
std::cout << name << '\n';
string name1 = "joe", name2;
name2 = name1;
std::cout << name1 << " " << name2 << '\n';
- operator[]—provides access to the value stored at a given index within the string
- operator+=—appends a string or single character to the end of a string object
- at—provides bounds-checking access to the character stored at a given index
- length—returns the number of characters that make up the string • size — returns the number of characters that make up the string (same as length)
- find — locates the index of a sub string within a string object •
- substr — returns a new string object made of a sub string of an existing string object
- empty — returns true if the string contains no characters; returns false if the string contains one or more characters
- clear—removes all the characters from a string
string word = "computer";
std::cout << "\"" << word << "\" contains " << word.length()
<< " letters." << '\n';
"computer" contains 8 letters.
We see that operator[] works exactly like its namesake in the std::vector class
#include <iostream>
#include <string>
int main() {
// Declare a string object and initialize it
std::string word = "fred";
// Prints 4, since word contains four characters
std::cout << word.length() << '\n';
// Prints "not empty", since word is not empty
if (word.empty())
std::cout << "empty\n";
else
std::cout << "not empty\n";
// Makes word empty
word.clear();
// Prints "empty", since word now is empty
if (word.empty())
std::cout << "empty\n";
else
std::cout << "not empty\n";
// Assign a string using operator= method
word = "good";
// Prints "good"
std::cout << word << '\n';
// Append another string using operator+= method
word += "-bye";
// Prints "good-bye"
std::cout << word << '\n';
// Print first character using operator[] method
std::cout << word[0] << '\n';
// Print last character
std::cout << word[word.length() - 1] << '\n';
// Prints "od-by", the substring starting at index 2 of length 5
std::cout << word.substr(2, 5);
std::string first = "ABC", last = "XYZ";
// Splice two strings with + operator
std::cout << first + last << '\n';
std::cout << "Compare " << first << " and ABC: ";
if (first == "ABC")
std::cout << "equal\n";
else
std::cout << "not equal\n";
std::cout << "Compare " << first << " and XYZ: ";
if (first == "XYZ")
std::cout << "equal\n";
else
std::cout << "not equal\n";
}