SlideShare a Scribd company logo
C++ Programming:
From Problem Analysis
to Program Design, Fourth Edition
Chapter 8: User-Defined Simple Data
Types, Namespaces, and the
string Type
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2
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 3
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 4
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 5
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 6
Enumeration Type (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8
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 9
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 10
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 11
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 12
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 13
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 14
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 15
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 16
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 17
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 18
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 19
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 20
Namespaces (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 21
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 22
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 23
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, Fourth Edition 24
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, Fourth Edition 25
Additional string Operations
• length
• size
• find
• substr
• swap
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 26
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, Fourth Edition 28
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, Fourth Edition 29
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, Fourth Edition 30
find Function (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 31
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, Fourth Edition 32
substr Function (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 33
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, Fourth Edition 34
Programming Example: Pig Latin
Strings
• Program prompts user to input a string
− Then outputs the string in the pig Latin form
• The rules for converting a string into pig Latin
form are as follows:
− If the string begins with a vowel, add the string
"-way" at the end of the string
• Example: the pig Latin form of "eye" is "eye-
way"
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 35
Programming Example: Pig Latin
Strings (continued)
• Rules (continued):
− If the string does not begin with a vowel, first
add "-" at the end of the string
• Then move the first character of the string to the
end of the string until the first character of the
string becomes a vowel
• Next, add the string "ay" at the end
• Example: pig Latin form of "There" is "ere-
Thay"
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 36
Programming Example: Pig Latin
Strings (continued)
• Rules (continued):
− Strings such as "by" contain no vowels
• The letter 'y' can be considered a vowel
• For this program the vowels are a, e, i, o, u, y, A,
E, I, O, U, and Y
− Strings such as "1234" contain no vowels
• The pig Latin form of a string that has no vowels
in it is the string followed by the string "-way"
• Example: pig Latin form of "1234" is "1234-
way"
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 37
Programming Example: Problem
Analysis
• If str denotes a string:
− Check the first character, str[0], of str
− If it is a vowel, add "-way" at the end of str
− If it is not a vowel:
• First add "-" at the end of the string
• Remove 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 str
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 38
Programming Example: Problem
Analysis (continued)
• If str denotes a string (continued):
− This process is repeated until either
• The first character of str is a vowel
• All characters of str are processed, in which
case str does not contain any vowels
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 39
Programming Example: Algorithm
Design
• The program contains the following functions:
− isVowel determines if a character is a vowel
− rotate moves first character of str to the
end of str
− pigLatinString finds pig Latin form of str
• Steps in the algorithm:
− Get str
− Use pigLatinString to find the pig Latin
form of str
− Output the pig Latin form of str
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 40
Programming Example: Function
isVowel
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 41
Programming Example: Function
rotate
• Takes a string as a parameter
• Removes the first character of the string
− Places it at end of the string by extracting the
substring starting at position 1 until the end of
the string, then adding the first character of the
string
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 42
Programming Example: Function
pigLatinString
• If pStr[0] is a vowel, add "-way" at end
• If pStr[0] is not a vowel:
− Move first character of pStr to the end of pStr
− The second character of pStr becomes the first
character of pStr
• Now pStr may or may not contain a vowel
− Use a bool variable, foundVowel, which is set to
true if pStr contains a vowel and false
otherwise
− Initialize foundVowel to false
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 43
Programming Example: Function
pigLatinString (continued)
− If pStr[0] is not a vowel, move str[0] to
the end of pStr by calling the function
rotate
− Repeat third step until either the first character
of pStr becomes a vowel or all characters of
pStr have been checked
• Convert pStr into the pig Latin form
• Return pStr
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 44
Programming Example: Main
Algorithm
• Get the string
• Call pigLatinString to find the pig Latin
form of the string
• Output the pig Latin form of the string
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 45
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 46
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 47
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 48
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

More Related Content

Similar to 9781423902096_PPT_ch08.ppt

Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
ahmed22dg
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
Terry Yoast
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
ahmed22dg
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
Terry Yoast
 
C material
C materialC material
C material
tarique472
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
Terry Yoast
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
Terry Yoast
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 
C++ ch2
C++ ch2C++ ch2
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
FatimaZafar68
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02
Terry Yoast
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
Adan Hubahib
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
Terry Yoast
 
enum_namespace.ppt
enum_namespace.pptenum_namespace.ppt
enum_namespace.ppt
AshishNayyar11
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in C
babuk110
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
Vigneshkumar Ponnusamy
 
9781285852744 ppt ch18
9781285852744 ppt ch189781285852744 ppt ch18
9781285852744 ppt ch18
Terry Yoast
 

Similar to 9781423902096_PPT_ch08.ppt (20)

Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
 
C material
C materialC material
C material
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
C++ ch2
C++ ch2C++ ch2
C++ ch2
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
 
enum_namespace.ppt
enum_namespace.pptenum_namespace.ppt
enum_namespace.ppt
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in C
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
 
9781285852744 ppt ch18
9781285852744 ppt ch189781285852744 ppt ch18
9781285852744 ppt ch18
 

More from LokeshK66

Iot application in smart cities .pptx
Iot    application in  smart  cities .pptxIot    application in  smart  cities .pptx
Iot application in smart cities .pptx
LokeshK66
 
building mat.pptx
building mat.pptxbuilding mat.pptx
building mat.pptx
LokeshK66
 
ch4_additional.ppt
ch4_additional.pptch4_additional.ppt
ch4_additional.ppt
LokeshK66
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
LokeshK66
 
ch6_additional.ppt
ch6_additional.pptch6_additional.ppt
ch6_additional.ppt
LokeshK66
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
LokeshK66
 
SQLSecurity.ppt
SQLSecurity.pptSQLSecurity.ppt
SQLSecurity.ppt
LokeshK66
 

More from LokeshK66 (7)

Iot application in smart cities .pptx
Iot    application in  smart  cities .pptxIot    application in  smart  cities .pptx
Iot application in smart cities .pptx
 
building mat.pptx
building mat.pptxbuilding mat.pptx
building mat.pptx
 
ch4_additional.ppt
ch4_additional.pptch4_additional.ppt
ch4_additional.ppt
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 
ch6_additional.ppt
ch6_additional.pptch6_additional.ppt
ch6_additional.ppt
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
 
SQLSecurity.ppt
SQLSecurity.pptSQLSecurity.ppt
SQLSecurity.ppt
 

Recently uploaded

CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
Nguyen Thanh Tu Collection
 
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxA Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
OH TEIK BIN
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Kalna College
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
 
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Celine George
 
220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology
Kalna College
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
MJDuyan
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
khabri85
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
heathfieldcps1
 
Creative Restart 2024: Mike Martin - Finding a way around “no”
Creative Restart 2024: Mike Martin - Finding a way around “no”Creative Restart 2024: Mike Martin - Finding a way around “no”
Creative Restart 2024: Mike Martin - Finding a way around “no”
Taste
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
 

Recently uploaded (20)

CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
 
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxA Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
 
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17
 
220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
 
Creative Restart 2024: Mike Martin - Finding a way around “no”
Creative Restart 2024: Mike Martin - Finding a way around “no”Creative Restart 2024: Mike Martin - Finding a way around “no”
Creative Restart 2024: Mike Martin - Finding a way around “no”
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
 

9781423902096_PPT_ch08.ppt

  • 1. C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type
  • 2. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2 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
  • 3. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 3 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
  • 4. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4 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 <...
  • 5. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5 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
  • 6. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 6 Enumeration Type (continued)
  • 7.
  • 8. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8 Declaring Variables • Syntax: • For example, given the following definition: we can declare the following variables:
  • 9. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9 Assignment • The statement: popularSport = FOOTBALL; stores FOOTBALL into popularSport • The statement: mySport = popularSport; copies the value of the popularSport into mySport
  • 10. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 10 Operations on Enumeration Types • No arithmetic operations are allowed on enumeration types • ++ and -- are illegal too: • Solution: use a static cast:
  • 11. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11 Relational Operators • An enumeration type is an ordered set of values: • Enumeration type is an integer data type and can be used in loops:
  • 12. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 12 Input /Output of Enumeration Types • I/O are defined only for built-in data types − Enumeration type cannot be input/output (directly)
  • 13. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 13 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
  • 14. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 14 Declaring Variables When Defining the Enumeration Type • You can declare variables of an enumeration type when you define an enumeration type:
  • 15. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 15 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:
  • 16. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 16 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
  • 17. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17 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++
  • 18. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 18 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 _
  • 19. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 19 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
  • 20. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 20 Namespaces (continued)
  • 21. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 21 Namespaces (continued) • The scope of a namespace member is local to the namespace • Ways a namespace member can be accessed outside the namespace:
  • 22. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 22 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
  • 23. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 23 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
  • 24. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 24 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
  • 25. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 25 Additional string Operations • length • size • find • substr • swap
  • 26. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 26 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
  • 27.
  • 28. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 28 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
  • 29. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 29 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
  • 30. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 30 find Function (continued)
  • 31. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 31 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
  • 32. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 32 substr Function (continued)
  • 33. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 33 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"
  • 34. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 34 Programming Example: Pig Latin Strings • Program prompts user to input a string − Then outputs the string in the pig Latin form • The rules for converting a string into pig Latin form are as follows: − If the string begins with a vowel, add the string "-way" at the end of the string • Example: the pig Latin form of "eye" is "eye- way"
  • 35. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 35 Programming Example: Pig Latin Strings (continued) • Rules (continued): − If the string does not begin with a vowel, first add "-" at the end of the string • Then move the first character of the string to the end of the string until the first character of the string becomes a vowel • Next, add the string "ay" at the end • Example: pig Latin form of "There" is "ere- Thay"
  • 36. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 36 Programming Example: Pig Latin Strings (continued) • Rules (continued): − Strings such as "by" contain no vowels • The letter 'y' can be considered a vowel • For this program the vowels are a, e, i, o, u, y, A, E, I, O, U, and Y − Strings such as "1234" contain no vowels • The pig Latin form of a string that has no vowels in it is the string followed by the string "-way" • Example: pig Latin form of "1234" is "1234- way"
  • 37. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 37 Programming Example: Problem Analysis • If str denotes a string: − Check the first character, str[0], of str − If it is a vowel, add "-way" at the end of str − If it is not a vowel: • First add "-" at the end of the string • Remove 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 str
  • 38. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 38 Programming Example: Problem Analysis (continued) • If str denotes a string (continued): − This process is repeated until either • The first character of str is a vowel • All characters of str are processed, in which case str does not contain any vowels
  • 39. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 39 Programming Example: Algorithm Design • The program contains the following functions: − isVowel determines if a character is a vowel − rotate moves first character of str to the end of str − pigLatinString finds pig Latin form of str • Steps in the algorithm: − Get str − Use pigLatinString to find the pig Latin form of str − Output the pig Latin form of str
  • 40. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 40 Programming Example: Function isVowel
  • 41. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 41 Programming Example: Function rotate • Takes a string as a parameter • Removes the first character of the string − Places it at end of the string by extracting the substring starting at position 1 until the end of the string, then adding the first character of the string
  • 42. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 42 Programming Example: Function pigLatinString • If pStr[0] is a vowel, add "-way" at end • If pStr[0] is not a vowel: − Move first character of pStr to the end of pStr − The second character of pStr becomes the first character of pStr • Now pStr may or may not contain a vowel − Use a bool variable, foundVowel, which is set to true if pStr contains a vowel and false otherwise − Initialize foundVowel to false
  • 43. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 43 Programming Example: Function pigLatinString (continued) − If pStr[0] is not a vowel, move str[0] to the end of pStr by calling the function rotate − Repeat third step until either the first character of pStr becomes a vowel or all characters of pStr have been checked • Convert pStr into the pig Latin form • Return pStr
  • 44. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 44 Programming Example: Main Algorithm • Get the string • Call pigLatinString to find the pig Latin form of the string • Output the pig Latin form of the string
  • 45. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 45 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
  • 46. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 46 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
  • 47. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 47 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
  • 48. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 48 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