0% found this document useful (0 votes)
29 views26 pages

Lecture 4 SE

This chapter discusses user-defined simple data types like enumerations, namespaces, and strings in C++. It covers creating and manipulating enumeration types, using the typedef statement, and the namespace mechanism. It also explores string functions like length(), size(), find(), substr(), and swap() for manipulating strings.

Uploaded by

Zarak Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views26 pages

Lecture 4 SE

This chapter discusses user-defined simple data types like enumerations, namespaces, and strings in C++. It covers creating and manipulating enumeration types, using the typedef statement, and the namespace mechanism. It also explores string functions like length(), size(), find(), substr(), and swap() for manipulating strings.

Uploaded by

Zarak Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

C++ Programming:

From Problem Analysis


to Program Design, Fourth 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 familiar with 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: From Problem Analysis to Program Design, Fourth Edition 2


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: From Problem Analysis to Program Design, Fourth Edition 3


Enumeration Type (continued)

• A new simple data type can be defined by


specifying its name and the values, but not
the operations
− The values must be identifiers
• Syntax:

− value1, value2, … are identifiers called


enumerators
− value1 < value2 < value3 <...

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4


Enumeration Type (continued)

• Enumeration type is an ordered set of values


• If a value has been used in one enumeration
type it can’t be used by another in same block
• The same rules apply to enumeration types
declared outside of any blocks

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5


Enumeration Type (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 6


Declaring Variables

• Syntax:

• For example, given the following definition:

we can declare the following variables:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8


Assignment

• The statement:
popularSport = FOOTBALL;
stores FOOTBALL into popularSport
• The statement:
mySport = popularSport;
copies the value of the popularSport into
mySport

C++ Programming: From Problem Analysis to Program Design, Fourth 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, Fourth Edition 10


Relational Operators

• An enumeration type is an ordered set of


values:

• Enumeration type is an integer data type and


can be used in loops:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11


Input /Output of Enumeration
Types
• I/O are defined only for built-in data types
− Enumeration type cannot be input/output
(directly)

C++ Programming: From Problem Analysis to Program Design, Fourth 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, Fourth Edition 13


Declaring Variables When
Defining the Enumeration Type
• You can declare variables of an enumeration
type when you define an enumeration type:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 14


Anonymous Data Types
• Anonymous type : values are directly
specified in the declaration, with no type name

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 15


typedef Statement
• You can create synonyms or aliases to a data
type using the typedef statement
• Syntax:

• typedef does not create any new data types


− Creates an alias to an existing data type

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 16


Namespaces

• ANSI/ISO standard C++ was officially


approved in July 1998
• Most of the recent compilers are also
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++

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17


Namespaces (continued)
• Global identifiers in a header file used in a
program become global in the program
− Syntax error occurs if an identifier in a
program has the same name as a global
identifier in the header file
• 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 _

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 18


Namespaces (continued)

• ANSI/ISO Standard C++ attempts to solve


this problem with the namespace mechanism
• Syntax:

where a member is usually a variable


declaration, a named constant, a function, or
another namespace

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 19


Namespaces (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 20


Namespaces (continued)
• The scope of a namespace member is local
to the namespace
• Ways a namespace member can be
accessed outside the namespace:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 21


Accessing a namespace Member
• Examples:
globalType::RATE
globalType::printResult();
• After the using statement, it is not necessary
to precede the namespace_name:: before
the namespace member
− Unless a namespace member and a global
identifier or a block identifier have same name

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 22


Summary
• Enumeration type: set of ordered values
− Created with 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: From Problem Analysis to Program Design, Fourth Edition 23


Summary (continued)
• 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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 24


Summary (continued)
• Keyword namespace must appear in the
using statement
• A string is a sequence of zero or more
characters
• Strings in C++ are enclosed in ""
• In C++, [] is the array subscript operator
• The function length returns the number of
characters currently in the string

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 25


Summary (continued)

• 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: From Problem Analysis to Program Design, Fourth Edition 26

You might also like