C++ Programming:. Program Design Including

Slides:



Advertisements
Similar presentations
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
1 Week 1304/07/2005Course ISM3230Dr. Simon Qiu  Learn how to create and manipulate enumeration type  Become aware of the typedef statement  Learn about.
True or false A variable of type char can hold the value 301. ( F )
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
1 Simple Data Types: Built-in and Use-Defined. 2 Chapter 10 Topics  Built-In Simple Types  Integral and Floating Point Data Types  Using Combined Assignment.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Chapter 10 Simple Data Types: Built- In and User- Defined Dale/Weems.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
Chapter 10 Simple Data Types: Built-In and User-Defined.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Simple Data Types: Built-In and User-Defined
Dale/Weems/Headington
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
1 Chapter 10 & 11 enum & Structured Types Dale/Weems.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 10 Simple Data Types: Built-In and User-Defined
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Objectives In this chapter, you will:
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Chapter 10 Simple Data Types: Built-In and User-Defined
Basic Elements of C++.
Chapter 2: Basic Elements of C++
C++ Simple Data Types Simple types Integral Floating
Basic Elements of C++ Chapter 2.
Data Types Chapter 8.
Chapter 8: The string Type
Review for Final Exam.
Enumerations CS Fall 1999 Enumerations.
Chapter 4: Control Structures I (Selection)
Review for Final Exam.
Engineering Problem Solving with C++ An Object Based Approach
Objectives In this chapter, you will:
Presentation transcript:

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

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, Second Edition expanded by J. Goetz, 2004

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, Second Edition expanded by J. Goetz, 2004

Enumeration Type (continued) The syntax for enumeration type is: enum typeName{value1, value2, ...}; value1, value2, … are identifiers called enumerators value1 < value2 < value3 <... 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, Second Edition expanded by J. Goetz, 2004

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 enum mathStudents{John, Bill, Lisa}; enum compStudents {Susan, John, william}; John is not allowed in the 2nd enum type The same rules apply to enumeration types declared outside of any blocks C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Enumeration Types C++ allows creation of a new simple type by listing (enumerating) all the ordered values in the domain of the type EXAMPLE Below JAN < FEB < MAR < APR , and so on enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; name of new type list of all possible values of this new type expanded by J. Goetz, 2004

Examples The following are illegal enumeration types because none of the values is an identifier: enum grades{'A', 'B', 'C', 'D', 'F'}; enum places{1st, 2nd, 3rd, 4th}; The following are legal enumeration types: enum grades{A, B, C, D, F}; enum places{first, second, third, fourth}; C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Declaring Variables The syntax for declaring variables is: dataType identifier, identifier,...; The following statement defines an enumeration type sports enum sports{basketball, football, hockey, baseball, soccer, volleyball}; The following statement declares variables of the type sports. sports popularSport, mySport; C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Assignment The statement: popularSport = football; stores the word football into popularSport mySport = popularSport; copies the contents of the variable popularSport into mySport C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Declaring enum Type Variables enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; MonthType thisMonth; // declares 2 variables MonthType lastMonth; // of type MonthType lastMonth = OCT ; // assigns values thisMonth = NOV ; // to these variables . lastMonth = thisMonth ; thisMonth = DEC ; 10 expanded by J. Goetz, 2004

Storage of enum Type Variables stored as 0 stored as 1 stored as 2 stored as 3 etc. enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; stored as 11 expanded by J. Goetz, 2004

Operations No arithmetic operation is allowed on enumeration types The following statements are illegal; mySport = popularSport + 2; //illegal popularSport = football + soccer; //illegal popularSport = popularSport * 2; // illegal The increment and decrement operations are not allowed on enumeration types popularSport++; //illegal popularSport--; //illegal C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

C++ Simple Data Types simple types integral floating char short int long bool enum float double long double unsigned C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

More about enum Type Stream I/O ( using the insertion << and extraction >> operators ) is not defined for enumeration types. Instead, functions can be written for this purpose. 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) Comparison of enum type values is defined using the 6 relational operators ( < , <= , > , >= , == , != ). Enumeration type can be used in a Switch statement for the switch expression and the case labels. An enum type can be the return type of a value-returning function in C++. expanded by J. Goetz, 2004

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 popularSport = static_cast <sports> (popularSport +1); popularSport++; //illegal football <= soccer // is true C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

switch ( thisMonth ) // using enum type switch expression { case JAN : MonthType thisMonth; switch ( thisMonth ) // using enum type switch expression { case JAN : case FEB : case MAR : cout << “Winter quarter” ; break ; case APR : case MAY : case JUN : cout << “Spring quarter” ; case JUL : case AUG : case SEP : cout << “Summer quarter” ; case OCT : case NOV : case DEC : cout << “Fall quarter” ; } 16 expanded by J. Goetz, 2004

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, Second Edition expanded by J. Goetz, 2004

Using enum type Control Variable with for Loop enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; void WriteOutName ( /* in */ MonthType ) ; // prototype . MonthType month ; for (month = JAN ; month <= DEC ; month = static_cast <MonthType> (month + 1 ) ) { // requires use of type cast to increment WriteOutName ( month ) ; // function call to perform output } 18 expanded by J. Goetz, 2004

void WriteOutName ( /* in */ MonthType month ) // Prints out calendar name corresponding to month // Precondition: month is assigned // Postcondition: calendar name for month has been written out { switch ( month ) { case JAN : cout << “ January ” ; break ; case FEB : cout << “ February ” ; break ; case MAR : cout << “ March ” ; break ; case APR : cout << “ April ” ; break ; case MAY : cout << “ May ” ; break ; case JUN : cout << “ June ” ; break ; case JUL : cout << “ July ” ; break ; case AUG : cout << “ August ” ; break ; case SEP : cout << “ September ” ; break ; case OCT : cout << “ October ” ; break ; case NOV : cout << “ November ” ; break ; case DEC : cout << “ December ” ; break ; } 19 expanded by J. Goetz, 2004

Function with enum type Return Value enum SchoolType { PRE_SCHOOL, ELEM_SCHOOL, MIDDLE_SCHOOL, HIGH_SCHOOL, COLLEGE } ; . SchoolType GetSchoolData ( void ) // Obtains information from keyboard to determine school level // Postcondition: Function value == personal school level { SchoolType schoolLevel ; int age ; int lastGrade ; cout << “Enter age : “ ; // prompt for information cin >> age ; 20 expanded by J. Goetz, 2004

schoolLevel = PRE_SCHOOL ; else if ( age < 6 ) schoolLevel = PRE_SCHOOL ; else { cout << “Enter last grade completed in school : “ ; cin >> lastGrade; if ( lastGrade < 5 ) schoolLevel = ELEM_SCHOOL ; else if ( lastGrade < 8 ) schoolLevel = MIDDLE_SCHOOL ; else if ( lastGrade < 12 ) schoolLevel = HIGH_SCHOOL ; schoolLevel = COLLEGE ; } return schoolLevel ; // return enum type value 21 expanded by J. Goetz, 2004

Multifile C++ Programs C++ programs often consist of several different files with extensions such as .h and .cpp related typedef statements, const values, enum type declarations, and similar items are often placed in user-written header files by using the #include preprocessor directive the contents of these header files are inserted into any program file that uses them expanded by J. Goetz, 2004

Inserting Header Files #include <iostream> // iostream #include “school.h” int main ( ) { enum SchoolType { PRE_SCHOOL, . ELEM_SCHOOL, . MIDDLE_SCHOOL, . HIGH_SCHOOL, COLLEGE } ; } expanded by J. Goetz, 2004

Anonymous Data Types Anonymous - a data type in which values are directly specified in the variable declaration with no type name, for example: enum {basketball, football, baseball} mysport; Creating an anonymous type has drawbacks We cannot pass an anonymous type as a parameter to a function use them with care !!! C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Anonymous Data Types 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, Second Edition expanded by J. Goetz, 2004

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 existingTypeName newTypeName; typedef does not create any new data types typedef creates an alias to an existing data type typedef creates an additional name for an already existing data type C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

typedef statement before bool type became part of ISO-ANSI C++, a Boolean type was simulated this way typedef int Boolean; // new data type Boolean const Boolean true = 1 ; const Boolean false = 0 ; . Boolean dataOK ; dataOK = true ; expanded by J. Goetz, 2004

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++ this book focus 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++ C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

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, Second Edition expanded by J. Goetz, 2004

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, Second Edition expanded by J. Goetz, 2004

Syntax: namespace The syntax of the statement namespace is: namespace namespace_name { members } where a member is usually a variable declaration, a named constant, e.g. const double rate = 7.50; a function, e.g. void printResult(); or another namespace namespace globalType // e.g. const double rate = 7.50; void printResult(); C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

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 globalType, the following statement is required: globalType::rate To access the function printResult, the following statement is required: globalType::printResult(); C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Accessing a namespace Member 2. To simplify the accessing of all namespace members: using namespace namespace_name; e.g. using namespace globalType; using namespace std; 3. To simplify the accessing of a specific namespace member: using namespace_name::identifier; e.g. using globalType::rate; C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

The using Statement After the using statement Not necessary to precede the namespace_name :: before the namespace member e.g. p.399/4 either x or expN::x If a namespace member and a global identifier or a block identifier have the same name namespace_name :: must precede the namespace member e.g. p.398/2 expN::x C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

The string Type p.1475 To use the data type string, the program must include the header file <string> p.1475 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: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

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 If str1 = "Sunny", the statement stores the string "Sunny Day" into str2: str2 = str1 + " Day"; C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

length Function Length returns the number of characters currently in the string The syntax to call the length function is: strVar.length() where strVar is variable of the type string no arguments length returns an unsigned integer The value returned can be stored in an integer variable C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

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: strVar.size() where strVar is variable of the type string has no arguments C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

find Function find searches a string for the first occurrence of a particular substring Returns an unsigned integer value of type string::size_type (an unsigned integral (data) type) giving the result of the search The syntax to call the function find is: strVar.find(strExp) where strVar is a string variable and strExp is a string expression evaluating to a string The string expression, strExp, can also be a character C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

find Function (continued) If successful, find returns the position in strVar 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”) – max value of data type string C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

substr Function substr returns a particular substring of a string The syntax to call the function substr is: strVar.substr(expr1,expr2) where expr1 and expr2 are expressions evaluating to unsigned integers The expression expr1 specifies a position within the string (starting position of the substring) The expression expr2 specifies the length of the substring to be returned C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

swap Function swap interchanges the contents of two string variables The syntax to use the function swap is strVar1.swap(strVar2); where strVar1 and strVar2 are string variables Suppose you have the following statements: string str1 = "Warm"; string str2 = "Cold"; After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "Warm" C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Programming Example: Pig Latin Strings p.410 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: 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, Second Edition expanded by J. Goetz, 2004

Pig Latin Strings (continued) If the string does not begin with a vowel, first add "-" at the end of the string Then move the 1st 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“ 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“ C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Pig Latin Strings (continued) 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, Second Edition expanded by J. Goetz, 2004

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 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, Second Edition expanded by J. Goetz, 2004

Algorithm Design The program contains the following functions: isVowel - to determine whether a character is a vowel rotate - to move first character of str to the end of str pigLatinString - to find the pig Latin form of str Main Algorithm: Get str Use the function pigLatinString to find the pig Latin form of str Output the pig Latin form of str C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Function isVowel bool isVowel(char ch) { switch(ch) case 'A': case 'E': case 'I': case 'O': case 'U': case 'Y': case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': return true; default: return false; } C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

Function rotate (continued) string rotate(string pStr) // Takes a string as a parameter { int len = pStr.length(); string rStr; rStr = pStr.substr(1,len - 1) + pStr[0]; // Removes 1st // chr and places it at end return rStr; } C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

// Pig Latin Strings p.410 string rotate(string pStr) { #include <iostream> #include <string> using namespace std; bool isVowel(char ch); string rotate(string pStr); string pigLatinString(string pStr); int main() { string str; cout << "Enter a string: "; cin >> str; cout << endl; cout << "The pig Latin form of " << str << " is: " << pigLatinString(str) << endl; return 0; } bool isVowel(char ch) switch (ch) case 'A': case 'E': case 'I': case 'O': case 'U': case 'Y': case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': return true; default: return false; string rotate(string pStr) { string::size_type len = pStr.length(); string rStr; rStr = pStr.substr(1, len - 1) + pStr[0]; return rStr; } string pigLatinString(string pStr) // see next slide string::size_type len; bool foundVowel; string::size_type counter; if (isVowel(pStr[0])) //Step 1 pStr = pStr + "-way"; else //Step 2 pStr = pStr + '-'; pStr = rotate(pStr); //Step 3 len = pStr.length(); //Step 3.a foundVowel = false; //Step 3.b for (counter = 1; counter < len - 1; counter++)//Step 3.d if (isVowel(pStr[0])) foundVowel = true; break; else //Step 3.c pStr = rotate(pStr); if (!foundVowel) //Step 4 pStr = pStr.substr(1, len) + "-way"; else pStr = pStr + "ay"; return pStr; //Step 5 C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

string pigLatinString(string pStr) { string::size_type len; bool foundVowel; string::size_type counter; if (isVowel(pStr[0])) //Step 1 pStr = pStr + "-way"; else //Step 2 pStr = pStr + '-'; pStr = rotate(pStr); //Step 3 len = pStr.length(); //Step 3.a foundVowel = false; //Step 3.b for (counter = 1; counter < len - 1; counter++) if (isVowel(pStr[0])) foundVowel = true; break; } else //Step 3.c pStr = rotate(pStr); if (!foundVowel) //Step 4 pStr = pStr.substr(1, len) + "-way"; else pStr = pStr + "ay"; return pStr; //Step 5 1. If pStr[0] is a vowel, add "-way" at end of pStr 2. If pStr[0] is not a vowel 3a. Move the first character of pStr to the end of pStr The second character of pStr becomes the first character of pStr Now pStr may or may not contain a vowel 3b. Use a Boolean variable, foundVowel, which is set to true if pStr contains a vowel and false otherwise initialize foundVowel to false 3c. if pStr[0] is not a vowel, move str[0] to the end of pStr by calling the function rotate repeat above step until either the first character of pStr becomes a vowel or all characters of pStr have been checked 4. Convert pStr into the pig Latin form 5. Return pStr C++ Programming: Program Design Including Data Structures, Second Edition expanded by J. Goetz, 2004

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, Second Edition expanded by J. Goetz, 2004

Summary 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, Second Edition expanded by J. Goetz, 2004

Summary 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, Second Edition expanded by J. Goetz, 2004

Summary 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, Second Edition expanded by J. Goetz, 2004