C Programming Program Design Including Data Structures Third

  • Slides: 54
Download presentation
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type C++ Programming: Program Design Including Data Structures, Third Edition

Objectives In this chapter you will: • Learn how to create and manipulate your

Objectives In this chapter you will: • Learn how to create and manipulate your own simple data type—called the enumeration type • Become aware of the typedef statement • Learn about the namespace mechanism • Explore the string data type, and learn how to use the various string functions to manipulate strings C++ Programming: Program Design Including Data Structures, Third Edition 2

Enumeration Type • Data type - a set of values together with a set

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: Program Design Including Data Structures, Third Edition 3

Enumeration Type (continued) • A new simple data type can be defined by specifying

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: Program Design Including Data Structures, Third Edition 4

Enumeration Type (continued) • The syntax for enumeration type is: • value 1, value

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: Program Design Including Data Structures, Third Edition 5

Enumeration Type (continued) • Enumeration type is an ordered set of values • If

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: Program Design Including Data Structures, Third Edition 6

C++ Programming: Program Design Including Data Structures, Third Edition 7

C++ Programming: Program Design Including Data Structures, Third Edition 7

C++ Programming: Program Design Including Data Structures, Third Edition 8

C++ Programming: Program Design Including Data Structures, Third Edition 8

C++ Programming: Program Design Including Data Structures, Third Edition 9

C++ Programming: Program Design Including Data Structures, Third Edition 9

Assignment • The statement: popular. Sport = FOOTBALL; stores the word FOOTBALL into popular.

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: Program Design Including Data Structures, Third Edition 10

Operations • No arithmetic operation is allowed on enumeration types • The following statements

Operations • No arithmetic operation is allowed on enumeration types • The following statements are illegal: C++ Programming: Program Design Including Data Structures, Third Edition 11

Operations (continued) • The increment and decrement operations are not allowed on enumeration types

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

Operations (continued) C++ Programming: Program Design Including Data Structures, Third Edition 13

Operations (continued) C++ Programming: Program Design Including Data Structures, Third Edition 13

Operations and Input/Output • Because an enumeration is an ordered set of values −

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: Program Design Including Data Structures, Third Edition 14

Functions and Enumeration Types • Enumeration type can be passed as parameters to functions

Functions and Enumeration Types • Enumeration type 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: Program Design Including Data Structures, Third Edition 15

Anonymous Data Types • Anonymous - a data type in which values are directly

Anonymous Data Types • Anonymous - a data type in which values are directly specified in the variable declaration with no type name, for example: • Creating an anonymous type has drawbacks • We cannot pass an anonymous type as a parameter to a function C++ Programming: Program Design Including Data Structures, Third Edition 16

Anonymous Data Types (continued) • A function cannot return a value of an anonymous

Anonymous Data Types (continued) • A function cannot return a value of an anonymous type • Values used in one can be used in another, but they are treated differently C++ Programming: Program Design Including Data Structures, Third Edition 17

typedef Statement • You can create synonyms or aliases to a previously defined data

typedef Statement • You can create synonyms or aliases to a previously defined data type by using the typedef statement • The syntax of the typedef statement is: • typedef does not create any new data types • typedef creates an alias to an existing data type C++ Programming: Program Design Including Data Structures, Third Edition 18

ANSI/ISO Standard C++ • ANSI/ISO standard C++ was officially approved in July 1998 •

ANSI/ISO Standard C++ • ANSI/ISO standard C++ was officially approved in July 1998 • Most of the recent compilers are also compatible with ANSI/ISO standard C++ • For the most part, standard C++ and ANSI/ISO standard C++ are the same, but − ANSI/ISO Standard C++ has some features not available in Standard C++ Programming: Program Design Including Data Structures, Third Edition 19

Namespaces • When a header file, such as iostream, is included in a program

Namespaces • When a header file, such as iostream, is included in a program − Global identifiers in the header file also become global identifiers in the program • If a global identifier in a program has the same name as one of the global identifiers in the header file − The compiler will generate a syntax error (such as identifier redefined) • The same problem can occur if a program uses third party libraries C++ Programming: Program Design Including Data Structures, Third Edition 20

Namespaces (continued) • To overcome this problem, third party vendors begin their global identifiers

Namespaces (continued) • To overcome this problem, third party vendors begin their global identifiers with a special symbol • Because compiler vendors begin their global identifier with _ (underscore) − To avoid linking errors, do not begin identifiers in your program with _ • ANSI/ISO standard C++ attempts to solve this problem of overlapping global identifier names with the namespace mechanism C++ Programming: Program Design Including Data Structures, Third Edition 21

Syntax: namespace • The syntax of the statement namespace is: where a member is

Syntax: namespace • The syntax of the statement namespace is: where a member is usually a variable declaration, a named constant, a function, or another namespace C++ Programming: Program Design Including Data Structures, Third Edition 22

C++ Programming: Program Design Including Data Structures, Third Edition 23

C++ Programming: Program Design Including Data Structures, Third Edition 23

Accessing a namespace Member • The scope of a namespace member is local to

Accessing a namespace Member • The scope of a namespace member is local to the namespace • Usually two ways a namespace member can be accessed outside the namespace • One way is to use the syntax: namespace_name: : identifier • To access the member rate of the namespace global. Type, the following statement is required: global. Type: : RATE C++ Programming: Program Design Including Data Structures, Third Edition 24

Accessing a namespace Member (continued) • To access the function print. Result, the following

Accessing a namespace Member (continued) • To access the function print. Result, the following statement is required: global. Type: : print. Result(); • To simplify the accessing of all namespace members: using namespace_name; • To simplify the accessing of a specific namespace member: using namespace_name: : identifier; C++ Programming: Program Design Including Data Structures, Third Edition 25

The using Statement • After the using statement − Not necessary to precede the

The using Statement • After the using statement − Not necessary to precede the namespace_name and the scope resolution operator before the namespace member • If a namespace member and a global identifier or a block identifier have the same name − namespace_name and scope resolution operator must precede the namespace member C++ Programming: Program Design Including Data Structures, Third Edition 26

The string Type • To use the data type string, the program must include

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 in name, 'W', is in position 0, the second character, 'i', is in position 1, and so on C++ Programming: Program Design Including Data Structures, Third Edition 27

The string Type (continued) • The variable name is capable of storing any size

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: Program Design Including Data Structures, Third Edition 28

length Function • Length returns the number of characters currently in the string •

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: Program Design Including Data Structures, Third Edition 29

C++ Programming: Program Design Including Data Structures, Third Edition 30

C++ Programming: Program Design Including Data Structures, Third Edition 30

size Function • The function size is the same as the function length •

size Function • The function size is the 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: Program Design Including Data Structures, Third Edition 31

find Function • find searches a string for the first occurrence of a particular

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: Program Design Including Data Structures, Third Edition 32

find Function (continued) • If successful, find returns the position in str. Var where

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: Program Design Including Data Structures, Third Edition 33

C++ Programming: Program Design Including Data Structures, Third Edition 34

C++ Programming: Program Design Including Data Structures, Third Edition 34

substr Function • substr returns a particular substring of a string • The syntax

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: Program Design Including Data Structures, Third Edition 35

substr Function (continued) • The expression expr 1 specifies a position within the string

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: Program Design Including Data Structures, Third Edition 36

C++ Programming: Program Design Including Data Structures, Third Edition 37

C++ Programming: Program Design Including Data Structures, Third Edition 37

swap Function • swap interchanges the contents of two string variables • The syntax

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: Program Design Including Data Structures, Third Edition 38

Programming Example: Pig Latin Strings • Program prompts user to input a string −

Programming Example: Pig Latin Strings • Program prompts user to input a string − Then outputs the string in the pig Latin form • The rules for converting a string into pig Latin form are as follows: 1. If the string begins with a vowel, add the string "-way" at the end of the string − For example, the pig Latin form of the string "eye" is "eye-way" C++ Programming: Program Design Including Data Structures, Third Edition 39

Pig Latin Strings (continued) 2. If the string does not begin with a vowel,

Pig Latin Strings (continued) 2. If the string does not begin with a vowel, first add "-" at the end of the string − Then move the first character of the string to the end of the string until the first character of the string becomes a vowel − Next, add the string "ay" at the end − For example, the pig Latin form of the string "There" is "ere-Thay" C++ Programming: Program Design Including Data Structures, Third Edition 40

Pig Latin Strings (continued) 3. Strings such as "by" contain no vowels − In

Pig Latin Strings (continued) 3. Strings such as "by" contain no vowels − In cases like this, the letter y can be considered a vowel − For this program, the vowels are a, e, i, o, u, y, A, E, I, O, U, and Y; the pig Latin form of "by" is "y-bay" 4. Strings such as "1234" contain no vowels − The pig Latin form of a string that has no vowels in it is the string followed by the string "-way" − For example, the pig Latin form of the string "1234" is "1234 -way" C++ Programming: Program Design Including Data Structures, Third Edition 41

Problem Analysis • If str denotes a string − Check the first character, str[0],

Problem Analysis • If str denotes a string − Check the first character, str[0], of str − If str[0] is a vowel, add "-way" at the end of str − If the first character of str, str[0], is not a vowel • First add "-" at the end of the string • Remove the first character of str from str and put it at end of str • Now the second character of str becomes the first character of str C++ Programming: Program Design Including Data Structures, Third Edition 42

Problem Analysis (continued) − This process is repeated until either • The first character

Problem Analysis (continued) − This process is repeated until either • The first character of str is a vowel • All characters of str are processed, in which case str does not contain any vowels C++ Programming: Program Design Including Data Structures, Third Edition 43

Algorithm Design • The program contains the following functions: − − − • is.

Algorithm Design • The program contains the following functions: − − − • is. Vowel - to determine whether a character is a vowel rotate - to move first character of str to the end of str pig. Latin. String - to find the pig Latin form of str Steps in the Algorithm: 1. Get str 2. Use the function pig. Latin. String to find the pig Latin form of str 3. Output the pig Latin form of str C++ Programming: Program Design Including Data Structures, Third Edition 44

C++ Programming: Program Design Including Data Structures, Third Edition 45

C++ Programming: Program Design Including Data Structures, Third Edition 45

Function rotate • Takes a string as a parameter • Removes the first character

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, and then adding the first character of the string C++ Programming: Program Design Including Data Structures, Third Edition 46

Function rotate (continued) C++ Programming: Program Design Including Data Structures, Third Edition 47

Function rotate (continued) C++ Programming: Program Design Including Data Structures, Third Edition 47

Function pig. Latin. String • If p. Str[0] is a vowel, add "-way" at

Function pig. Latin. String • If p. Str[0] is a vowel, add "-way" at end of p. Str • If p. Str[0] is not a vowel − Move the first character of p. Str to the end of p. Str − The second character of p. Str becomes the first character of p. Str • Now p. Str may or may not contain a vowel − Use a bool variable, found. Vowel, which is set to true if p. Str contains a vowel, and false otherwise − Initialize found. Vowel to false C++ Programming: Program Design Including Data Structures, Third Edition 48

Function pig. Latin. String (continued) − if p. Str[0] is not a vowel, move

Function pig. Latin. String (continued) − if p. Str[0] is not a vowel, move str[0] to the end of p. Str by calling the function rotate − Repeat third step until either the first character of p. Str becomes a vowel or all characters of p. Str have been checked • Convert p. Str into the pig Latin form • Return p. Str C++ Programming: Program Design Including Data Structures, Third Edition 49

Main Algorithm 1. Get the string 2. Call the function pig. Latin. String to

Main Algorithm 1. Get the string 2. Call the function pig. Latin. String to find the pig Latin form of the string 3. Output the pig Latin form of the string C++ Programming: Program Design Including Data Structures, Third Edition 50

Summary • An enumeration type is a set of ordered values • Reserved word

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: Program Design Including Data Structures, Third Edition 51

Summary (continued) • An anonymous type is one where a variable’s values are specified

Summary (continued) • An anonymous type is one where a variable’s values are specified without any type name • C++’s reserved word typedef creates synonyms or aliases to previously defined data types • The namespace mechanism is a feature of ANSI/ISO Standard C++ • A namespace member is usually a named constant, variable, function, or another namespace C++ Programming: Program Design Including Data Structures, Third Edition 52

Summary (continued) • The keyword namespace must appear in the using statement • A

Summary (continued) • The keyword namespace must appear in the using statement • 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: Program Design Including Data Structures, Third Edition 53

Summary (continued) • The function size returns the number of characters currently in the

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: Program Design Including Data Structures, Third Edition 54