Chapter 4

22
Chapter 4 Strings and Screen I/O

description

Chapter 4. Strings and Screen I/O. Objectives. Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform basic string operations. Use cin and cout. Use special characters. Format output. Accept characters and strings as input. - PowerPoint PPT Presentation

Transcript of Chapter 4

Page 1: Chapter 4

Chapter 4

Strings and Screen I/O

Page 2: Chapter 4

Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform basic string operations. Use cin and cout. Use special characters. Format output. Accept characters and strings as input.

Page 3: Chapter 4

Strings and Literals Recall that a group of characters

put together is a string. C++ does not have a data type for

strings. Hard coded values or strings are

called literals. Literals can be numeric literals,

string literals, or character literals.

Page 4: Chapter 4

Classes and Objects An object-oriented string data type

is referred to as a string class. A string class is actually a definition

used to create a string object. A class is a generic definition from

which an object is created. An object is said to be an instance

of a class.

Page 5: Chapter 4

Using the String Class We will use the apstring class

which is made up of two files: apstring.h and apstring.cpp.

Declaring a string object is much like declaring other variables:apstring MyString1;

apstring MyString2("ABCDEF");

Page 6: Chapter 4

Assigning Strings to String Objects

1. You can assign the contents of one string object to another string object.

MyString1 = MyString2; 2. You can assign a string literal to a string object.

MyString1 = "string literal"; 3. You can assign a character literal to a string object.

MyString1 = 'A';

Page 7: Chapter 4

Messages One of the important concepts behind

the use of objects is the idea of containment (or encapsulation).

An object hides the details of how data is stored and how operations work.

To make the object do what we want it to do, we send the object a message.

Page 8: Chapter 4

Obtaining the Length of a String The message used to obtain the length

of a string is simply length. l = MyString2.length();

MyString2 is the name of the string that we want to send a message to.

The dot operator separates the name from the message.

The code inside the object that performs the length operation is called a method.

Page 9: Chapter 4

String Concatenation Concatenation means adding one

string onto the end of another string.

The + operator can be used to concatenate strings.

MyString1 = MyString1 + ' ' + MyString2;

The += operator can also be used.MyString1 += MyString2;

Page 10: Chapter 4

Using cin and cout The stream that brings data from your

keyboard is cin, and the stream that takes data to your screen is cout.

The >> operator is also referred to as the extraction operator because it extracts data from the stream.

The << operator is also referred to as the insertion operator because it inserts data into the stream.

Page 11: Chapter 4

New Line and Other Special Characters

The \n character is called the new line character or the end-of-line character.

endl can be used in place of the \n.cout << i << '\n';cout << i << endl;

Other special characters include: \t for a tab \\ for a backslash\' for a single quote \" for a double quote

Page 12: Chapter 4

Using setf and unsetf The cout object has format options

that can be changed by using setf and unsetf.

An example of the syntax:cout.setf(ios::right);

Formatting options include: left, right, showpoint, uppercase, showpos, scientific, and uppercase.

Page 13: Chapter 4

Using the I/O Manipulators The most common I/O

manipulators in C++ are setprecision and setw.

setprecision is used to set the number of digits that will appear to the right of the decimal point.

setw is used to change the number of spaces the compiler uses when it displays a number.

Page 14: Chapter 4

I/O Manipulator Examples

Using setprecisioncout << setprecision(2) << total;

Using setwcout << setw(10) << i << setw(10) << j;

Page 15: Chapter 4

Inputting Characters The >> operator can be used to

input characters. If the user enters more than one

character, only the first character will be stored in the variable.

Page 16: Chapter 4

Inputting Strings The getline method is used to

input strings entered by the user. Examplecout << "Enter your first name: ";

getline(cin, FirstName);

Page 17: Chapter 4

Flushing the Input Stream After you have input a number using a

cin statement, the new line character that is generated when you press Enter stays in the input stream.

You must remove the extra characters from the input stream before the getline method is executed.

You can use the following statement to "flush" the stream:

cin.ignore(80, '\n');

Page 18: Chapter 4

Summary Strings allow computers to process

text as well as numbers. Hard-coded numeric values are

called numeric literals. Hard-coded text is called a string literal.

A class is a definition used to create an object. An object is said to be an instance of a class.

Page 19: Chapter 4

Summary You can use cout to display the

contents of a string object. To make an object perform an

operation on itself, you send the object a message.

The length method is used to determine the length of a string stored in a string object.

Page 20: Chapter 4

Summary Concatenation is the operation of

adding one string onto the end of another string.

The << and >> symbols are actually operators. The cin and cout keywords are actually objects.

The cin and cout objects are streams. A stream is data flowing from one place to another.

Page 21: Chapter 4

Summary The \n character is a special character

called the new line character or end-of-line character.

There are special characters for printing characters such as tab, the backslash, and quotes.

You can use endl in place of '\n'. The cout object has format options that

can be changed with the setf and unsetf methods.

Page 22: Chapter 4

Summary setprecision is used to set the number of

digits that will appear to the right of the decimal point.

setw is used to set the field width. The >> operator can be used to input

characters. To input strings, use the getline method of

the string class. It is sometimes necessary to flush the input

stream to remove characters left in it.