Using Standard Classes

Sample program using class string

// To use strings.
 
#include <iostream>    // Declaration of cin, cout
#include <string>      // Declaration of class string
using namespace std;
   
int main()
{
   // Defines four strings:
   string prompt("What is your name:  "),
          name,                  // An empty
          line( 40, '-'),        // string with 40 '-'
          total = "Hello ";      // is possible!
   
   cout << prompt;          // Request for input.
   getline( cin, name);     // Inputs a name in one line
   
   total = total + name;     // Concatenates and
                             // assigns strings.
   
   cout << line << endl      // Outputs line and name
        << total << endl;
   cout << " Your name is "  // Outputs length
        << name.length() << " characters long!" << endl;
   cout << line << endl;
   return 0;
} 

Note 
Both the operators + and += for concatenation and the relational operators <, <=, >, >=, ==, and != are defined for objects of class string. Strings can be printed with cout and the operator <<.
     The class string will be introduced in detail later on.

Sample screen output

What is your name: Rose Summer
---------------------------------------
Hello Rose Summer
Your name is 11 characters long!
--------------------------------------- 
Several classes are defined in the C++ standard library. These include stream classes for input and output, but also classes for representing strings or handling error conditions.
Each class is a type with certain properties and capacities. As previously mentioned, the properties of a class are defined by its data members and the class's capacities are defined by its methods. Methods are functions that belong to a class and cooperate with the members to perform certain operations. Methods are also referred to as member functions.

Creating Objects

An object is a variable of a class type, also referred to as an instance of the class. When an object is created, memory is allocated to the data members and initialized with suitable values.
Example:
string s("I am a string"); 
In this example the object s, an instance of the standard class string (or simply a string), is defined and initialized with the string constant that follows. Objects of the string class manage the memory space required for the string themselves.
In general, there are several ways of initializing an object of a class. A string can thus be initialized with a certain number of identical characters, as the example on the opposite page illustrates.

Calling Methods

All the methods defined as public within the corresponding class can be called for an object. In contrast to calling a global function, a method is always called for one particular object. The name of the object precedes the method and is separated from the method by a period.
Example:
s.length();                    // object.method(); 
The method length() supplies the length of a string, i.e. the number of characters in a string. This results in a value of 13 for the string s defined above.

Classes and Global Functions

Globally defined functions exist for some standard classes. These functions perform certain operations for objects passed as arguments. The global function getline(), for example, stores a line of keyboard input in a string.
Example:
getline(cin, s); 
The keyboard input is terminated by pressing the return key to create a new-line character, '\n', which is not stored in the string.