220 likes | 349 Views
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.
E N D
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.
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.
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.
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");
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';
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.
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.
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;
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.
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
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.
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.
I/O Manipulator Examples Using setprecision cout << setprecision(2) << total; Using setw cout << setw(10) << i << setw(10) << j;
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.
Inputting Strings • The getline method is used to input strings entered by the user. • Example cout << "Enter your first name: "; getline(cin, FirstName);
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');
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.
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.
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.
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.
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.