Chapter 7 Programming I
Chapter 7 Programming I
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 2
product or service or otherwise on a password-protected website for classroom use.
Objectives (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 3
product or service or otherwise on a password-protected website for classroom use.
Enumeration Type (1 of 5)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 4
product or service or otherwise on a password-protected website for classroom use.
Enumeration Type (2 of 5)
• You can specify the name and the values, but not the operations
• The syntax for enumeration type is:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 5
product or service or otherwise on a password-protected website for classroom use.
Enumeration Type (3 of 5)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 6
product or service or otherwise on a password-protected website for classroom use.
Enumeration Type (4 of 5)
EXAMPLE 7-1
The statement:
enum colors {BROWN, BLUE, RED, GREEN, YELLOW};
defines a new data type called colors, and the values belonging to this data type are
BROWN, BLUE, RED, GREEN, and YELLOW.
EXAMPLE 7-2
The statement:
enum standing {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
defines standing to be an enumeration type. The values belonging to standing are
FRESHMAN, SOPHOMORE, JUNIOR, and SENIOR.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 7
product or service or otherwise on a password-protected website for classroom use.
Enumeration Type (5 of 5)
EXAMPLE 7-3
Consider the following statements:
These are illegal enumeration types because none of the values is an identifier. The following, however, are legal
enumeration types:
enum grades {A, B, C, D, F};
enum places {FIRST, SECOND, THIRD, FOURTH};
EXAMPLE 7-4
Consider the following statements:
Suppose that these statements are in the same program in the same block. The second enumeration type,
compStudent, is not allowed because the value JOHN was used in the previous enumeration type
mathStudent.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 8
product or service or otherwise on a password-protected website for classroom use.
Declaring Variables
• Syntax
• Example
enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER,
VOLLEYBALL};
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 9
product or service or otherwise on a password-protected website for classroom use.
Assignment
popularSport = FOOTBALL;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 10
product or service or otherwise on a password-protected website for classroom use.
Operations on Enumeration Types
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 11
product or service or otherwise on a password-protected website for classroom use.
Relational Operators
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 12
product or service or otherwise on a password-protected website for classroom use.
Input /Output of Enumeration Types
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 13
product or service or otherwise on a password-protected website for classroom use.
Functions and Enumeration Types
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 14
product or service or otherwise on a password-protected website for classroom use.
Declaring Variables When Defining the Enumeration Type
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 15
product or service or otherwise on a password-protected website for classroom use.
Anonymous Data Types (1 of 2)
• Anonymous type values are directly specified in the declaration, with no type
name
• Example:
enum {BASKETBALL, FOOTBALL, BASEBALL, HOCKEY} mySport;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 16
product or service or otherwise on a password-protected website for classroom use.
Anonymous Data Types (2 of 2)
• Drawbacks:
• Cannot pass/return an anonymous type to/from a function
• Values used in one type can be used in another, but are treated differently:
enum {ENGLISH, FRENCH, SPANISH, GERMAN, RUSSIAN} languages;
enum {ENGLISH, FRENCH, SPANISH, GERMAN, RUSSIAN} foreignLanguages;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 17
product or service or otherwise on a password-protected website for classroom use.
typedef Statement (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 18
product or service or otherwise on a password-protected website for classroom use.
typedef Statement (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 19
product or service or otherwise on a password-protected website for classroom use.
Namespaces (1 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 20
product or service or otherwise on a password-protected website for classroom use.
Namespaces (2 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 21
product or service or otherwise on a password-protected website for classroom use.
Namespaces (3 of 6)
• ANSI/ISO Standard C++ attempts to solve this problem with the namespace
mechanism
• The general syntax of the statement namespace is:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 22
product or service or otherwise on a password-protected website for classroom use.
Namespaces (4 of 6)
EXAMPLE 7-8
The statement:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 23
product or service or otherwise on a password-protected website for classroom use.
Namespaces (5 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 24
product or service or otherwise on a password-protected website for classroom use.
Namespaces (6 of 6)
• Unless a namespace member and a global identifier or a block identifier have the
same name
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 25
product or service or otherwise on a password-protected website for classroom use.
string Type
• To use data type string, a program must include the header file string
• A string is a sequence of zero or more characters
• The first character is in position 0
• The second character is in position 1, etc.
• Binary operator + performs the string concatenation operation
• Array subscript operator [] allows access to an individual character in a string
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 26
product or service or otherwise on a password-protected website for classroom use.
Additional string Operations
• The data type string has a data type, string::size_type, and a named
constant, string::npos, defined as follows:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 27
product or service or otherwise on a password-protected website for classroom use.
Example 7-18: swap Function
EXAMPLE 7-18
The swap function is used to swap—that is, interchange—the contents of two string
variables.
Suppose you have the following statements:
string str1 = "Warm";
string str2 = "Cold";
After the following statement executes, the value of str1 is "Cold" and the value of
str2 is "Warm".
str1.swap(str2);
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 28
product or service or otherwise on a password-protected website for classroom use.
Quick Review (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 29
product or service or otherwise on a password-protected website for classroom use.
Quick Review (2 of 3)
• Anonymous type: a variable’s values are specified without any type name
• 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
• Scope of a namespace member is local to namespace
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 30
product or service or otherwise on a password-protected website for classroom use.
Quick Review (3 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 31
product or service or otherwise on a password-protected website for classroom use.