C Programming From Problem Analysis to Program Design

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: User-Defined Simple Data Types and the string Type C++ Programming: From Problem Analysis to Program Design, Third Edition

Objectives In this chapter you will: • Learn how to create and manipulate your own simple data type—called the enumeration type • Explore the string data type, and learn how to use the various string functions to manipulate strings C++ Programming: From Problem Analysis to Program Design, Third Edition 2

Enumeration Type • Data type - a set of values together with a set of operations on those values • To define a new simple data type, called enumeration type, we need three things: − A name for the data type − A set of values for the data type − A set of operations on the values C++ Programming: From Problem Analysis to Program Design, Third Edition 3

Enumeration Type (continued) • A new simple data type can be defined by specifying its name and the values, but not the operations • The values must be identifiers C++ Programming: From Problem Analysis to Program Design, Third Edition 4

Enumeration Type (continued) • The syntax for enumeration type is: • value 1, value 2, … are identifiers called enumerators • value 1 < value 2 < value 3 <. . . C++ Programming: From Problem Analysis to Program Design, Third Edition 5

Enumeration Type (continued) • Enumeration type is an ordered set of values • If a value has been used in one enumeration type − It cannot be used by another in the same block • The same rules apply to enumeration types declared outside of any blocks C++ Programming: From Problem Analysis to Program Design, Third Edition 6

C++ Programming: From Problem Analysis to Program Design, Third Edition 7

C++ Programming: From Problem Analysis to Program Design, Third Edition 8

C++ Programming: From Problem Analysis to Program Design, Third Edition 9

Assignment • The statement: popular. Sport = FOOTBALL; stores the word FOOTBALL into popular. Sport • The statement: my. Sport = popular. Sport; copies the contents of the variable popular. Sport into my. Sport C++ Programming: From Problem Analysis to Program Design, Third Edition 10

Operations • No arithmetic operation is allowed on enumeration types • The following statements are illegal: C++ Programming: From Problem Analysis to Program Design, Third Edition 11

Operations (continued) • The increment and decrement operations are not allowed on enumeration types • The following statements are illegal: C++ Programming: From Problem Analysis to Program Design, Third Edition 12

Operations (continued) C++ Programming: From Problem Analysis to Program Design, Third Edition 13

Operations and Input/Output • Because an enumeration is an ordered set of values − We can use relational operators with them • The cast operator can be used to increment, decrement, and compare values − Values can be used in loops • Input and output are defined only for built-in data types such as int, char, double • The enumeration type can be neither input nor output (directly) C++ Programming: From Problem Analysis to Program Design, Third Edition 14

Functions and Enumeration Types • Enumeration types can be passed as parameters to functions either by value or by reference • A function can return a value of the enumeration type C++ Programming: From Problem Analysis to Program Design, Third Edition 15

#include <iostream> using namespace std; enum day { Sun=1, Mon, Tue, Wen, Thu, Fri, Sat}; void main() { day today, tomorrow; today = Sun; tomorrow= static_cast<day>(today+1); cout<<today<<" "<<tomorrow<<endl; switch(today) { case 1: cout<<"today is Sunday"; break; case 2: cout<<"today is Monday"; break; case 3: cout<<"today is Tuesday"; } } Output is: 12 today is Sunday C++ Programming: From Problem Analysis to Program Design, Third Edition 16

The string Type • To use the data type string, the program must include the header file <string> • The statement: string name = "William Jacob"; declares name to be a string variable and also initializes name to "William Jacob" • The first character, 'W', in name is in position 0; the second character, 'i', is in position 1, and so on C++ Programming: From Problem Analysis to Program Design, Third Edition 17

The string Type (continued) • The variable name is capable of storing any size string • Binary operator + (to allow the string concatenation operation), and the array subscript operator [], have been defined for the data type string • For example, If str 1 = "Sunny", the statement stores the string "Sunny Day" into str 2: str 2 = str 1 + " Day"; C++ Programming: From Problem Analysis to Program Design, Third Edition 18

length Function • Length returns the number of characters currently in the string • The syntax to call the length function is: str. Var. length() where str. Var is variable of the type string • length has no arguments • length returns an unsigned integer • The value returned can be stored in an integer variable C++ Programming: From Problem Analysis to Program Design, Third Edition 19

C++ Programming: From Problem Analysis to Program Design, Third Edition 20

size Function • The function size is same as the function length • Both functions return the same value • The syntax to call the function size is: str. Var. size() where str. Var is variable of the type string • As in the case of the function length, the function size has no arguments C++ Programming: From Problem Analysis to Program Design, Third Edition 21

find Function • find searches a string for the first occurrence of a particular substring • Returns an unsigned integer value of type string: : size_type giving the result of the search • The syntax to call the function find is: str. Var. find(str. Exp) where str. Var is a string variable and str. Exp is a string expression evaluating to a string • The string expression, str. Exp, can also be a character C++ Programming: From Problem Analysis to Program Design, Third Edition 22

find Function (continued) • If successful, find returns the position in str. Var where the match begins • For the search to be successful the match must be exact • If unsuccessful, find returns the special value string: : npos (“not a position within the string”) C++ Programming: From Problem Analysis to Program Design, Third Edition 23

C++ Programming: From Problem Analysis to Program Design, Third Edition 24

substr Function • substr returns a particular substring of a string • The syntax to call the function substr is: str. Var. substr(expr 1, expr 2) where expr 1 and expr 2 are expressions evaluating to unsigned integers C++ Programming: From Problem Analysis to Program Design, Third Edition 25

substr Function (continued) • The expression expr 1 specifies a position within the string (starting position of the substring) • The expression expr 2 specifies the length of the substring to be returned C++ Programming: From Problem Analysis to Program Design, Third Edition 26

C++ Programming: From Problem Analysis to Program Design, Third Edition 27

swap Function • swap interchanges the contents of two string variables • The syntax to use the function swap is str. Var 1. swap(str. Var 2); where str. Var 1 and str. Var 2 are string variables • Suppose you have the following statements: string str 1 = "Warm"; string str 2 = "Cold"; • After str 1. swap(str 2); executes, the value of str 1 is "Cold" and the value of str 2 is "War" C++ Programming: From Problem Analysis to Program Design, Third Edition 28

Part of the Algorithm Design for the Pig Latin Strings Program • The Big Latin program in the end of this chapter contains three functions one of them is: − rotate - to move first character of str to the end of str C++ Programming: From Problem Analysis to Program Design, Third Edition 29

Function rotate • Takes a string as a parameter • Removes the first character of the string − Places it at end of the string by extracting the substring starting at position 1 until the end of the string, then adding the first character of the string C++ Programming: From Problem Analysis to Program Design, Third Edition 30

Function rotate (continued) C++ Programming: From Problem Analysis to Program Design, Third Edition 31

Summary • An enumeration type is a set of ordered values • Reserved word enum creates an enumeration type • No arithmetic operations are allowed on the enumeration type • Relational operators can be used with enum values • Enumeration type values cannot be input or output directly C++ Programming: From Problem Analysis to Program Design, Third Edition 32

Summary (continued) • An anonymous type is one where a variable’s values are specified without any type name C++ Programming: From Problem Analysis to Program Design, Third Edition 33

Summary (continued) • A string is a sequence of zero or more characters • Strings in C++ are enclosed in double quotation marks • In C++, [] is called the array subscript operator • The function length returns the number of characters currently in the string C++ Programming: From Problem Analysis to Program Design, Third Edition 34

Summary (continued) • The function size returns the number of characters currently in the string • The function find searches a string to locate the first occurrence of a particular substring • The function substr returns a particular substring of a string • The function swap is used to swap the contents of two string variables C++ Programming: From Problem Analysis to Program Design, Third Edition 35
- Slides: 35