Chapter 7:
User-Defined Simple Data Types,
Namespaces,
and the string Type
Objectives
• In this chapter, you will:
– Create and manipulate your own simple data type
called the enumeration type
– Become familiar with the typedef statement
– Learn about the namespace mechanism
– Explore the string data type and various string
functions to manipulate strings
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 2
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 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
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 3
Enumeration Type (cont’d.)
• You can specify the name and the values, but
not the operations
• Syntax:
– value1, value2, … are identifiers called
enumerators
– List specifies the ordering:
value1 < value2 < value3 <...
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 4
Enumeration Type (cont’d.)
• 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 same block
• Same rules apply to enumeration types
declared outside of any blocks
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 5
Enumeration Type (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 6
Enumeration Type (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 7
Declaring Variables
• Syntax:
• Example:
– Can declare variables such as:
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 8
Assignment
• Values can be stored in enumeration data
types:
popularSport = FOOTBALL;
– Stores FOOTBALL into popularSport
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 9
Operations on Enumeration Types
• No arithmetic operations are allowed on
enumeration types
• ++ and -- are illegal, too:
• Solution: use a static cast
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 10
Relational Operators
• An enumeration type is an ordered set of
values:
• An enumeration type is an integral data type
and can be used in loops:
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 11
Input /Output of Enumeration
Types
• An enumeration type cannot be input/output
(directly)
– Can input and output indirectly
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 12
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
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 13
Declaring Variables When Defining
the Enumeration Type
• Can declare variables of an enumeration type
when you define an enumeration type:
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 14
Anonymous Data Types
• Anonymous type: values are directly specified
in the declaration, with no type name
• Example:
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 15
Anonymous Data Types (cont’d.)
• 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:
• Best practices: to avoid confusion, define an
enumeration type first, then declare variables
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 16
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, 'W', is in position 0
– The second character, 'i', is in position 1
– name is capable of storing any size string
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 17
string Type (continued)
• Binary operator + and the array subscript
operator [], have been defined for the
data type string
– + performs the string concatenation operation
• Example:
str1 = "Sunny";
str2 = str1 + " Day";
stores "Sunny Day" into str2
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 18
Additional string Operations
• length
• size
• find
• substr
• swap
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 19
length Function
• Returns the number of characters currently in
the string
• Syntax:
where strVar is variable of the type string
• length returns an unsigned integer
• The value returned can be stored in an integer variable
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 20
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 21
size Function
• size is the same as the function length
– Both functions return the same value
• Syntax:
where strVar is variable of the type string
• As in the case of the function length, the
function size has no arguments
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 22
find Function
• Searches a string for the first occurrence of a
particular substring
• Returns an unsigned integer value of type
string::size_type
– Or string::npos if unsuccessful
• Syntax:
– strExp can be a string or a character
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 23
find Function (continued)
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 24
substr Function
• Returns a particular substring of a string
• Syntax:
expr1 and expr2 are expressions evaluating to
unsigned integers
– expr1 specifies a position within the string
(starting position of the substring)
– expr2 specifies the length of the substring to be
returned
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 25
substr Function (continued)
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 26
swap Function
• Interchanges contents of two string variables
• Syntax:
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 "War"
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 27
Summary
• 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
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 28
Summary (cont’d.)
• using statement simplifies access to namespace
members
• A string is a sequence of 0 or more characters
• Strings in C++ are enclosed in ""
• First character of a string is in position 0
• In C++, [] is the array subscript operator
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 29