Programming Languages Chapter 11 Programming From Problem Analysis Program Design Eighth Edition Inheritance And Composition

subject Type Homework Help
subject Pages 8
subject Words 2208
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 11-1
Chapter 11
Inheritance and Composition
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 11-2
Lecture Notes
Overview
Chapter 11 discusses additional techniques of programming with classes. Classes
provide reusability through their various relationships with each other. In this chapter,
students are introduced to two means of relating classes: inheritance (an “is-a”
relationship) and composition (a “has-a” relationship). They will also learn about the
three basic principles of object-oriented design: encapsulation, inheritance, and
polymorphism.
Objectives
In this chapter, the student will:
Learn about inheritance
Learn about derived and base classes
Explore how to redefine the member functions of a base class
Examine how the constructors of base and derived classes work
Learn how the destructors of base and derived classes work
Learn how to construct the header file of a derived class
Become aware of stream classes hierarchy
Explore three types of inheritance: public, protected, and private
Learn about composition (aggregation)
Become familiar with the three basic principles of object-oriented design
Teaching Tips
Inheritance
1. Explain why programmers might choose to extend a class definition when designing a
new class, rather than write one from scratch.
Teaching
Tip
Inheritance is one of the primary aspects of OOD; therefore, you might want to
provide some additional real-world examples before continuing the discussion of
this section. For example, demonstrate how a transportation class can be a base
class from which many different modes might be derived, such as a bike, car, or
plane.
2. Examine how the personType class from Chapter 10 can be extended to create a
partTimeEmployee class.
page-pf3
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-3
Teaching
Tip
Discuss why extending the personType class is a better solution than simply
writing another partTimeEmployee class and copying in the variables you
need to use. Emphasize that this is a small example, but that many applications
have large amounts of code that might be reused. Talk about the testing process
for large applications and how reusable code might make the development of
new applications more efficient and less prone to errors.
3. Discuss how the clockType class can be extended to provide additional capabilities
such as time zone information.
4. Define the terms derived class and base class.
5. Define single and multiple inheritance. Use Figure 11-1 to illustrate the structure of a
single inheritance hierarchy.
Teaching
Tip
Note that not all object-oriented programming languages offer multiple
inheritance. The reasons for this will be explored when multiple inheritance is
discussed.
6. Examine the syntax for declaring a derived class. Illustrate with the shape and
circle classes in Example 11-1.
Redefining (Overriding) Member Functions of the Base Class
1. Define the term overriding.
2. Explain how to override a function of a base class in a derived class.
Teaching
Tip
Students may be unclear of the difference between overriding and overloading.
Explain that a derived class member function overrides a base class function if it
has the same name and parameter lists. However, if the derived class member
function has differing parameter lists, it is not overriding the base function, but is
overloading the inherited function in the derived class.
3. Examine the rectangleType and boxType classes presented in this section and
page-pf4
4. Explain how to call a base member function from a derived class under various
circumstances. Discuss the situations in which this is useful with the code presented in
this section.
Constructors of Derived and Base Classes
2. Examine the syntax for executing a default base class constructor from a derived class.
4. Illustrate how the constructors for derived and base classes work together using
Example 11-2.
Teaching
Tip
Verify that students are comfortable with the syntax for calling base constructors
from a derived class constructor. Walk through each component of the boxType
constructor with parameters to clarify.
5. Step through Example 11-3 and Figure 11-5 to illustrate how base and derived classes
are used in a program.
6. Explain how to create header files for derived classes.
Destructors in a Derived Class
1. Note that when the destructor of a derived class executes, it automatically invokes the
destructor of the base class.
763-764.
Multiple Inclusions of a Header File
1. Examine how to use the ifndef, define, and endif preprocessor commands to
avoid compiler errors when including a header file in more than one file in a program.
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-5
Teaching
Tip
Explain why multiple inclusions of a header file can generate compiler errors.
Note that programmers often place the entire header file between the
ifndef/define and endif commands to avoid any errors.
C++ Stream Classes
Protected Members of a Class
1. Explain when to declare the members of a base class as protected.
Inheritance as public, protected, or private
1. Step through the general rules for accessing public, protected, and private
base classes.
Teaching
Tip
Reiterate the importance of information hiding from the client. Ask students to
discuss how the protected member access specifier assists in information
hiding.
Accessing protected Members in the Derived Class
1. Illustrate how a derived class can directly access a protected member of a base class
using Example 11-5.
Quick Quiz 1
1. ____________________ represents an “is-a” relationship.
2. Classes that are created from existing classes are called ____________________
classes.
3. True or False: A derived class object can directly access the private members of a base
class.
4. True or False: A derived class can have a constructor with default parameters.
page-pf6
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-6
Composition (Aggregation)
1. Explain the “has-a” relationship between classes, where one or more members of a class
are objects of another class.
2. Illustrate composition by showing how the dateType class presented in this section is
an object in the personalInfo class. Illustrate with the UML diagram in Figures 11-
7 and 11-8, and the accompanying code.
Teaching
Tip
Note the differences in how class constructors are used in inheritance and
composition relationships. In inheritance, the base class constructor is invoked
with the class name, whereas in composition, the member object name is used to
call its own constructor.
Object-Oriented Design (OOD) and Object-Oriented Programming (OOP)
1. Describe the limitations of structured programming.
Teaching
Tip
Spend some additional time discussing the differences between OOP and
structured programming to solidify the students’ understanding of both of these
programming techniques.
2. Define the three basic principles of object-oriented programming: encapsulation,
page-pf7
2. Encourage students to work through the “Grade Report” Programming Example to
consolidate their understanding of inheritance and composition.
Quick Quiz 2
1. What is meant by the term encapsulation?
2. ____________________ is the ability to use the same expression to denote different
operations.
3. Objects interact with each other via ____________________.
4. True or False: In structured programming, the function is a fundamental entity.
Class Discussion Topics
1. Ask students to consider relevant issues when determining whether to use protected
or private members in a base class. Are there any risks involved with using the
protected member access specifier instead of the private member access
specifier?
Additional Projects
2. Design a simple inheritance hierarchy that has a base class named foodType. The
base class has a variable for the name of the object, a variable for the calories of the
object (given to the constructor as a parameter), and accessor and mutator functions for
each. The derived classes will be the various food groups in the plate.
page-pf8
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-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
1. Learn about C++ Classes and Objects: Learn about Inheritance and Polymorphism:
2. Six Different Kinds of Composition:
Key Terms
Base classes: classes from which you derive new classes

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.