41
1 Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu Learn how to create and manipulate Learn how to create and manipulate enumeration type enumeration type Become aware of the typedef statement Become aware of the typedef statement Learn about the namespace mechanism Learn about the namespace mechanism Explore the string data type Explore the string data type learn how to use the various string learn how to use the various string functions functions Chapter 8: Chapter 8: User-Defined Simple Data Types, String Type, Namespaces

1 Week 1304/07/2005Course ISM3230Dr. Simon Qiu Learn how to create and manipulate enumeration type Become aware of the typedef statement Learn about

Embed Size (px)

Citation preview

1

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

Learn how to create and manipulate enumeration typeLearn how to create and manipulate enumeration type

Become aware of the typedef statementBecome aware of the typedef statement

Learn about the namespace mechanismLearn about the namespace mechanism

Explore the string data typeExplore the string data type

learn how to use the various string functions learn how to use the various string functions

Chapter 8: Chapter 8: User-Defined Simple Data Types, String Type, Namespaces

2

Enumeration TypeEnumeration Type

Data typeData type - a set of values together with a set of operations on - a set of values together with a set of operations on those valuesthose values

To define a new simple data type, called enumeration type, To define a new simple data type, called enumeration type, we need three things:we need three things:

A name for the data typeA name for the data type

A set of values for the data typeA set of values for the data type

A set of operations on the valuesA set of operations on the values

A new simple data type can be defined by specifying its name A new simple data type can be defined by specifying its name and the values, but not the operationsand the values, but not the operations

The values must be identifiersThe values must be identifiers

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

3

Enumeration Type (continued)Enumeration Type (continued) The syntax for enumeration type is:The syntax for enumeration type is:

enum typeName{value1, value2, ...};enum typeName{value1, value2, ...};

//value1, value2, … are identifiers called enumerators//value1, value2, … are identifiers called enumerators//value1 < value2 < value3 <... it is ordered set of values//value1 < value2 < value3 <... it is ordered set of values

If a value has been used in one enumeration typeIf a value has been used in one enumeration type

It cannot be used by another in the same blockIt cannot be used by another in the same block

The same rules apply to enumeration types declared outside of The same rules apply to enumeration types declared outside of any blocksany blocks

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

4

ExamplesExamples

The following are illegal enumeration types because none of The following are illegal enumeration types because none of the values is an identifier:the values is an identifier:

enum grades{'A', 'B', 'C', 'D', 'F'}; enum grades{'A', 'B', 'C', 'D', 'F'};

enum places{1st, 2nd, 3rd, 4th}; enum places{1st, 2nd, 3rd, 4th};

The following are legal enumeration types:The following are legal enumeration types:

enum grades {A, B, C, D, F};enum grades {A, B, C, D, F};

enum places{first, second, third, fourth};enum places{first, second, third, fourth};

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

5

Declaring VariablesDeclaring Variables

The syntax for declaring variables is:The syntax for declaring variables is:

dataType identifier, identifier,...;dataType identifier, identifier,...;

Examples:Examples:

-The following statement defines an enumeration type sports -The following statement defines an enumeration type sports

enum sports {basketball, football, hockey, enum sports {basketball, football, hockey,

baseball, soccer, volleyball}; baseball, soccer, volleyball};

- The following statement declares variables of the type sports.- The following statement declares variables of the type sports.

sports popularSport, mySport;sports popularSport, mySport;

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

6

AssignmentAssignment

The statement:The statement:

popularSport = football;popularSport = football;

//stores the word football into popularSport //stores the word football into popularSport

The statement:The statement:

mySport = popularSport;mySport = popularSport;

//copies the contents of the variable //copies the contents of the variable

//popularSport into mySport//popularSport into mySport

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

7

OperationsOperations

No arithmetic operation is allowed on enumeration types No arithmetic operation is allowed on enumeration types

The following statements are illegal;The following statements are illegal;

mySport = popularSport + 2; //illegalmySport = popularSport + 2; //illegal

popularSport = football + soccer; //illegalpopularSport = football + soccer; //illegal

popularSport = popularSport * 2; // illegal\popularSport = popularSport * 2; // illegal\

popularSport++; //illegalpopularSport++; //illegal

popularSport--; //illegalpopularSport--; //illegal

The increment and decrement operations are not allowed on The increment and decrement operations are not allowed on enumeration typesenumeration types

8

Operations and Input/OutputOperations and Input/Output Because an enumeration is an ordered set of values. We can Because an enumeration is an ordered set of values. We can

use relational operators with them:use relational operators with them:football <= soccer is truefootball <= soccer is truebasketball < football is falsebasketball < football is false

The cast operator can be used to increment, decrement, and The cast operator can be used to increment, decrement, and compare values. Values can be used in loops:compare values. Values can be used in loops:

for (mysport=baseketball; mysport <=soscer; for (mysport=baseketball; mysport <=soscer; mysport=static_cast<soirts>(sport+1))mysport=static_cast<soirts>(sport+1))

Input and output are defined only for built-in data types such Input and output are defined only for built-in data types such as int, char, doubleas int, char, double

The enumeration type can be neither input nor output The enumeration type can be neither input nor output (directly)(directly)

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

9

Functions and Enumeration Functions and Enumeration TypesTypes

Enumeration type can be passed as parameters to functions Enumeration type can be passed as parameters to functions either by value or by referenceeither by value or by reference

A function can return a value of the enumeration typeA function can return a value of the enumeration type

//define the courses as enumeration type//define the courses as enumeration typeenum courses {algebra, computer, philsophy, analysis, chemistry, histry};enum courses {algebra, computer, philsophy, analysis, chemistry, histry};

//function to read enum data input//function to read enum data inputcourses readCourses()courses readCourses(){ { courses registered;courses registered;

. . . . . . . . . . reture registered; }reture registered; }

//output the enum data//output the enum dataVoid printEnum (courses registered) Void printEnum (courses registered) { { switch (registered)switch (registered)

case algebra: case algebra: cout << “algebra”;cout << “algebra”; case computer: case computer: cout << “Computer”;cout << “Computer”;

…… …… }}

10

Anonymous Data TypesAnonymous Data Types

AnonymousAnonymous - a data type in which values are directly specified - a data type in which values are directly specified in the variable declaration with no type namein the variable declaration with no type nameexample:example:

enum {basketball, football, baseball} mysport;enum {basketball, football, baseball} mysport;

Creating an anonymous type has drawbacksCreating an anonymous type has drawbacks

We cannot pass an anonymous type as a parameter to a We cannot pass an anonymous type as a parameter to a functionfunction

A function cannot return a value of an anonymous typeA function cannot return a value of an anonymous type

Values used in one can be used in another, but they are treated Values used in one can be used in another, but they are treated differentlydifferently

11

typedef Statementtypedef Statement

You can create synonyms or aliases to a previously defined You can create synonyms or aliases to a previously defined data type by using the typedef statementdata type by using the typedef statement

The syntax of the typedef statement is:The syntax of the typedef statement is:

typedef existingTypeName newTypeName;typedef existingTypeName newTypeName;

exams:exams: typedef int myBoolean;typedef int myBoolean;

const myBoolean True =1;const myBoolean True =1;

const myBoolean False =0;const myBoolean False =0;

////typedef does not create any new data typestypedef does not create any new data types

//typedef creates an alias to an existing data type//typedef creates an alias to an existing data type

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

12

NamespacesNamespaces

When a header file, such as iostream, is included in a programWhen a header file, such as iostream, is included in a programGlobal identifiers in the header file also become global identifiers in the Global identifiers in the header file also become global identifiers in the

programprogram

If a global identifier in a program has the same name as one of If a global identifier in a program has the same name as one of the global identifiers in the header filethe global identifiers in the header fileThe compiler will generate a syntax error (such as identifier redefined) The compiler will generate a syntax error (such as identifier redefined)

The same problem can occur if a program uses third party The same problem can occur if a program uses third party librarieslibraries

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

13

Namespaces (continued)Namespaces (continued)

To overcome this problem, third party vendors begin their To overcome this problem, third party vendors begin their global identifiers with a special symbol global identifiers with a special symbol

Because compiler vendors begin their global identifier with _ Because compiler vendors begin their global identifier with _ (underscore)(underscore)

To avoid linking errors, do not begin identifiers in your program with _To avoid linking errors, do not begin identifiers in your program with _

ANSI/ISO standard C++ attempts to solve this problem of ANSI/ISO standard C++ attempts to solve this problem of overlapping global identifier names with the namespace overlapping global identifier names with the namespace mechanismmechanism

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

14

Syntax: namespaceSyntax: namespace The syntax of the statement namespace is:The syntax of the statement namespace is:

namespace namespace_namenamespace namespace_name{ members { members } } //where a member is usually a variable declaration//where a member is usually a variable declaration//a named constant, a function, or another namespace//a named constant, a function, or another namespace

namespace globalTypenamespace globalType{ { const int n = 10, double rate = 7.5, int count =0;const int n = 10, double rate = 7.5, int count =0;

void printResult ();void printResult (); }}//you can define the function here://you can define the function here:// void globalType::printResult() { …. }// void globalType::printResult() { …. }

using namespace globalType;using namespace globalType; Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

15

Accessing a namespace Accessing a namespace MemberMember

The scope of a namespace member is local to the namespaceThe scope of a namespace member is local to the namespace

Usually two ways a namespace member can be accessed Usually two ways a namespace member can be accessed outside the namespaceoutside the namespace

One way is to use the syntax:One way is to use the syntax:

namespace_name::identifiernamespace_name::identifier

To access the member rate of the namespace globalType, the To access the member rate of the namespace globalType, the following statement is required:following statement is required:

globalType::rateglobalType::rate

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

16

Accessing a namespace Member Accessing a namespace Member (continued)(continued) To access the function printResult, the following statement is To access the function printResult, the following statement is

required:required:

globalType::printResult();globalType::printResult();

To simplify the accessing of all namespace members:To simplify the accessing of all namespace members:

using namespace namespace_name;using namespace namespace_name;

To simplify the accessing of a specific namespace member:To simplify the accessing of a specific namespace member:

using namespace_name::identifier;using namespace_name::identifier;

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

17

The using StatementThe using Statement

After the using statementAfter the using statement

Not necessary to precede the namespace_name and the scope Not necessary to precede the namespace_name and the scope resolution operator before the namespace memberresolution operator before the namespace member

If a namespace member and a global identifier or a block If a namespace member and a global identifier or a block identifier have the same nameidentifier have the same name

namespace_name and scope resolution operator must precede namespace_name and scope resolution operator must precede the namespace memberthe namespace member

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

18

The string TypeThe string Type

To use the data type string, the program must include the To use the data type string, the program must include the header file <string> by using: header file <string> by using:

#include <string>#include <string>

The declaration statement: The declaration statement:

string name = "William Jacob";string name = "William Jacob";

//declares name to be a string variable and also //declares name to be a string variable and also

//initializes name to "William Jacob"//initializes name to "William Jacob"

The first character, 'W', in name is in position 0, the second The first character, 'W', in name is in position 0, the second character, 'i', is in position 1, and so oncharacter, 'i', is in position 1, and so on

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

19

The string Type (continued)The string Type (continued)

The variable name is capable of storing any size stringThe variable name is capable of storing any size string

Binary operator + (to allow the string concatenation Binary operator + (to allow the string concatenation operation), and the array subscript operator [], have been operation), and the array subscript operator [], have been defined for the data type stringdefined for the data type string

Example: Example: str2 = str1 + " Day";str2 = str1 + " Day";

//// If str1 = "Sunny", the statement stores the string "Sunny Day" into If str1 = "Sunny", the statement stores the string "Sunny Day" into str2str2

//whete str2 [0] = ‘S’ and str2 [6] = ‘D’//whete str2 [0] = ‘S’ and str2 [6] = ‘D’

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

20

length Functionlength Function

Length returns the number of characters currently in the Length returns the number of characters currently in the stringstring

The syntax to call the length function is:The syntax to call the length function is:

strVar.length();strVar.length();

exam: firstName.length();exam: firstName.length();

//where strVar is variable of the type string//where strVar is variable of the type string

//length has no arguments//length has no arguments

//length returns an unsigned integer//length returns an unsigned integer

The value returned can be stored in an integer variableThe value returned can be stored in an integer variable

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

21

size Functionsize Function

The function size is same as the function lengthThe function size is same as the function length

Both functions return the same valueBoth functions return the same value

The syntax to call the function size is:The syntax to call the function size is:

strVar.size()strVar.size()

exam: firstName.size();exam: firstName.size();

//where strVar is variable of the type string//where strVar is variable of the type string

As in the case of the function length, the function size has As in the case of the function length, the function size has no arguments no arguments

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

22

find Functionfind Function

find searches a string for the first occurrence of a find searches a string for the first occurrence of a particular substringparticular substring

Returns the position of an unsigned integer value of Returns the position of an unsigned integer value of type string::size_type giving the result of the searchtype string::size_type giving the result of the search

The syntax to call the function find is:The syntax to call the function find is:strVar.find(strExp)strVar.find(strExp)

//where strVar is a string variable and //where strVar is a string variable and

//strExp is a string expression evaluating to a //strExp is a string expression evaluating to a stringstring

//strExp, can also be a character//strExp, can also be a characterexam: firstName.find (“Jone”);exam: firstName.find (“Jone”);

23

substr Functionsubstr Function substr returns a particular substring of a string substr returns a particular substring of a string

The syntax to call the function substr is:The syntax to call the function substr is:strVar.substr(expr1,expr2)strVar.substr(expr1,expr2)

//where expr1 and expr2 are expressions //where expr1 and expr2 are expressions //expr1 specifies a position //expr1 specifies a position

within the string – within the string – starting starting position of the position of the

substringsubstring//expr2 specifies the length of the substring to be //expr2 specifies the length of the substring to be

returnedreturned

String sentence = “It is cloudy and worm”;String sentence = “It is cloudy and worm”;String str2 = sentence.substring (2,10);String str2 = sentence.substring (2,10);

//where str2 = “ is cloudy”//where str2 = “ is cloudy”Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

24

swap Functionswap Function

swap interchanges the contents of two string variablesswap interchanges the contents of two string variables

The syntax to use the function swap isThe syntax to use the function swap is

strVar1.swap(strVar2);strVar1.swap(strVar2);where strVar1 and strVar2 are string variableswhere strVar1 and strVar2 are string variables

Suppose you have the following statements:Suppose you have the following statements:

string str1 = "Warm";string str1 = "Warm";

string str2 = "Cold";string str2 = "Cold";

After str1.swap(str2); executes, the value of str1 is "Cold" After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "Warm"and the value of str2 is "Warm"

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

25

Programming Example: Pig Latin Programming Example: Pig Latin StringsStrings

Program prompts user to input a stringProgram prompts user to input a string

Then outputs the string in the pig Latin formThen outputs the string in the pig Latin form

The rules for converting a string into pig Latin form are as The rules for converting a string into pig Latin form are as follows:follows:

1.1. If the string begins with a vowel, add the string "-way" If the string begins with a vowel, add the string "-way" at the end of the stringat the end of the string

− For example, the pig Latin form of the string "eye" is For example, the pig Latin form of the string "eye" is "eye-way“"eye-way“

2.2. If the string does not begin with a vowel, first add "-" at the end of If the string does not begin with a vowel, first add "-" at the end of the stringthe string

− Then move the first character of the string to the end of the Then move the first character of the string to the end of the string until the first character of the string becomes a vowelstring until the first character of the string becomes a vowel

26

Pig Latin Strings (continued)Pig Latin Strings (continued)

− Next, add the string "ay" at the end. Next, add the string "ay" at the end.

For example, the pig Latin form of the string "There" is For example, the pig Latin form of the string "There" is "ere-Thay“"ere-Thay“

3.3. Strings such as "by" contain no vowelsStrings such as "by" contain no vowels− In cases like this, the letter y can be considered a vowel 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, 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“U, and Y the pig Latin form of "by" is "y-bay“

4.4. Strings such as "1234" contain no vowels Strings such as "1234" contain no vowels − The pig Latin form of a string that has no vowels in it is The pig Latin form of a string that has no vowels in it is

the string followed by the string "-way“the string followed by the string "-way“

− For example, the pig Latin form of the string "1234" is For example, the pig Latin form of the string "1234" is "1234-way“"1234-way“

27

Problem AnalysisProblem Analysis

If str denotes a stringIf str denotes a string

• Check the first character, str[0], of strCheck the first character, str[0], of str

• If str[0] is a vowel, add "-way" at the end of strIf str[0] is a vowel, add "-way" at the end of str

• If the first character of str, str[0], is not a vowelIf the first character of str, str[0], is not a vowel

– First add "-" at the end of the stringFirst add "-" at the end of the string

– Remove the first character of str from str and put it at end of strRemove 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 strNow the second character of str becomes the first character of str

• This process is repeated until eitherThis process is repeated until either

– The first character of str is a vowel The first character of str is a vowel

– All characters of str are processed, in which case str does not All characters of str are processed, in which case str does not contain any vowelscontain any vowels

28

Algorithm DesignAlgorithm Design The program contains the following functions:The program contains the following functions:

isVowelisVowel - to determine whether a character is a vowel - to determine whether a character is a vowel

rotaterotate - to move first character of str to the end of str - to move first character of str to the end of str

pigLatinStringpigLatinString - to find the pig Latin form of str - to find the pig Latin form of str

Steps in the Algorithm: Steps in the Algorithm:

Get strGet str

Use the function pigLatinString to find the str Use the function pigLatinString to find the str

Output the pig Latin form of strOutput the pig Latin form of str

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

29

Function isVowelFunction isVowel

bool isVowel(char ch)bool isVowel(char ch)

{{ switch(ch)switch(ch)

{{

case 'A': case 'E': case 'A': case 'E':

case 'I': case 'O': case 'I': case 'O':

case 'U': case 'Y':case 'U': case 'Y':

case 'a': case 'e': case 'a': case 'e':

case 'i': case 'o': case 'i': case 'o':

case 'u': case 'y': return true;case 'u': case 'y': return true;

default: return false;default: return false; }}

}}

30

Function rotateFunction rotate

Takes a string as a parameterTakes a string as a parameter

Removes the first character of the stringRemoves the first character of the string

Places it at end of the string by extracting the substring Places it at end of the string by extracting the substring starting at position 1 until the end of the string, then starting at position 1 until the end of the string, then adding the first character of the string adding the first character of the string

string rotate(string pStr)string rotate(string pStr){{

int len = pStr.length();int len = pStr.length();string rStr;string rStr;rStr = pStr.substr(1,len - 1) + pStr[0];rStr = pStr.substr(1,len - 1) + pStr[0];return rStr;return rStr;

}}

31

Function pigLatinStringFunction pigLatinString If pStr[0] is a vowel, add "-way" at end of pStrIf pStr[0] is a vowel, add "-way" at end of pStr

If pStr[0] is not a vowelIf pStr[0] is not a vowel

Move the first character of pStr to the end of pStrMove the first character of pStr to the end of pStr

The second character of pStr becomes the first character of The second character of pStr becomes the first character of pStr. Now pStr may or may not contain a vowelpStr. Now pStr may or may not contain a vowel

Use a Boolean variable, foundVowel, which is set to true if Use a Boolean variable, foundVowel, which is set to true if pStr contains a vowel and false otherwisepStr contains a vowel and false otherwise

initialize foundVowel to falseinitialize foundVowel to false

32

Function pigLatinString Function pigLatinString (continued)(continued)

if pStr[0] is not a vowel, move str[0] to the end of pStr if pStr[0] is not a vowel, move str[0] to the end of pStr by calling the function rotateby calling the function rotate

repeat third step until either the first character of pStr repeat third step until either the first character of pStr becomes a vowel or all characters of pStr have been becomes a vowel or all characters of pStr have been checked checked

Convert pStr into the pig Latin formConvert pStr into the pig Latin form

Return pStrReturn pStr

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

33

Main AlgorithmMain Algorithm

1.1. Get the stringGet the string

2.2. Call the function pigLatinString to find the pig Call the function pigLatinString to find the pig Latin form of the stringLatin form of the string

3.3. Output the pig Latin form of the stringOutput the pig Latin form of the string

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

34

Program CodeProgram Code#include <iostream>#include <iostream>#include <string>#include <string>using namespace std;using namespace std;

bool isVowel(char ch);bool isVowel(char ch);string rotate(string pStr);string rotate(string pStr);string pigLatinString(string pStr);string pigLatinString(string pStr);

int main()int main(){ string str;{ string str; cout << "Enter a string: ";cout << "Enter a string: "; cin >> str;cin >> str; cout << endl;cout << endl;

cout << "The pig Latin form of " << str << " is: "cout << "The pig Latin form of " << str << " is: " << pigLatinString(str) << endl;<< pigLatinString(str) << endl;

return 0; }return 0; }

35

bool isVowel(char ch)bool isVowel(char ch){{ switch (ch)switch (ch) {{ case 'A': case 'E':case 'A': case 'E': case 'I': case 'O':case 'I': case 'O': case 'U': case 'Y':case 'U': case 'Y': case 'a': case 'e':case 'a': case 'e': case 'i': case 'o':case 'i': case 'o': case 'u': case 'y': return true;case 'u': case 'y': return true; default: return false;default: return false; }}}}

Program CodeProgram Code

string rotate(string pStr)string rotate(string pStr){{ string::size_type len = string::size_type len =

pStr.length();pStr.length();

string rStr = string rStr = pStr.substr(1, len - 1) + pStr.substr(1, len - 1) + pStr[0];pStr[0];

return rStr;return rStr;}}

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

36

string pigLatinString(string pStr)string pigLatinString(string pStr){ . . . . . . { . . . . . . if (isVowel(pStr[0])) if (isVowel(pStr[0])) //Step 1//Step 1 pStr = pStr + "-way";pStr = pStr + "-way"; else else //Step 2//Step 2 { pStr = pStr + '-';{ pStr = pStr + '-'; pStr = rotate(pStr); pStr = rotate(pStr); //Step 3//Step 3 len = pStr.length(); len = pStr.length(); //Step 3.a//Step 3.a foundVowel = false; foundVowel = false; //Step 3.b//Step 3.b for (counter = 1; counter < len - 1; counter++) for (counter = 1; counter < len - 1; counter++) //Step 3.d//Step 3.d if (isVowel(pStr[0]))if (isVowel(pStr[0])) { foundVowel = true;{ foundVowel = true; break; }break; } else else //Step 3.c//Step 3.c pStr = rotate(pStr);pStr = rotate(pStr);

if (!foundVowel) //Step 4if (!foundVowel) //Step 4 pStr = pStr.substr(1, len) + "-way";pStr = pStr.substr(1, len) + "-way"; elseelse pStr = pStr + "ay";pStr = pStr + "ay"; } return pStr; } //Step 5} return pStr; } //Step 5

Program CodeProgram Code

37

SummarySummary

An enumeration type is a set of ordered valuesAn enumeration type is a set of ordered values

Reserved word enum creates an enumeration typeReserved word enum creates an enumeration type

No arithmetic operations are allowed on the enumeration typeNo arithmetic operations are allowed on the enumeration type

Relational operators can be used with enum valuesRelational operators can be used with enum values

Enumeration type values cannot be input or output directlyEnumeration type values cannot be input or output directly

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

38

SummarySummary

An anonymous type is one where a variable’s values are An anonymous type is one where a variable’s values are specified without any type namespecified without any type name

C++’s reserved word typedef creates synonyms or aliases to C++’s reserved word typedef creates synonyms or aliases to previously defined data typespreviously defined data types

The namespace mechanism is a feature of ANSI/ISO Standard The namespace mechanism is a feature of ANSI/ISO Standard C++C++

A namespace member is usually a named constant, variable, A namespace member is usually a named constant, variable, function, or another namespacefunction, or another namespace

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

39

SummarySummary

The keyword namespace must appear in the using statementThe keyword namespace must appear in the using statement

A string is a sequence of zero or more charactersA string is a sequence of zero or more characters

Strings in C++ are enclosed in double quotation marksStrings in C++ are enclosed in double quotation marks

In C++, [] is called the array subscript operatorIn C++, [] is called the array subscript operator

The function length returns the number of characters The function length returns the number of characters currently in the stringcurrently in the string

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

40

SummarySummary

The function size returns the number of characters currently The function size returns the number of characters currently in the stringin the string

The function find searches a string to locate the first The function find searches a string to locate the first occurrence of a particular substringoccurrence of a particular substring

The function substr returns a particular substring of a stringThe function substr returns a particular substring of a string

The function swap is used to swap the contents of two string The function swap is used to swap the contents of two string variablesvariables

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu

41

Assignment 10Assignment 10

 1.1. Exercises 8.2Exercises 8.22.2. Exercises 8.4 Exercises 8.4 3.3. Exercises 8.7Exercises 8.74.4. Exercises 8.9Exercises 8.9

5.5. Program 8.4Program 8.4

Week 13 04/07/2005 Course ISM3230 Dr. Simon Qiu