Chapter 4 INHERITANCE - PowerPoint PPT Presentation

1 / 64
About This Presentation
Title:

Chapter 4 INHERITANCE

Description:

Direct base class for Coupe. Direct derived calss from Car. Indirect derived class from Vehicle ... Coupe. 4.1 INTRODUCTION(Cont. ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 65
Provided by: ccNct
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4 INHERITANCE


1
Chapter 4 INHERITANCE
2
4.1 INTRODUCTION
  • Suppose that a class Student has already been
    designed and implemented and that a new
    application needs a class GradStudent. Since a
    GradStudent is a Student, rather than build the
    class GradStudent from scrath, we can add to the
    class Student whatever data and methods it needs
    to become a GradStudent.

3
4.1 INTRODUCTION(Cont.)
  • The resulting class GradStudent is said to be
    inherited from the class Student.
  • The new class GradStudent is called a derived
    class or a subclass of Student.
  • The orginal class Student is called the base
    class or superclass of GradStudent.

4
4.1 INTRODUCTION(Cont.)
  • The derived class GradStudent inherits all of the
    data and methods from the existing class
    Student(except for constructors, the destructor,
    and an overload of the assignment operator).
  • A derived class thus has all of the data and
    method (with the exceptions noted) of its base
    class, in addition to added data and methods.

5
FIGURE 4.1.1
  • Class GradStudent is derived from class Student.

6
4.1 INTRODUCTION(Cont.)
  • Inheritance promotes code reuse because the code
    in the base class is inherited by the subclass
    and thus need not be rewitten.
  • If the code were rewritten, bugs might
    inadvertently be introduced.
  • Inheritance also provides a mechanism to express
    the natural relationships among the components of
    a program.

7
4.1 INTRODUCTION(Cont.)
  • Inheritance is required for polymorphism in which
    the particular method to invoke depends on the
    class to which the object belongs, but the class
    to which the object belongs is not known until
    the program is executing.(examined in CH5)
  • Besides the is a relationship present in
    inheritance (e.g., a GradStudent is a Student),
    we can think of a derived class as a specialized
    version of the base class.

8
FIGURE 4.1.2
  • Class CPen is derived from class Pen.

9
4.1 INTRODUCTION(Cont.)
  • A derived class can itself serve as a base class
    for another class.
  • Notice that as we move down the hierarchy, each
    class is a specialized version of its base class.

Direct base class for Car Indirect base class for
Coupe Direct derived class from Vehicle Direct
base class for Coupe Direct derived calss from
Car Indirect derived class from Vehicle
10
4.1 INTRODUCTION(Cont.)
  • C also supports multiple inheritance, in which
    a derived class can have multiple base classes.
    (also be discussed in 4.7)
  • Class directly support the creation of abstract
    data types. Inheritance extends that support by
    promoting the derivation of new abstract data
    types from already existing ones.
  • Object-oriented languages thus provide
    programmers with the tools for programming with
    abstract data types.

11
4.2 BASIC CONCEPTS AND SYNTAX
  • To derive class DC from class BC, we write
  • //BC is the base class DC is the derived class
  • class DC public BC
  • //
  • Except for public BC the preceding
    declaration looks like an ordinary class
    declaration.

12
4.2 BASIC CONCEPTS AND SYNTAX(Cont.)
  • The C Postscript section at the end of this
    chapter discusses the other types of inheritance,
    in which the keyword public is omitted or
    replaced by either private or protected.
  • // BC is the base class DC is the derived class
  • Class DC BC // Caution default
    inheritance is private
  • //
  • If public is omitted the inheritance defaults to
    private.

13
4.2 BASIC CONCEPTS AND SYNTAX(Cont.)
  • Example 4.2.1
  • class Pen
  • public
  • enum ink Off, On
  • void set_status ink
  • void set_location( int, int )
  • Private
  • int x
  • int y
  • ink status

p.set_status( PenOn ) p.set_status( PenOff
) p.set_location( x, y )
14
4.2 BASIC CONCEPTS AND SYNTAX(Cont.)
  • Suppose that our hardware is upgraded so that we
    have a colored pen. Rather than declare a brand
    new class to describe the colored pen, we can
    derive a colored pen class from the pen class
  • class Cpen public Pen
  • public
  • void set_color( int )
  • private
  • int color

15
FIGURE 4.2.1
  • Pen CPen

X Y Status Set_status Set_location
X Y Status Set_status Set_location Color Set_color
From Pen
Base class
Derived class
16
4.2 BASIC CONCEPTS AND SYNTAX(Cont.)
  • private Members in Inheritance
  • Each private member in a base class is visible
    only in the base class.
  • A private member of a base class is inherited by
    the derived class, but it is not visible in the
    derived class.
  • Although a private member of a base class cannot
    be directly accessed in a derived class, it might
    be indirectly accessed through a derived method.

17
Example 4.2.2
  • class Point
  • public
  • void set_x( int x1 ) x x1
  • void set_y( int y1 ) y y1
  • int get_x() const return x
  • int get_y() const return y
  • private
  • int x
  • int y

class Intense_point public Point public
void set_intensity( int i ) intensity I
int get_intensity() const return intensity
private int intensity
18
Example 4.2.2 (Cont.)
19
4.2 BASIC CONCEPTS AND SYNTAX(Cont.)
  • Adjusting Access
  • The access status of an inherited member can be
    changed with a using declaration.
  • See example 4.2.3

20
EXAMPLE 4.2.3
  • class BC // base class
  • public
  • void set_x( float a ) x a
  • private
  • float x
  • class DC public BC // derived class
  • public
  • void set_y( float ) y b
  • private
  • flaot y
  • using BCset_x

int main() DC d d.set_y( 4.31 ) //OK
d.set_x( -8.03 ) // ERROR //
set_x is private in DC
21
4.2 BASIC CONCEPTS AND SYNTAX(Cont.)
  • Name Hiding
  • If a derived class adds a data member with the
    same name as a data member in the base class ,
    the local data member hides the inherited data
    member.
  • Similarly, if a derived class adds a method with
    the same name in the base class, the added method
    hides the base classs method.

22
EXAMPLE 4.2.4
  • class BC // base class
  • public
  • void h( float ) // BCh
  • class DC public BC // derived class
  • public
  • void h( char ) // DANGER hides
    BCh
  • int main()
  • DC d1
  • d1.h( Boffo! ) //DCh, not BCh
  • d1.h( 707.7 ) // ERROR DCh
    hides BCh
  • d1.BCh( 707.7 ) //OK invokes BCh
  • //

23
4.2 BASIC CONCEPTS AND SYNTAX(Cont.)
  • Indirect Inheritance
  • Data members and methods may traverse several
    inheritance links as they are included from a
    base to a derived class.
  • Inheritance thus may be either direct (to a
    derived class from a direct base class) or
    indirect (to a derived class from an indirect
    base class).

24
EXAMPLE 4.2.5
  • // direct base class for Cat,
  • //indirect base class for HouseCat
  • class Animal
  • public
  • string species
  • float lifeExpectancy
  • bool warmBlooded_P
  • // direct derived class from Animal
  • // direct base class for HouseCat
  • class Cat public Animal
  • public
  • string range 100
  • float favoritePrey 100 100

// indirect derived class from Animal // direct
derived class from Cat class HouseCat public
Cat public string toys 1000
string catPsychiatrist string catDentist
string catDoctor string apparentOwner
25
4.3 SAMPLE APPLICATION TRACKING FILMS
  • Problem
  • Sample Output
  • Solution
  • C Implementation
  • Discussion
  • Program Development

26
4.4 protected MEMBERS
  • Becides private and public members, C provides
    protected members.
  • In the absence of inheritance, a protected member
    is just like a private member it is visible only
    within the class.
  • In public inheritance, a protected member differs
    from a private member in that a protected member
    in the base class is protected in the derived
    class.
  • Thus when a derived class inherits a protected
    member from a base class, that protected member
    is visible in the derived class.

27
EXAMPLE 4.4.1
  • class BC
  • public
  • void set_x( int a ) x a
  • protected
  • int get_x() const return x
  • private
  • int x
  • class DC public BC
  • public
  • void add2() int c get_x() set_x( c2
    )

28
EXAMPLE 4.4.1(Cont.)
  • int main()
  • DC d
  • d.set_x( 3 ) //OK set_x is public in DC
  • // ERROR get_x is protected in DC
  • cout ltlt d.get_x() ltlt \n
  • d.x 77 // ERROR x is private in BC
  • d.add2() // OK add2 is public in DC

class DC public BC // // ERROR x
is accessible only within BC void add3() x
3 //
29
4.4 protected MEMBERS(Cont.)
  • A derived class may access protected members that
    it inherits from a base class.
  • In other words , these protected members are
    visible in the derived class.
  • Yet a derived class may not access protected
    members of a base class object, that is , an
    object that belongs to the base class but not to
    the derived class.

30
EXAMPLE 4.4.2
  • Class BC
  • protected
  • int get_w() const
  • //
  • Class DC public BC
  • public
  • // get_w belongs to DC because it is
    inherited from BC
  • void get_val() const return get_w()
  • // ERROR b.get_w not visible in DC
    since b.get_w is a
  • //member of BC , not DC
  • void base_w( const BC b ) const coutltlt
    b.get_w() ltlt\n

31
4.4 protected MEMBERS(Cont.)
  • A private member is inherited but not visible in
    derived class.
  • Except for a friend function(see Section 6.4),
    only method of a class can access a private
    member of that class.
  • A protected member is inherited and visible in a
    derived class. Thus a protected member can be
    visible throughout the class hierarchy.
  • Except for a friend function, only methods within
    the class hierarchy can access a protected
    member.
  • By making a member of a class public, we make the
    member visible wherever the class is visible.

32
EXAMPLE 4.4.3
BC
Public Protected private
Init_x Get_x x
  • class BC
  • public
  • void init_x() x 0
  • protected
  • int get_x() const return x
  • private
  • int x
  • class DC
  • public
  • void g() init_x() coutltlt get_x() ltlt
    \n

DC
Public Protected Public Private to B
Init_x Get_x G
x
33
4.4 protected MEMBERS(Cont.)
  • In general, protected data should be avoided.
  • Data that might be protected can usually be made
    private and made accessible by protected
    accessors.
  • More complex data members are sometimes better
    declared protected instead of supplying
    complicated accessors.
  • Using private data members promotes data hiding.
  • A class with private data members and protected
    accessors can be completely reimplemented without
    disturbing the interface.

34
EXAMPLE 4.4.4
  • Class BC
  • //
  • protected
  • int y
  • Can be better

Class BC // protected int
get_y() const return y void set_y( int
a ) y a private int y
35
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • Constructors Under Inheritance
  • An object in a derived class inherits
    characteristics from the base class but also has
    characteristics that are specific to the derived
    class.

36
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • A base class constructor ( if any ) is invoked
    when a derived class object is created.
  • The base class constructor handles initialization
    and other matters for the from the base class
    part of the object.
  • If the derived class has a constructor of its own
    , this constructor can handle the added by
    derived class part of the object.

37
EXAMPLE 4.5.1
  • class BC // base class
  • public
  • BC() x y -1 // base constructor
  • protected
  • int get_x() const return x
  • int get_y() const return y
  • private
  • int x
  • int y
  • class DC public BC //derived class
  • public
  • void write() const cout ltlt get_x()
    get_y() ltlt \n

Int main() //d1.BC() invoked DC
d1 //1 written to standard output
d1.write() //
38
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • Base class constructors are often sufficient for
    the derived class.
  • Sometimes, however, it makes sense for derived
    class to have its own constructors.
  • A constructor specific to a derived class may
    invoke a base class constructor, if one exits.

39
EXAMPLE 4.5.2
  • class Animal
  • public
  • Animal() species Animal
  • Animal( const char s ) species s
  • private
  • string species
  • class Primate public Animal
  • public
  • Primate() Animal( Primate )
  • Primate( int n ) Animal( Primate )
    heart_cham n
  • private
  • int heart_cham
  • private

40
EXAMPLE 4.5.2(Cont.)
  • Animal slug //
    Animal()
  • Animal tweety( canary ) // Animal(
    const char )
  • Primate godzilla //
    Primate()
  • Primate human( 4 ) //
    Primate( int )
  • The two Primate constructors invoke a base class
  • Animal constructor in their initialization
    section
  • Primate() Animal( Primate )
  • Primate( int n ) Animal( Primate )
  • heart_cham n

41
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • In a deep inheritance hierarchy, creation of an
    object belonging to a derived class may have a
    domino effect with respect to constructor
    invocation.

42
EXAMPLE 4.5.3
class Human public Primate public
Human() Primate() Human( int c )
Primate( c )
  • Class Animal
  • public
  • Animal() species Animal
  • Animal( const char s ) species s
  • private
  • string species
  • Class Primate public Animal
  • public
  • Primate() Animal( Primate )
  • Primate( int n ) Animal( Primate )
  • heart_cham n
  • private
  • int heart_cham

Human jill() // Human() Human fred( 4 )
// Human( int )
43
FIGURE 4.5.3
  • Animal Animal ( ) body executes first
  • Primate Primate ( ) body executes second
  • Human Human ( ) body executes third

44
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • Derived Class Constructor Rules
  • If a base class has constructors but no default
    constructor, then a derived class constructor
    must explicitly invoke some base class
    constructor.
  • See Example 4.5.4

45
EXAMPLE 4.5.4
46
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • Suppose that a base class has a default
    constructor and that a derived class has
    constructors, none of which explicitly invoke any
    base class constructor.
  • In this case, the base class default constructor
    is invoked automatically whenever a derived class
    object is created.

47
EXAMPLE 4.5.5
48
EXAMPLE 4.5.5(Cont.)
  • If DC has constructors but BC has no
    constructors, then the appropriate DC constructor
    executes automatically whenever a DC object is
    created.
  • If DC has no constructors but BC has
    constructors, then BC must have a default
    constructor so that BCs default constructor can
    execute automatically whenever a DC object is
    created.
  • If DC has constructors and BC ahs a default
    constructor, then BCs default constructor
    executes automatically whenever a DC object is
    created unless the appropriate DC constructor
    explicitly invokes, in its initialization
    section,som other BC constructor.
  • If DC and BC have constructors but BC has no
    default constructor, then each DC constructor
    must explicitly invoke, in its initialization
    section, a BC constructor, which then executes
    when a DC object is created.

49
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • It makes sense that the creation of a derived
    class object should cause some base class
    constructor, if any , to execute.
  • A derived class constructor may depend upon
    actions from a base class constructor.
  • A derived class object is a specialization of a
    base class object, which means that the body of a
    base class constructor, if the class has
    constructors, should execute first when a
    derived class object is created. The body of a
    more specialized constructor, which is the local
    derived class constructor, then can handle any
    special details.

50
EXAMPLE 4.5.6
51
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • Destructors Under Inheritance
  • Constructor bodies in an inheritance hierarchy
    execute in a
  • Base class to derived class
  • order. Destructor bodies in an inheritance
    hierarchy execute in a
  • Derived class to base class
  • order. So the destructor bodies execute in the
    reverse order of the constructor bodies.

52
EXAMPLE 4.5.7
53
4.5 CONSTRUCTORS AND DESTRUCTORS UNDER INHERITANCE
  • A destructor often frees storage allocated by a
    constructor.
  • By firing in the reverse order of constructors,
    destructors ensure that the most recently
    allocated storage is the first storage to be
    freed.
  • Unlike constructors, a destructor never explicity
    invokes another destructor. Since each class has
    at most one destructor, it is unambiguous which
    destructor, if any , should execute.

54
EXAMPLE 4.5.8
55
4.6 SAMPLE APPLICATIONA SEQUENCE HIERARCHY
  • Problem
  • Sample Output
  • Solution
  • C Implementation
  • Discussion
  • Program Development

56
4.7 MULTIPLE INHERITANCE
  • In single inheritance, a derived class has a
    single base class. In multiple inheritance, a
    derived class has multiple base classes.
  • In technical jargon, a hierarchy with only single
    inheritance is a tree, whereas a hierarchy with
    multiple inheritance is a graph.

BC
Single inheritance
DC
BC1
BC2
Multiple inheritance
DC
57
4.7 MULTIPLE INHERITANCE(Cont.)
  • In a single inheritance hierarchy, a derived
    class typically represents a specialization of
    its base class.
  • Because classes are user-defined data types in
    C, such a derived class is a specialization or
    refinement of the more general data type that its
    base class represents.
  • In a multiple inheritance hierarchy, a derived
    class typically represents a combination of its
    base class.

58
EXAMPLE 4.7.1
59
EXAMPLE 4.7.2
60
4.7 MULTIPLE INHERITANCE(Cont.)
  • Inheritance and Access
  • The rules of inheritance and access do not change
    from a single to a multiple inheritance
    hierarchy. A derived class inherits data members
    from all its base classes.
  • Multiple inheritance increases opportunities for
    name conflicts. The conflict can occur between
    the derived class and one of the base classes or
    between the base classes themselves. It is up to
    the programmer to resolve the conflict.

61
EXAMPLE 4.7.3
62
4.7 MULTIPLE INHERITANCE(Cont.)
  • Virtual Base Classes
  • Multiple inheritance hierarchies can be complex,
    which may lead to the situation in which a
    derived class inherit multiple times from the
    same indirect base class.

63
EXAMPLE 4.7.4
BC
DC1
DC2
z
64
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com