You are on page 1of 8

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

© 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.
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.

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
Teaching
this section. For example, demonstrate how a transportation class can be a base
Tip
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.

© 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.
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-3

Discuss why extending the personType class is a better solution than simply
writing another partTimeEmployee class and copying in the variables you
Teaching need to use. Emphasize that this is a small example, but that many applications
Tip 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.

Note that not all object-oriented programming languages offer multiple


Teaching
inheritance. The reasons for this will be explored when multiple inheritance is
Tip
discussed.

6. Examine the syntax for declaring a derived class. Illustrate with the shape and
circle classes in Example 11-1.

7. Discuss the rules regarding derived class definitions in C++. Emphasize how issues of
access are handled between base and derived classes with the private and public
member access specifiers.

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.

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
Teaching
has the same name and parameter lists. However, if the derived class member
Tip
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
describe how overriding takes place in some of the member functions.

© 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.
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-4

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

1. Explore the issues involved with executing a base class constructor from a derived
class.

2. Examine the syntax for executing a default base class constructor from a derived class.

3. Examine the syntax for executing base class constructors with parameters from a
derived class.

4. Illustrate how the constructors for derived and base classes work together using
Example 11-2.

Verify that students are comfortable with the syntax for calling base constructors
Teaching
from a derived class constructor. Walk through each component of the boxType
Tip
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.

2. Explain how to create new header files for new classes that contain commands to tell
the computer where to look for the base class definition. Review the example on Pages
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.

© 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.
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-5

Explain why multiple inclusions of a header file can generate compiler errors.
Teaching
Note that programmers often place the entire header file between the
Tip
ifndef/define and endif commands to avoid any errors.

C++ Stream Classes

1. Describe how C++ stream classes are implemented using the inheritance mechanism
with Figure 11-6.

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.

Reiterate the importance of information hiding from the client. Ask students to
Teaching
discuss how the protected member access specifier assists in information
Tip
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.
Answer: Inheritance

2. Classes that are created from existing classes are called ____________________
classes.
Answer: derived

3. True or False: A derived class object can directly access the private members of a base
class.
Answer: False

4. True or False: A derived class can have a constructor with default parameters.
© 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.
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-6

Answer: True

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.

Note the differences in how class constructors are used in inheritance and
Teaching composition relationships. In inheritance, the base class constructor is invoked
Tip 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.

Spend some additional time discussing the differences between OOP and
Teaching
structured programming to solidify the students’ understanding of both of these
Tip
programming techniques.

2. Define the three basic principles of object-oriented programming: encapsulation,


inheritance, and polymorphism.

Teaching Review examples of the above principles in action with the C++ library classes,
Tip functions, and operators that the students have studied thus far.

3. Introduce the concept of parametric polymorphism. Note that this will be discussed in
more detail in a later chapter.

4. Briefly review the history of object-oriented programming.

Identifying Classes, Objects, and Operations

1. Describe the technique of using nouns and verbs to identify classes and objects in a
problem. Illustrate with the problem statement in this section.

© 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.
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-7

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?
Answer: Encapsulation is the ability to combine data, and the operations on that data, in
a single unit.

2. ____________________ is the ability to use the same expression to denote different


operations.
Answer: Polymorphism

3. Objects interact with each other via ____________________.


Answer: function calls

4. True or False: In structured programming, the function is a fundamental entity.


Answer: True

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?

2. Note that many facets of object-oriented programming and structured programming


overlap. Ask students to think of previous programming examples in which OOP and
structured programming are both used (e.g., overloading the get function).

Additional Projects
1. Create a group of related classes that represents the food plate advocated by current
health experts. You can find a sample guide here: https://www.choosemyplate.gov/.

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.

Each derived class includes a const variable that indicates how many servings per day
of that food group should be consumed. Include appropriate constructors, accessor and
mutator functions, and a Boolean function that takes a number of servings and returns a

© 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.
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 11-8

message indicating whether it is within the acceptable range. Test your classes with a
user program that declares and manipulates objects of every food group.

Additional Resources
1. Learn about C++ Classes and Objects: Learn about Inheritance and Polymorphism:
http://cplus.about.com/od/learning1/ss/cppobjects.htm#step5

2. Six Different Kinds of Composition:


http://www.conradbock.org/compkind.html

3. Overriding in Object-Oriented Programming:


http://www.codersource.net/2010/02/11/overriding-in-object-oriented-programming/

4. Polymorphism:
http://www.cplusplus.com/doc/tutorial/polymorphism/

Key Terms
 Base classes: classes from which you derive new classes
 Composition (aggregation): one or more members of a class are objects of another
class type; a “has-a” relationship
 Derived classes: new classes that are created from base classes
 Encapsulation: the ability to combine data, and operations on that data, in a single unit
 Inheritance: the ability to create new classes from existing classes; an “is-a”
relationship
 Multiple inheritance: when a derived class is derived from more than one base class
 Polymorphism: the ability to use the same expression to denote different operations
 Single inheritance: when an object is derived from a single class

© 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.

You might also like