The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Pstring Variables

You can create a variable with type pstring in the usual ways:

  pstring first;
  first = "Hello, ";
  pstring second = "world.";

The first line creates an pstring without giving it a value. The second line assigns it the string value "Hello." The third line is a combined declaration and assignment, also called an initialization.

Normally when string values like "Hello, " or "world." appear, they are treated as C strings. In this case, when we assign them to an pstring variable, they are converted automatically to pstring values.

We can output strings in the usual way:

  cout << first << second << endl;

In order to compile this code, you will have to include the header file for the pstring class, and you will have to add the file pstring.cpp to the list of files you want to compile. The details of how to do this depend on your programming environment.

Before proceeding, you should type in the program above and make sure you can compile and run it.


Last Update: 2005-12-05