0% found this document useful (0 votes)
4 views36 pages

chtp8 19

The document discusses the inheritance hierarchy in C++ programming, specifically focusing on the CommissionEmployee and BasePlusCommissionEmployee classes. It explains the importance of including base class headers, the linking process, and the use of protected data members versus private data members in class design. Additionally, it covers constructors, destructors, and the implications of different inheritance types on member accessibility.

Uploaded by

Hamzih Alsmadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views36 pages

chtp8 19

The document discusses the inheritance hierarchy in C++ programming, specifically focusing on the CommissionEmployee and BasePlusCommissionEmployee classes. It explains the importance of including base class headers, the linking process, and the use of protected data members versus private data members in class design. Additionally, it covers constructors, destructors, and the implications of different inheritance types on member accessibility.

Uploaded by

Hamzih Alsmadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Instructor Note: C How to Program, Chapter 19 is a copy of C++ How to

Program, 9/e Chapter 11. We have not renumbered the PowerPoint Slides.

Chapter 11
Object-Oriented Programming:
Inheritance
C++ How to Program, 9/e

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.3.3 Creating a CommissionEmployee–
BasePlusCommissionEmployee Inheritance Hierarchy
(cont.)
Including the Base-Class Header in the Derived-Class
Header with #include
•We #include the base class’s header in the derived class’s
header (line 8 of Fig. 11.10).
•This is necessary for three reasons.
– The derived class uses the base class’s name in line 10, so we
must tell the compiler that the base class exists.
– The compiler uses a class definition to determine the size of
an object of that class. A client program that creates an object
of a class #includes the class definition to enable the
compiler to reserve the proper amount of memory for the
object.
– The compiler must determine whether the derived class uses
the base class’s inherited members properly.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.3.3 Creating a CommissionEmployee–
BasePlusCommissionEmployee Inheritance Hierarchy
(cont.)

Linking Process in an Inheritance Hierarchy


•In Section 3.7, we discussed the linking process for creating
an executable GradeBook application.
•The linking process is similar for a program that uses
classes in an inheritance hierarchy.
•The process requires the object code for all classes used in
the program and the object code for the direct and indirect
base classes of any derived classes used by the program.
•The code is also linked with the object code for any C++
Standard Library classes used in the classes or the client
code.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.3.4 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using protected Data
• In this section, we introduce the access specifier
protected.
• To enable class BasePlusCommissionEmployee to
directly access CommissionEmployee data members
firstName, lastName, socialSecurityNumber,
grossSales and commissionRate, we can declare
those members as protected in the base class.
• A base class’s protected members can be accessed
within the body of that base class, by members and
friends of that base class, and by members and
friends of any classes derived from that base class.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.3.4 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using protected Data (cont.)
Defining Base Class CommissionEmployee
with protected Data
•Class CommissionEmployee (Fig. 11.12) now
declares data members firstName, lastName,
socialSecurityNumber, grossSales and
commissionRate as protected (lines 31–36)
rather than private.
•The member-function implementations are
identical to those in Fig. 11.5.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
11.3.4 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using protected Data (cont.)
• BasePlusCommissionEmployee inherits from class
CommissionEmployee in Fig. 11.12.
• Objects of class BasePlusCommissionEmployee can
access inherited data members that are declared
protected in class CommissionEmployee (i.e., data
members firstName, lastName,
socialSecurityNumber, grossSales and
commissionRate).
• As a result, the compiler does not generate errors when
compiling the BasePlusCommissionEmployee
earnings and print member-function definitions in
Fig. 11.11 (lines 34–38 and 41–49, respectively).
• Objects of a derived class also can access protected
members in any of that derived class’s indirect base classes.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.3.4 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using protected Data (cont.)
Testing the Modified BasePlusCommissionEmployee
Class
•To test the updated class hierarchy, we reused the test
program from Fig. 11.9.
•As shown in Fig. 11.13, the output is identical to that of Fig.
11.9.
•The code for class BasePlusCommissionEmployee, which
is 74 lines, is considerably shorter than the code for the
noninherited version of the class, which is 161 lines, because
the inherited version absorbs part of its functionality from
CommissionEmployee, whereas the noninherited version
does not absorb any functionality.
•Also, there is now only one copy of the
CommissionEmployee functionality declared and defined in
class CommissionEmployee.
– Makes the source code easier to maintain, modify and debug.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
11.3.4 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using protected Data (cont.)
Notes on Using protected Data
•Inheriting protected data members
slightly increases performance, because we
can directly access the members without
incurring the overhead of calls to set or get
member functions.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.3.4 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using protected Data (cont.)
• Using protected data members creates two serious
problems.
– The derived-class object does not have to use a member
function to set the value of the base class’s protected data
member.
– Derived-class member functions are more likely to be written so
that they depend on the base-class implementation. Derived
classes should depend only on the base-class services (i.e., non-
private member functions) and not on the base-class
implementation.
• With protected data members in the base class, if the
base-class implementation changes, we may need to
modify all derived classes of that base class.
• Such software is said to be fragile or brittle, because a
small change in the base class can “break” derived-class
implementation.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.3.5 CommissionEmployee–
BasePlusCommissionEmployee Inheritance Hierarchy
Using private Data

• We now reexamine our hierarchy once


more, this time using the best software
engineering practices.
• Class CommissionEmployee now
declares data members firstName,
lastName, socialSecurityNumber,
grossSales and commissionRate as
private (as shown previously in lines 31–
36 of Fig. 11.4).
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
11.3.5 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using private Data (cont.)
Changes to Class CommissionEmployee’s Member Function
Definitions
•In the CommissionEmployee constructor implementation
(Fig. 11.14, lines 9–16), we use member initializers (line 12) to set
the values of members firstName, lastName and
socialSecurityNumber.
•We show how derived-class BasePlusCommissionEmployee
(Fig. 11.15) can invoke non-private base-class member
functions (setFirstName, getFirstName, setLastName,
getLastName, setSocialSecurityNumber and
getSocialSecurityNumber) to manipulate these data
members.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
11.3.5 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using private Data (cont.)
Changes to Class
BasePlusCommissionEmployee’s Member
Function Definitions
•Class BasePlusCommissionEmployee has
several changes to its member-function
implementations (Fig. 11.15) that distinguish it from
the previous version of the class (Figs. 11.10–11.11).
•Member functions earnings (Fig. 11.15, lines 34–
37) and print (lines 40–48) each invoke
getBaseSalary to obtain the base salary value.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
11.3.5 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using private Data (cont.)
BasePlusCommissionEmployee Member Function earnings
•Class BasePlusCommissionEmployee’s earnings function
(Fig. 11.15, lines 34–37) redefines class
CommissionEmployee’s earnings member function (Fig.
11.14, lines 85–88) to calculate the earnings of a base-salaried
commission employee. It also calls CommissionEmployee’s
earnings function.
– Note the syntax used to invoke a redefined base-class member
function from a derived class—place the base-class name and the
binary scope resolution operator (::) before the base-class
member-function name.
– Good software engineering practice: If an object’s member function
performs the actions needed by another object, we should call that
member function rather than duplicating its code body.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.3.5 CommissionEmployee–
BasePlusCommissionEmployee Inheritance
Hierarchy Using private Data (cont.)
BasePlusCommissionEmployee Member Function print
•BasePlusCommissionEmployee’s print
function (Fig. 11.15, lines 40–48) redefines class
CommissionEmployee’s print function (Fig.
11.14, lines 91–98) to output the appropriate
base-salaried commission employee
information.
•By using inheritance and by calling member
functions that hide the data and ensure
consistency, we’ve efficiently and effectively
constructed a well-engineered class.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.4 Constructors and Destructors in Derived
Classes
• Instantiating a derived-class object begins a chain of
constructor calls in which the derived-class constructor,
before performing its own tasks, invokes its direct base
class’s constructor either explicitly (via a base-class member
initializer) or implicitly (calling the base class’s default
constructor).
• If the base class is derived from another class, the base-class
constructor is required to invoke the constructor of the next
class up in the hierarchy, and so on.
• The last constructor called in this chain is the constructor of
the class at the base of the hierarchy, whose body actually
finishes executing first.
• The most derived-class constructor’s body finishes executing
last.
• Each base-class constructor initializes the base-class data
members that the derived-class object inherits.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.4 Constructors and Destructors in Derived
Classes (cont.)
• When a derived-class object is destroyed, the program
calls that object’s destructor.
• This begins a chain (or cascade) of destructor calls in
which the derived-class destructor and the destructors
of the direct and indirect base classes and the classes’
members execute in reverse of the order in which the
constructors executed.
• When a derived-class object’s destructor is called, the
destructor performs its task, then invokes the destructor
of the next base class up the hierarchy.
• This process repeats until the destructor of the final base
class at the top of the hierarchy is called.
• Then the object is removed from memory.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.4 Constructors and Destructors in Derived
Classes (cont.)
• Base-class constructors, destructors and overloaded
assignment operators (Chapter 10) are not inherited by
derived classes.
• Derived-class constructors, destructors and overloaded
assignment operators, however, can call base-class
versions.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.4 Constructors and Destructors in Derived
Classes (cont.)

C++11: Inheriting Base Class Constructors


•Sometimes a derived class’s constructors simply mimic
the base class’s constructors.
•A frequently requested convenience feature for C++11
was the ability to inherit a base class’s constructors.
•You can now do this by explicitly including a using
declaration of the form
using BaseClass::BaseClass;
•anywhere in the derived-class definition.
•In the preceding declaration, BaseClass is the base
class’s name.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.4 Constructors and Destructors in Derived
Classes (cont.)
• When you inherit constructors:
– By default, each inherited constructor has the same access level
(public, protected or private) as its corresponding base-
class constructor.
– The default, copy and move constructors are not inherited.
– If a constructor is deleted in the base class by placing =
delete in its prototype, the corresponding constructor in the
derived class is also deleted.
– If the derived class does not explicitly define constructors, the
compiler generates a default constructor in the derived class—
even if it inherits other constructors from its base class.
– If a constructor that you explicitly define in a derived class has
the same parameter list as a base-class constructor, then the
base-class constructor is not inherited.
– A base-class constructor’s default arguments are not inherited.
Instead, the compiler generates overloaded constructors in the
derived class.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.5 public, protected and private
Inheritance
• When deriving a class from a base class, the base class
may be inherited through public, protected or
private inheritance.
• Use of protected and private inheritance is rare.
• Figure 11.16 summarizes for each type of inheritance
the accessibility of base-class members in a derived
class.
• The first column contains the base-class access
specifiers.
• A base class’s private members are never accessible
directly from a derived class, but can be accessed through
calls to the public and protected members of the base
class.

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
11.6 Software Engineering with Inheritance

• When we use inheritance to create a new class


from an existing one, the new class inherits the
data members and member functions of the
existing class, as described in Fig. 11.16.
• We can customize the new class to meet our
needs by including additional members and by
redefining base-class members.
• The derived-class programmer does this in C++
without accessing the base class’s source code.
• The derived class must be able to link to the
base class’s object code.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
11.6 Software Engineering with Inheritance (cont.)

• When we use inheritance to create a new class


from an existing one, the new class inherits the
data members and member functions of the
existing class.
• We can customize the new class to meet our
needs by redefining base-class members and
by including additional members.
• The derived-class programmer does this in C++
without accessing the base class’s source code
(the derived class must be able to link to the
base class’s object code).

©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.


11.7 Software Engineering with Inheritance (cont.)

• Software developers can develop proprietary


classes for sale or license.
• Users then can derive new classes from these
library classes rapidly and without accessing
the proprietary source code.
• The software developers need to supply the
headers along with the object code
• The availability of substantial and useful class
libraries delivers the maximum benefits of
software reuse through inheritance.
©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.

You might also like