Programming Languages Chapter 13 Programming From Problem Analysis Program Design Eighth Edition Overloading And Templates

subject Type Homework Help
subject Pages 9
subject Words 3205
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 13-1
Chapter 13
Overloading and Templates
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 13-2
Lecture Notes
Overview
Chapter 13 covers two important concepts in object-oriented programming: operator
overloading and templates. Students will explore how to overload the standard operators
in user-defined classes. They will also learn about the use of the this pointer and
friend functions in user-defined classes. Finally, students will examine how to create
class and function templates in order to provide generic code in a program.
Objectives
In this chapter, the student will:
Learn about overloading
Become familiar with the restrictions on operator overloading
Examine the pointer this
Learn about friend functions
Learn how to overload operators as members and nonmembers of a class
Discover how to overload various operators
Become familiar with the requirements for classes with pointer member variables
Learn about templates
Explore how to construct function templates and class templates
Become aware of C++11 random number generators
Teaching Tips
Introduction
1. Introduce the concept of writing generic code for related functions and classes, and put
this into the context of object-oriented development.
Why Operator Overloading Is Needed
2. Review the definition of operator overloading.
page-pf3
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 13-3
Teaching
Tip
Emphasize that clients of a class find it much more convenient and intuitive to
use standard operators to perform operations on objects of the class. Use the
<string> header file as an example of how operator overloading can ease the
task of programming. Point out that this is another form of encapsulation: the
client is aware that the operators work as expected, but is unaware of the
implementation details.
Operator Overloading
1. List the standard operators that C++ overloads, including the arithmetic operators and
the stream extraction and insertion operators.
2. Explain that C++ allows programmers to overload most operators to work with a
specific application.
Teaching
Tip
Note that C++ does not allow you to create new operators.
3. Define the term operator function and note the syntax for an operator function name.
Syntax for Operation Functions
2. Remind students that the assignment and member selection operators are the only built-
in operators in C++ for classes.
Teaching
Tip
Reiterate that all other standard operators for classes must be explicitly
overloaded. Students are probably more accustomed to using applications as
clients (with operator overloading already in place), so this might be somewhat
counterintuitive.
3. Describe the basic steps for overloading an operator.
Overloading an Operator: Some Restrictions
1. Step through the restrictions on operator overloading that are discussed in this section.
page-pf4
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 13-4
Teaching
Tip
Ask students what they think the basis is for some of the restrictions described in
this section. Which of them are they more likely to forget when overloading a
function (e.g., that the associativity of an overloaded function must stay the same
as the original) and why?
Pointer this
2. Use Examples 13-2 and 13-3 to explain how the this operator is used in a program
and why it is so useful.
Teaching
Tip
Emphasize that the this pointer cannot have its value changed; in other words,
it always points to the object that is calling the function in which it is used.
Teaching
Tip
Note that this is particularly useful in operator overloading. Ask students to
think of reasons why this might be the case.
Quick Quiz 1
1. What is operator overloading?
2. The function heading for an operator function uses the reserved word
____________________.
3. True or False: The associativity of an operator can be modified in an overloaded
operator function.
4. Every object maintains a hidden pointer to itself called the ____________________
pointer.
Friend Functions of Classes
1. Define a friend function and describe the purpose of friend functions in a class.
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 13-5
Teaching
Tip
Discuss the additional overhead involved with accessing private member
functions with accessor and mutator functions instead of friend functions. Ask
students to examine the example in this section and think about how it would be
implemented without the use of friend functions.
2. Examine the syntax of a friend function prototype and discuss its placement in the
header file of a class.
3. Discuss the implementation of a friend function. Use Example 13-4 to illustrate how
Operator Functions as Member Functions and Nonmember Functions
1. Step through the rules for declaring operator functions as member functions and
nonmember functions.
Teaching
Tip
Mention that nonmember functions are primarily used for operator overloading.
Note that the C++ ternary operator cannot be overloaded.
Overloading Binary Operators
2. Describe how to overload a binary operator as a nonmember function. Examine the
syntax for the function prototype. Use Example 13-6 to illustrate.
Teaching
Tip
Emphasize that in the function heading of a friend function definition, the name
of the class and scope resolution operator are not included, because a friend
function is not a member of the class.
Overloading the Stream Insertion (<<) and Stream Extraction (>>) Operators
1. Explain why an operator function that overloads the insertion or extraction operator
must be a nonmember function of a class.
page-pf6
2. Describe how to overload the stream insertion operator. Examine the syntax of the
function prototype.
Teaching
Tip
Verify that students understand why both parameters in the overloaded stream
insertion function are reference parameters, as well as why the second parameter
is declared const.
3. Explain the mechanics of the return type of stream operators and why it must be a
reference type.
4. Briefly describe how to overload the stream extraction operator.
Overloading the Assignment (=) Operator
1. Review how the built-in assignment operator produces a shallow copy for classes with
pointer member variables.
2. Examine the syntax for an overloaded assignment operator function prototype and
definition. In particular, note the constant reference return type and use of the this
page-pf7
4. Examine the general syntax for the function prototype and definition of an overloaded
post-increment operator as a member function.
5. Examine the general syntax for the function prototype and definition of an overloaded
post-increment operator as a nonmember function.
Teaching
Tip
Verify that students understand why the post-increment version of an overloaded
increment operator needs two parameters. What does the compiler do when the
second dummy parameter is not included?
Operator Overloading: Member versus Nonmember
1. Discuss when to use a member function or nonmember function for operator functions
Classes and Pointer Member Variables (Revisited)
1. Review the three functions a class with pointer member variables should include: an
Operator Overloading: One Final Word
1. Review overloaded functions with the modified clockType class and the “Complex
Numbers” Programming Example in this section.
Teaching
Tip
Ask your students to work in small groups and code the overloaded subtraction
and division functions for the “Complex Numbers” Programming Example. This
will give them firsthand experience with coding overloaded operators and
increase their comfort level when programming on their own.
Quick Quiz 2
1. A(n) ____________________ function is a nonmember function that has access to all
the public and private variables of a class.
2. Which operator functions cannot be overloaded as friend functions?
page-pf8
3. True or False: The stream extraction and stream insertion operators must be overloaded
as member functions.
4. True or False: The body of an overloaded assignment operator should include self-
assignments.
Overloading the Array Index (Subscript) Operator ([])
1. Remind students that an overloaded subscript operator must be a member of the class.
2. Explain that the subscript operator must handle two cases: one for constant arrays and
the other for nonconstant arrays.
4. Step through the “newString” Programming Example in this section to illustrate the
use of the overloaded array subscript operator.
Teaching
Tip
Look at the <string> header file again and ask your students to comment on
the similarities and differences of the string class definition with the
newString class definition, particularly in terms of functions and operating
overloading. Do they think the function implementations are similar?
Function Overloading
1. Introduce the concept of function overloading.
2. Relate operator overloading to function overloading.
3. Discuss the use of multiple constructors as an example of function overloading.
Teaching
Tip
Note that this section is simply a quick review of the function overloading
concepts discussed in previous chapters. Students need to have an understanding
of function overloading to grasp the importance of templates, which will be
discussed next.
page-pf9
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 13-9
Templates
1. Define the terms function template and class template.
Teaching
Tip
Emphasize the importance of templates in any object-oriented programming
language. Note that some programming languages use the term generics to
describe the use of templates.
2. Examine the syntax for declaring a template. Emphasize that the type is a parameter to
the template, much like a variable is a parameter to a function.
Function Templates
1. Explain that templates can simplify the process of overloading functions by providing
Class Templates
1. Explain how class templates can be used to write a single code segment for a set of
related classes.
2. Examine the syntax for declaring a class template.
3. Define the term parameterized type.
Teaching
Tip
Mention that the classes in the C++ Standard Library are templates.
4. Explain the term instantiation of a class template.
Teaching
Tip
Emphasize that user-defined templates can be instantiated with built-in types or
user-defined types.
5. Compare the listType class with the listType template to demonstrate the
usefulness of class templates.
6. Explain why separating a class template into a header and implementation file (as
described thus far in the text) is a problem for the compiler. Describe solutions to this
problem.
page-pfa
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 13-10
Teaching
Tip
Verify that students understand how to compile class templates. Although this
text provides a solution of placing both the class definition and implementation
in the same file, it might be useful to show students an example of a class
template with separate files. In this scenario, you would place a directive to
include the implementation file at the end of the header file.
C++11 Random Number Generator
1. Remind students of the functions rand and srand, as discussed in chapter 5. Explain
2. Illustrate the difference between an engine and a distribution using the sample code
provided.
Quick Quiz 3
1. C++ simplifies the process of overloading functions by providing
____________________.
2. Class templates are also called ____________________ types.
3. True or False: Including multiple constructors of a class is an example of function
overloading.
4. True or False: A template instantiation can only be created with a built-in type.
Class Discussion Topics
1. Moderate a discussion on whether friend functions violate the principles of object-
oriented programming by providing access to private members of a class. Ask your
page-pfb
2. Remind students that some overloaded operators, such as assignment operators and
3. Review the different approaches to compiling class templates. Which do your students
think is the best approach and why?
Additional Projects
1. Chapter 6 included a project that simulates a questionnaire to find a roommate. In this
program, the user is prompted with various questions that might determine a suitable
Additional Resources
2. Overloading Operators; The keyword this:
4. Class Templates:
5. C++11 Random Number Generator:
Key Terms
Class template: allows you to write a single code segment for a set of related classes
Complex number: a number of the form, a + ib, where i2 = -1, and a and b are real
numbers
Conversion constructor: a single-parameter function that converts its argument to an
object of the constructor’s class
page-pfc
© 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.

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.