Programming Languages Chapter 7 Programming From Problem Analysis Program Design Eighth Edition Userdefined Simple Data

subject Type Homework Help
subject Pages 8
subject Words 2196
subject Authors D. S. Malik

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 7-1
Chapter 7
User-Defined Simple Data Types, Namespaces, and the
string Type
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings, you will find lecture notes that summarize the section, Teacher
At a Glance
Instructor’s Manual Table of Contents
Overview
Objectives
Teaching Tips
Quick Quizzes
Class Discussion Topics
Additional Projects
Additional Resources
Key Terms
page-pf2
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 7-2
Lecture Notes
Overview
Chapter 7 introduces students to the C++ enum data type and the typedef statement.
Students will also explore the C++ namespace mechanism and its purpose. Finally,
students will learn how to manipulate strings with various C++ string functions.
Objectives
In this chapter, the student will:
Learn how to create your own simple data typecalled 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 variable
Learn how to output data stored in an enum variable
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
Teaching Tips
Enumeration Type
1. Explain how the C++ enum type gives users the ability to create their own simple data
types.
2. Describe how an enum type is defined with a type name and potential values for that
type.
Teaching
Tip
Discuss the limitations of the enum type in terms of its operations. You might
take this opportunity to briefly talk about object-oriented programming and
explain how classes overcome this limitation.
3. Discuss the syntax of a C++ enumeration type. Note that the values of the type have a
default ordering based on their position in the value list.
page-pf3
page-pf4
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 7-4
Input/Output of Enumeration Types
1. Note that enumeration types cannot be input or output directly. Using Example 7-5,
explain how to indirectly perform input and output operations with enumeration types.
Teaching
Tip
Point out that a compiler error is not generated when a direct input/output
operation is attempted. Rather, the integral position of the value in the list is
used.
Functions and Enumeration Types
Declaring Variables When Defining the Enumeration Type
1. Discuss the syntax for combining the definition and declaration of an enumeration type
into one statement. Provide some examples.
Anonymous Data Types
2. Discuss the drawbacks of using anonymous types. Use the code in this section to
illustrate.
typedef Statement
1. Explain the use of the typedef statement as an alias to a type.
2. Describe the syntax of the typedef statement.
Teaching
Tip
Students may wonder why it is necessary to use the typedef statement at all.
Discuss some advantages to using typedef statements, such as readability.
Point out that typedef statements are typically used with more advanced C++
constructs such as pointers to simplify the code (in the case of pointers, it
eliminates the need for asterisks).
3. Step through the “The Game of Rock, Paper, and Scissors” Programming Example with
your students to consolidate their understanding of enumeration types.
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 7-5
Quick Quiz 1
1. C++ allows you to define a new simple data type in which you specify its name and
values, but not its ____________________.
2. True or False: The values you specify for an enumeration type must be identifiers.
3. True or False: The default value assigned to enumerators starts at 1.
4. You must use the ____________________ operator in order to perform arithmetic
operations on an enumeration type.
Namespaces
1. Note that the namespace mechanism is a feature of ANSI/ISO Standard C++. Explain
that it was designed to solve the problem of overlapping global identifiers in header
files and libraries.
2. Describe the syntax of a namespace statement.
3. Discuss the syntax for accessing a namespace member. Also, review the syntax for
the using namespace statement and discuss how this simplifies the process of
accessing namespace members.
4. Use Examples 7-8 through 7-12 to illustrate the use of both C++ and user-defined
namespaces.
Teaching
Tip
Relate the use of the namespace statement in Example 7-12 to its use in
previous chapters as a means of accessing C++ standard header files. This will
help clarify the concept of namespaces for your students.
string Type
1. Reiterate that the C++ library provides the string data type as part of the ANSI/ISO
language standard.
2. Review the syntax rules and operations involving the use of C++ strings.
page-pf6
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 7-6
3. Introduce the C++ array subscript operator and discuss how it is used to access
characters in a string.
Teaching
Tip
You might wish to provide some additional code using the subscript operator
because it is commonly used in all programming languages for arrays and string
processing. Briefly define an array and explain that a string is an array of
characters.
Additional string Operations
1. Note that the C++ string has a data type, string::size_type, and a named
2. Briefly review Table 7-1, which lists some string functions
4. Note that the size function returns the same value as the length function.
5. Explain that the find function searches a string for a particular substring and returns
7. Using Example 7-16 discuss the use of the insert and replace functions.
9. Discuss the syntax of the substr function. Use Example 7-17 to illustrate how the
parameters work.
10. Explain that the swap function is used to interchange the values of two string
variables. Describe the syntax for calling this function. Use Example 7-18 to illustrate
this function.
Teaching
Tip
Provide an overview of additional string operations by using Appendix F or the
following Web site: http://en.cppreference.com/w/cpp/string/basic_string.
page-pf7
11. Encourage students to step through the “Pig Latin Strings” Programming Example to
consolidate their understanding of string manipulation.
Quick Quiz 2
1. The position of the first character in a string is ____________________.
2. Which C++ function(s) return(s) the number of characters currently in a string?
3. True or False: The data type string is not part of the C++ language.
4. True or False: The substr function has one parameter.
Class Discussion Topics
1. Ask your students if there might be a way to directly access commonly used operators,
2. The using namespace statement is commonly used in programs to simplify the
code involving C++ header files. Students might wonder if it is really necessary to code
Additional Projects
1. Write a program that defines an enumeration type for the months of the year. The value
of each month’s name should be set to the number of days in that month (e.g.,
2. Recall that the ctime() function can be used to return the current time. Write a
program that parses the string returned from ctime and displays a more user-friendly
page-pf8
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 7-8
© 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 use.
Additional Resources
2. Namespaces:
3. C++ Notes: <string> class:
Key Terms
Anonymous type: a data type in which you directly specify values in the variable
declaration with no type name
Array subscript operator []: used to access an individual character within a string by
specifying the character's position within the square brackets
Enumeration type: a user-defined simple data type
Enumerator: an identifier used in an enumeration type

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.