Programming Languages Chapter 10 Programming From Problem Analysis Program Design Eighth Edition Classes And Data

subject Type Homework Help
subject Pages 9
subject Words 3511
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 10-1
Chapter 10
Classes and Data Abstraction
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
Tips, Classroom Activities, and Lab Activities. Pay special attention to teaching tips and
activities geared towards quizzing your students and enhancing their critical thinking skills.
In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
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 10-2
Lecture Notes
Overview
Chapter 10 introduces a structured data type called a class, which is specifically
designed to group data and functions. After examining its structure, students will learn
how to create and use classes. The typical members of a class will be discussed,
including accessors, mutators, constructors, and destructors. We will define the
properties of these members, such as private, protected, public, and static.
Students will also learn about abstract data types and how classes are used to implement
them. In the process, they will be introduced to the concept of information hiding.
Finally, we will discuss information hiding as implemented in C++.
Objectives
In this chapter, the student will:
Learn about classes
Learn about private, protected, and public members of a class
Explore how classes are implemented
Become aware of accessor and mutator functions
Examine constructors and destructors
Learn about the abstract data type (ADT)
Explore how classes are used to implement ADTs
Teaching Tips
Classes
1. Review the concepts of object-oriented design discussed in Chapter 1, including the
definition of an object.
page-pf3
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-3
Teaching
Tip
The terms object and class may be confusing to students because they are often
used interchangeably. Because these terms will be referred to throughout the
chapter, emphasize the difference between them before continuing. A class is a
definition of a data type; an object is an instance variable of the class. You might
relate it to the struct type definition and variables declared of that struct
type.
2. Define a class and examine the syntax for declaring a class.
Teaching
Tip
Clarify the concept of grouping data and operations together into one data type
by providing some real-life examples, such as electrical appliances or
transportation vehicles. Discuss the typical operations associated with these
objects.
4. Explain how member access specifiers control access to the members of a class.
Illustrate with the clockType class presented in this section.
Teaching
Tip
Because the clockType class is used throughout this chapter to introduce new
concepts, go over the class definition in detail and verify that students are on
track.
Unified Modeling Language Class Diagrams
1. Use Figure 10-1 to illustrate how to use UML to model the members of a class.
Variable (Object) Declaration
1. Explain how to declare a class variable, or class object/class instance.
2. Describe how memory is allocated for a class, and use Figure 10-2 to illustrate this
concept.
Teaching
Tip
Discuss why it is acceptable to have one physical copy of a member function in
memory, whereas a data variable is allocated memory when an object is
declared.
page-pf4
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-4
Accessing Class Members
1. Discuss how to access class members and explain the rules involving access of
private and public members. Illustrate with Example 10-1.
Built-in Operations on Classes
1. Note the built-in operations that are valid for class objects: assignment and member
access.
Teaching
Tip
Mention that most programmers overload conventional operators for the classes
they implement in order to provide the same behaviors for the client.
Assignment Operator and Classes
1. Demonstrate how the assignment operator works with classes using Figure 10-3.
Class Scope
1. Explain the terms automatic and static as they relate to class scope.
2. Discuss the scope of both class objects and class members in a program.
Teaching
Tip
Emphasize that classes generally should not be declared static because it defeats
the purpose of object-oriented programming. However, discuss situations in
which using static classes is preferable. You might consider using an example
from the STL.
Functions and Classes
1. Discuss the rules for classes regarding parameter passing and return types.
Reference Parameters and Class Objects (Variables)
1. Explain how to determine whether to pass classes by value or by reference. Note the use
of constant reference parameters for classes that should not be modified in a called
function.
Teaching
Tip
Note that the guidelines for parameter passing in the two previous sections are
the same as for C++ structs.
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-5
Implementation of Member Functions
2. Explain the use of the scope resolution operator in function implementations.
3. Illustrate the process of implementing class member functions by stepping through the
5. Discuss how classes are used in a program. Define the terms client and instance
variables as they relate to program use.
Teaching
Tip
Clarify the term client in terms of the programs your student will write to test
their classes. In most cases, the client will be the test program that contains
main. In other words, this program will instantiate and manipulate a class as a
client.
Accessor and Mutator Functions
1. Define accessor and mutator functions and explain their purpose as member functions in
a class.
3. Illustrate the use of a class in a program with Example 10-2 presented in this section.
Order of public and private Members of a Class
1. Explain the rules regarding the declaration of private and public members of a
class. Use Examples 10-3 through 10-5 to illustrate various orderings of class members.
Teaching
Tip
Discuss the standard conventions for the ordering of public and private
members in a class. Share your own preferences with the class.
Quick Quiz 1
1. What are the two built-in operations that are valid for class objects?
page-pf6
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-6
© 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.
Answer: assignment (=) and member access (.)
2. By default, all members of a class are ____________________ (public/private).
3. True or False: In the definition of a class, you initialize the variable when you declare it.
4. True or False: If a member of a class is a function, it can directly access any member of
the class - member variables and member functions.
Constructors
2. Mention the two types of constructors.
3. Describe the properties of a constructor, including its name, parameters, and return type.
Invoking a Constructor
Invoking the Default Constructor
1. Examine the syntax to invoke the default constructor of a class.
Teaching
Tip
Emphasize that default constructors in C++ cannot be invoked with parentheses.
Note that some programming languages, such as Java, require empty
parentheses.
Invoking a Constructor with Parameters
1. Examine the syntax to invoke a constructor with parameters.
3. Illustrate how constructors are implemented and used in a program with Example 10-6
page-pf7
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-7
Constructors and Default Parameters
1. Explain how to implement a constructor with default parameters.
Teaching
Tip
Review the rules for declaring formal parameters in a function (Chapter 6) to
clarify why the clockType constructor with default parameters in this section
can replace two of its previously defined constructors.
Classes and Constructors: A Precaution
1. Explain what happens when a class does not include a default constructor. Discuss the
In-line Initialization of Data Members and the Default Constructor
1. Explain that the C++11 standard allows the initialization of data members when they are
declared in a class. This may eliminate the declaration of the default constructor.
Arrays of Class Objects (Variables) and Constructors
1. Discuss how to initialize an array of class objects by providing the appropriate
constructor(s).
2. Use Figures 10-8 and 10-9 to illustrate the process of assigning values to array
page-pf8
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-8
Quick Quiz 2
1. What is meant by an automatic class object?
2. True or False: Classes can be passed as parameters to functions either by value or by
reference.
3. In a function definition’s heading, the name of the function is the name of the class,
followed by the ___________________, followed by the function name.
::
4. What is an accessor function?
Data Abstraction, Classes, and Abstract Data Types
1. Describe the concept of data abstraction and its usefulness in creating robust
applications.
Teaching
Tip
Explain data abstraction by relating the clockType class with an actual alarm
clock. First, separate the implementation details from the operations. When using
the clockType class, who/what is the client? In a real-world clock, which of
these operations would also be abstracted from the client, assuming that the
client is a human being? What additional operations might be provided as public
operations?
3. Use the listType class presented in this section to demonstrate how classes can
represent ADTs. Present the UML diagram for the listType class shown in Figure
10-10.
page-pf9
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-9
A struct Versus a class
1. Discuss the difference between a struct and a class. Emphasize the enhanced
Information Hiding
1. Discuss the reasons for separating the specification and implementation details of a
class.
Teaching
Tip
Emphasize that separating the specification of a class from the implementation is
beneficial to programmers as well as clients. Discuss the benefits of restricting
access to a program when the application development involves a large team.
2. Explain how to create separate implementation and header files in C++.
3. Introduce the terms precondition and postcondition. Describe the purpose of including
Executable Code
2. Introduce the terms build, rebuild, and make. Explain the steps that are involved in
creating an executable file with separate header and implementation files.
Teaching
Tip
Ask students to share the type of compiler and/or development environment they
are using at home. Verify that everyone is comfortable with the
compiling/linking steps for their particular environment as well as for any
environment that is available at the school labs.
More Examples of Classes
1. Examine the design, specification, and implementation of the circleType class in
Example 10-8.
page-pfa
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-10
© 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
3. Examine the design, specification, and implementation of the personType class in
Example 10-10. Correlate this with the UML diagram for this class shown in Figure 10-
11.
Inline Functions
1. C++ allows inline function definitions, i.e., those that occur within the class definition.
2. Inline functions are typically used for very short definitions.
Static Members of a Class
1. Define a static member of a class and describe its purpose.
2. Note the rules for declaring and accessing static members in a class. Use Examples 10-
12 and 10-13 to illustrate.
3. Discuss how memory is allocated for static member variables.
Teaching
Tip
Emphasize that using static members is not a truly object-oriented approach
because the members are not associated with a particular object. However, static
variables are useful for many applications. Discuss situations in which using
static variables is the preferred approach in OOP.
4. Encourage students to work through the “Juice Machine” Programming Example to
consolidate their understanding of the concepts presented in this chapter.
Quick Quiz 3
1. Separating the design details of what an object does from its implementation is called
____________________.
2. The set of values belonging to an ADT is called a(n) ____________________.
3. A(n) ____________________ file contains the specification details of a class.
page-pfb
4. True or False: The implementation file of a class contains the function main.
Class Discussion Topics
1. Why do your students think initializing a class member variable during declaration is
not permitted?
2. This chapter only briefly introduces UML diagrams. Discuss the benefits of using UML
diagrams in real-world applications. Why is it important to design an application before
implementing it?
3. Is it always necessary to provide a default constructor for a class? Why or why not?
Additional Projects
1. Modify the student name/test scores program you created in Chapter 9 to use a class
for the student information instead of a struct. The member variables will be the
2. Modify the capitals program from Chapter 9 and create a countryType class to store
the same information. Provide accessor and mutator methods. This class will contain the
Additional Resources
1. Learn about C++ Classes and Objects:
2. Classes (I):
3. C++ Constructors:
4. Introduction to C++ Class Code and Header Files:
page-pfc
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 10-12
Key Terms
Abstract data type (ADT): a data type that specifies the logical properties without the
implementation details
Build, rebuild, or make: when applied to a project, commands that tell the system to
automatically compile and link all files required to create the executable code
Class: a collection of a fixed number of components with the operations you can
perform on those components
Class object (class instance, object): a class variable
Client: program or software that uses and manipulates the objects of a class
Constant function: a member function that cannot modify member variables of the
Instance variables: non-static data members of a class
Member access operator: the dot (period); used to access a member variable or
function
Members: the components of a class
Mutator function: a member function of a class that modifies the value(s) of the

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.