04 IntroToC Part 3 Inheritance

  • Upload
    -

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    1/25

    2/3/11

    1

    Object-Oriented Programming:

    Inheritance

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    12.1 Introduction

    Inheritance is a form of software reuse in which you create aclass that absorbs an existing classs data and behaviors andenhances them with new capabilities.

    You can designate that the new class should inherit themembers of an existing class.

    This existing class is called thebase class, and the new classis referred to as the derived class.

    A derived class represents a more specialized group ofobjects.

    A derived class contains behaviors inherited from its baseclass and can contain additional behaviors.

    A derived class can also customize behaviors inherited fromthe base class.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    2/25

    2/3/11

    2

    12.1 Introduction (cont.)

    With object-oriented programming, you focus onthe commonalities among objects in the systemrather than on the special cases.

    We distinguish between the is-a relationship andthe has-a relationship.

    The is-a relationship represents inheritance. In an is-a relationship, an object of a derived classalso can be treated as an object of its base class. By contrast, the has-a relationship represents

    composition.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    3/25

    2/3/11

    3

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    4/25

    2/3/11

    4

    12.3 protected Members

    A base classs protected members can be accessed within the bodyof that base class, by members and friends of that base class, and bymembers and friends of any classes derived from that base class.

    Derived-class member functions can refer to public andprotected members of the base class simply by using the membernames.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    5/25

    2/3/11

    5

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    CommissionEmployee

    BasePlusCommissionEmployee

    Earningby

    commission

    oversales

    made.

    Earningbya

    basesalary

    plus

    commission

    oversales

    made.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    6/25

    2/3/11

    6

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    7/25

    2/3/11

    7

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    8/25

    2/3/11

    8

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    9/25

    2/3/11

    9

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    10/25

    2/3/11

    10

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    12.4.4 CommissionEmployeeBasePlusCommissionEmployee Inheritance

    Hierarchy Using protected Data (cont.)

    Using protected data members creates two seriousproblems. The derived-class object does not have to use a member function to

    set the value of the base classs protected data member. Derived-class member functions are more likely to be written so

    that they depend on the base-class implementation. Derived classesshould 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 modifyall derived classes of that base class.

    Such software is said to be fragile orbrittle, because a smallchange in the base class can break derived-classimplementation.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    11/25

    2/3/11

    11

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    12/25

    2/3/11

    12

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    13/25

    2/3/11

    13

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    14/25

    2/3/11

    14

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    15/25

    2/3/11

    15

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    16/25

    2/3/11

    16

    12.5 Constructors and Destructors inDerived Classes

    Instantiating a derived-class object begins a chain of constructorcalls in which the derived-class constructor, before performing itsown tasks, invokes its direct base classs constructor eitherexplicitly (via a base-class member initializer) or implicitly(calling the base classs default constructor).

    If the base class is derived from another class, the base-classconstructor is required to invoke the constructor of the next classup in the hierarchy, and so on.

    The last constructor called in this chain is the constructor of theclass at the base of the hierarchy, whose body actually finishesexecuting first.

    The original derived-class constructors body finishes executinglast.

    Each base-class constructor initializes the base-class datamembers that the derived-class object inherits.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    17/25

    2/3/11

    17

    12.5 Constructors and Destructors inDerived Classes (cont.)

    When a derived-class object is destroyed, the program callsthat objects destructor.

    This begins a chain (or cascade) of destructor calls in whichthe derived-class destructor and the destructors of the directand indirect base classes and the classes members executein reverse of the order in which the constructors executed.

    When a derived-class objects destructor is called, thedestructor performs its task, then invokes the destructor ofthe next base class up the hierarchy.

    This process repeats until the destructor of the final baseclass at the top of the hierarchy is called.

    Then the object is removed from memory.1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    Demo

    CtorDtorInHierarchy.cpp

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    18/25

    2/3/11

    18

    12.5 Constructors and Destructors inDerived Classes (cont.)

    Base-class constructors, destructors and overloadedassignment operators are not inherited by derived classes.

    Derived-class constructors, destructors and overloadedassignment operators can call base-class constructors,destructors and overloaded assignment operators.

    Our next example defines class Commission-Employeeand class BasePlusCommissionEmployee withconstructors and destructors that each print a message wheninvoked. These messages demonstrate the order in which the constructors

    and destructors are called for objects in an inheritance hierarchy.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    19/25

    2/3/11

    19

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    20/25

    2/3/11

    20

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    21/25

    2/3/11

    21

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    22/25

    2/3/11

    22

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    23/25

    2/3/11

    23

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    24/25

    2/3/11

    24

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.

  • 7/28/2019 04 IntroToC Part 3 Inheritance

    25/25

    2/3/11

    25

    1992-2010 by Pearson Education, Inc.

    All Rights Reserved.