Chapter 13 ObjectOriented Programming: Polymorphism - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Chapter 13 ObjectOriented Programming: Polymorphism

Description:

Which class's function to invoke. Normally ... An overridden function in a derived class has the same signature and return type ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 17
Provided by: del596
Category:

less

Transcript and Presenter's Notes

Title: Chapter 13 ObjectOriented Programming: Polymorphism


1
Chapter 13Object-Oriented Programming
Polymorphism
  • C, How to Program
  • Deitel Deitel

2
Relationships Among Objects in an Inheritance
Hierarchy
  • Key concept
  • An object of a derived class can be treated as an
    object of its base class
  • is-a relationship A object is a B object.

3
Which Function Is Invoked with Pointer?
  • Invoked functionality depends on type of the
    handle (type of the pointer) used to invoke the
    function, not type of the object to which the
    handle points

4
Example
  • BankAccount ba(12,John,200.00)
  • BankAccount baPtr 0
  • Base-class pointer points to base-class object
  • Invoke base-class functionality
  • baPtr ba baPtr-gtprint( )

5
Example
  • SavingsAccount sa(13, Tom, 300.00)
  • SavingsAccount saPtr0
  • Derived-class pointer points to derived-class
    object
  • Invoke derived-class functionality
  • saPtr sa saPtr-gtprint( )

6
Invoking Base-Class Functions from Derived-Class
Objects
  • Base-class pointer points to derived-class object
    because derived-class object is an object of base
    class
  • Invoke base-class functionality because the type
    of pointer is Base-class
  • baPtr sa baPtr-gtprint( )

7
Aiming Derived-Class Pointers at Base-Class
Objects
  • Derived-class pointer points to a base-class
    object
  • Example saPtr ba ( Wrong!!!)
  • C compiler generates error
  • BankAccount (base-class object) is not a
    SavingsAccount (derived-class object)

BankAccount ba
BankAccount baPtr
12, John 200.00
1024
1024
13, Tom 300.00, 0.00
1048
1024
SavingsAccount sa
SavingsAccount saPtr
8
Derived-Class Member-Function Calls via
Base-Class Pointers
  • Base-class pointer points to derived-class object
  • Calling functions that exist in base class causes
    base-class functionality to be invoked
  • baPtr-gtwithdraw(30)
  • Calling functions that do not exist in base class
    (may exist in derived class) will result in
    error
  • baPtr-gtgetInterest( ) (Wrong!!! )
  • Derived-class members cannot be accessed from
    base-class pointers
  • However, they can be accomplished using
    downcasting (Section 13.8)

9
virtual Functions
  • Which classs function to invoke
  • Normally
  • Handle determines which classs functionality to
    invoke
  • With virtual functions
  • Type of the object being pointed to, not type of
    the handle, determines which version of a virtual
    function to invoke
  • Allows program to dynamically (at runtime rather
    than compile time) determine which function to
    use
  • Called dynamic binding or late binding

10
virtual Functions
  • Declared by preceding the functions prototype
    with the keyword virtual in base class
  • Derived classes override function as appropriate
  • An overridden function in a derived class has the
    same signature and return type (i.e. prototype)
    as the function it overrides in its base class.
  • virtual -gtoverride
  • non-virtual -gt redefine

11
Example
  • BankAccount.h
  • virtual void print( )
  • //print info. about bank account
  • SavingsAccount.h
  • virtual void print( )
  • //print info. about savings account
  • No change in .cpp files.

12
virtual Functions in Inheritance Hierarchy
  • virtual function is overridden.
  • Once virtual, always virtual (even not explicitly
    declared).
  • virtual function is not overridden.
  • Inherits from its base class

13
Static Binding/Dynamic Binding
  • Static binding
  • When calling a virtual function using specific
    object with dot operator, function invocation
    resolved at compile time
  • Dynamic binding
  • Choosing the appropriate function to call at
    execution time
  • Dynamic binding occurs only off pointer

14
Polymorphism with inheritance hierarchies
  • Program in the general vs. program in the
    specific
  • Process objects of classes that are part of the
    same hierarchy as if they are all objects of the
    base class
  • Each object performs the correct tasks for that
    objects type
  • Different actions occur depending on the type of
    object

15
Summary
16
Reference
  • Reproduced from the Cyber Classroom for C, How
    to Program, 5/e by Deitel Deitel.
  • Reproduced by permission of Pearson Education,
    Inc.
Write a Comment
User Comments (0)
About PowerShow.com