0% found this document useful (0 votes)
59 views

Chapter 7 Programming I

The chapter discusses user-defined data types in C++, including enumeration types and strings. It covers how to create and use enumeration types by defining a set of named values. Operations like assignment, comparison, input/output are demonstrated. Anonymous types are also discussed. The chapter also explains namespaces which avoid naming collisions, and strings which can be manipulated with functions.

Uploaded by

Suzan Anwar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Chapter 7 Programming I

The chapter discusses user-defined data types in C++, including enumeration types and strings. It covers how to create and use enumeration types by defining a set of named values. Operations like assignment, comparison, input/output are demonstrated. Anonymous types are also discussed. The chapter also explains namespaces which avoid naming collisions, and strings which can be manipulated with functions.

Uploaded by

Suzan Anwar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter 7

User-Defined Simple Data Types, Namespaces, and the string Type

C++ Programming: Program Design Including Data Structures, Eighth Edition


© 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
product or service or otherwise on a password-protected website for classroom 1
Objectives (1 of 2)

• In this chapter, you will:


• Learn how to create and manipulate your own simple data type—called the
enumeration type
• Explore how the assignment statement, and arithmetic and relational operators work
with enum types
• Learn how to use for loops with enum types
• Learn how to input data into an enum type
• Learn how to output data stored in an enum 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 2
product or service or otherwise on a password-protected website for classroom use.
Objectives (2 of 2)

• Explore how to write functions to process enum types


• Learn how to declare variables when defining the enumeration type
• Become familiar with anonymous types
• Become familiar with the typedef statement
• Learn about the namespace mechanism
• Explore the string data type, and learn how to use string functions to
manipulate strings

© 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)

• A data type is a set of values with a set of operations on them


• Enumeration type is a simple data type created by the programmer
• To define an enumeration type, you need:
• A name for the data type
• A set of values for the data type
• A set of operations on the values

© 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:

• value1, value2, … are identifiers called enumerators


• List specifies the ordering:
value1 < value2 < value3 <...

© 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)

• The enumeration type is an ordered set of values


• Default value assigned to enumerators starts at 0
• A value used in one enumeration type cannot be used by another in the same
block
• Same rules apply to enumeration types declared outside of any blocks

© 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};

•Can declare variables such as:

sports popularSport, 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 9
product or service or otherwise on a password-protected website for classroom use.
Assignment

• Values can be stored in enumeration data types:

popularSport = FOOTBALL;

• Stores FOOTBALL into popularSport

© 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

• No arithmetic operations are allowed on enumeration types

• ++ and -- are illegal, too

• The solution is applying the cast operator

© 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

• An enumeration type is an ordered set of values:


FOOTBALL <= SOCCER is true
HOCKEY > BASKETBALL is true
BASEBALL < FOOTBALL is false

• An enumeration type is an integral data type and can be used in loops:

© 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

• An enumeration type cannot be input/output (directly)


• Can input and output indirectly – refer to code segments below:

© 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

• Enumeration types can be passed as parameters to functions either by value or


by reference
• A function can return a value of the enumeration type

© 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

• Can declare variables of an enumeration type when you define an enumeration


type:

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

© 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;

• This statement is illegal:

• Best practices: to avoid confusion, define an enumeration type first, then


declare variables

© 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)

• The typedef statement is used to create synonyms or aliases to a data type


• The syntax of the typedef statement is:

• typedef does not create any new data types


• Only creates an alias to an existing data 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 18
product or service or otherwise on a password-protected website for classroom use.
typedef Statement (2 of 2)

• The typedef statement is used to create synonyms or aliases to a data type


• The syntax of the typedef statement is:

• typedef does not create any new data types


• Only creates an alias to an existing data 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 19
product or service or otherwise on a password-protected website for classroom use.
Namespaces (1 of 6)

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


• Most recent compilers are compatible with ANSI/ISO standard C++
• For the most part, standard C++ and ANSI/ISO standard C++ are the same
• However, ANSI/ISO Standard C++ has some features not available in Standard C++

© 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)

• Global identifiers in a header file used in a program become global in the


program
• A syntax error occurs if a program’s identifier has the same name as a global identifier
in the header file
• The same problem can occur with third-party libraries
• Common solution: third-party vendors begin their global identifiers with _
(underscore)
• Do not begin identifiers in your program with _

© 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:

where members consist of variable declarations, named constants, functions, or


another 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 22
product or service or otherwise on a password-protected website for classroom use.
Namespaces (4 of 6)

EXAMPLE 7-8
The statement:

defines globalType to be a namespace with four members: named


constants N and RATE, the variable count, and the function printResult.

© 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)

• A namespace member has scope local to the namespace


• A namespace member can be accessed outside the namespace
• The general syntax for accessing a namespace member is:

• ANSI/ISO Standard C++ provides the use of the statement using

© 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)

• Examples with namespaces

• After the using statement, it is not necessary to put the

• 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:

string::size_type An unsigned integer (data) type


string::npos The maximum value of the (data) type
string::size_type, a number such as
4294967295 on many machines

© 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)

• Enumeration type: 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
• Enumeration types can be passed as parameters to functions by value or by
reference

© 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)

• using statement simplifies access to namespace members


• A string is a sequence of zero or more characters
• Strings in C++ are enclosed in ""
• The first character of a string is in position 0
• In C++, [] is the array subscript operator

© 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.

You might also like