CS 1440 Lab 6

Cover sheetPrevious ActivityNext Activity

Activity 6-2: New Data Types

Activity Goals

Activity Procedure

While the char C++ base data type is not new to you, we haven't really used or discussed it much.  That's about to change!  A new data type is the string.  To use this new data type you must have the following statement in your program.

#include  <string>

A string is a sequence of characters.  The string data type is not a C++ base data type.  We'll get into what this really means in a couple of weeks, but for now just realize that variables of these special types can do some special things.

All variables, regardless of its data type are declared in the same way you already know.  So, to declare a char variable and a string variable, you simply do something similar to this:
          char  ch1, ch2;
          string  str1, str2;

You can assign values using constants:
          ch1  =  'A';
          str1  =  "Hello World!";

You can assign values using other variables of the type:
          ch2  =  ch1;
          str2  =  str1;

You can obtain values from the user:
          cin  >>  ch1;
          cin  >>  str1;

(More on this in the next activity.)

You can also:

        output using cout as you would expect

        pass as parameters to functions

        return from functions.

We're introducing the C++ string type to you because we want you to be able to read in the names of files. Using strings is much safer than using character arrays (as used in Chapter 5 of the text and in earlier versions of this lab). We're not going to tell you much about strings now -- just enough to do the job at hand. We'll address strings in more detail later -- for the time being, have faith and use the constructions this lab shows you.

Activity Followup

Nothing here. The following activities and the postlab will have you use strings, and they'll tell you what to do.