polymorphismcs-2303, c-term 20101 polymorphism hugh c. lauer adjunct professor (slides include...
Post on 22-Dec-2015
213 views
TRANSCRIPT
![Page 1: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/1.jpg)
PolymorphismCS-2303, C-Term 2010 1
Polymorphism
Hugh C. LauerAdjunct Professor
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
![Page 2: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/2.jpg)
PolymorphismCS-2303, C-Term 2010 2
Review — Accessing Members ofBase and Derived Classes
class B {
public:void m();void n();...
} // class B
class D: public B {
publicvoid m();void p();...
} // class D
• The following are legal:–B_obj.m() //B’s m()B_obj.n()
D_obj.m() //D’s m()D_obj.n() //B’s n()D_obj.p()
B_ptr->m() //B’s m()B_ptr->n()
D_ptr->m() //D’s m()D_ptr->n() //B’s n()D_ptr->p()
![Page 3: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/3.jpg)
PolymorphismCS-2303, C-Term 2010 3
Review — Accessing Members ofBase and Derived Classes (continued)
class B {
public:void m();void n();...
} // class B
class D: public B {
publicvoid m();void p();...
} // class D
• The following are legal:–B_ptr = D_ptr;
• The following are not legal:–D_ptr = B_ptr;B_ptr->p();
Even if B_ptr is known to point to an object of class D
Class D redefines method m()
![Page 4: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/4.jpg)
PolymorphismCS-2303, C-Term 2010 4
Review — Accessing Members ofBase and Derived Classes (continued)
• Access to members of a class object is determined by the type of the handle.
• Definition: Handle• The thing by which the members of an object are
accessed
• May be– An object name (i.e., variable, etc.)
– A reference to an object
– A pointer to an object
Deitel & Deitel, §20.3
Note: this is not exactly the samething as handle classes mentionedby Stroustrup
![Page 5: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/5.jpg)
PolymorphismCS-2303, C-Term 2010 5
Review — Accessing Members ofBase and Derived Classes (continued)
• This is referred to as static binding
• I.e., the binding between handles and members is determined at compile time
• I.e., statically
![Page 6: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/6.jpg)
PolymorphismCS-2303, C-Term 2010 6
What if we need Dynamic Binding?
• I.e., what if we need a class in which access to methods is determined at run time by the type of the object, not the type of the handle
class Shape {public:
void Rotate();void Draw();...
}
class Rectangle: public Shape {
public:void Rotate();void Draw();...
}
class Ellipse: public Shape {
public:void Rotate();void Draw();...
}
Need to access the method to
draw the right kind of object
Regardless of handle!
![Page 7: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/7.jpg)
PolymorphismCS-2303, C-Term 2010 7
Solution – Virtual Functions
• Define a method as virtual, and the subclass method overrides the base class method
• E.g., class Shape {
public:virtual void Rotate();virtual void Draw();...
}
This tells the compiler to addinternal pointers to every objectof class Shape and its derivedclasses, so that pointers to correct methods can be storedwith each object.
![Page 8: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/8.jpg)
PolymorphismCS-2303, C-Term 2010 8
What if we need Dynamic Binding?
class Shape {public:
virtual void Rotate();virtual void Draw();...
}
• I.e., subclass methods override the base class methods– (if specified)
• C++ dynamically chooses the correct method for the class from which the object was instantiated.
class Rectangle: public Shape {
public:void Rotate();void Draw();...
}
class Ellipse: public Shape {public:
void Rotate();void Draw();...
}
![Page 9: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/9.jpg)
PolymorphismCS-2303, C-Term 2010 9
Notes on virtual
• If a method is declared virtual in a class, – … it is automatically virtual in all derived
classes
• It is a really, really good idea to make destructors virtual!
virtual ~Shape();
– Reason: to invoke the correct destructor, no matter how object is accessed
![Page 10: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/10.jpg)
PolymorphismCS-2303, C-Term 2010 10
Notes on virtual (continued)
• A derived class may optionally override a virtual function• If not, base class method is used
class Shape {public:
virtual void Rotate();virtual void Draw();...
}class Line: public Shape {public:
void Rotate();//Use base class Draw method
...}
![Page 11: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/11.jpg)
PolymorphismCS-2303, C-Term 2010 11
Example
• Deitel & Deitel, §24.3•CommissionEmployee and BasePlusCommissionEmployee
![Page 12: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/12.jpg)
PolymorphismCS-2303, C-Term 2010 12
Polymorphism
• The ability to declare functions/methods as virtual is one of the central elements of polymorphism in C++
• Polymorphism:– from the Greek “having multiple forms”
• In programming languages, the ability to assign a different meaning or usage to something in different contexts
![Page 13: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/13.jpg)
PolymorphismCS-2303, C-Term 2010 13
A Note from D&D on virtual
• When a virtual function is called by name of a specific object – using the dot member-selection operator (e.g., squareObject.draw() ),
• … the function invocation is resolved at compile time– This is static binding, not polymorphic behavior!
• Dynamic binding with virtual functions only occurs from pointer and reference handles
Have not been able to verify from any other source!
![Page 14: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/14.jpg)
PolymorphismCS-2303, C-Term 2010 14
Summary – Based and Derived Class Pointers
• Base-class pointer pointing to base-class object– Straightforward
• Derived-class pointer pointing to derived-class object– Straightforward
• Base-class pointer pointing to derived-class object– Safe– Can access non-virtual methods of only base-class– Can access virtual methods of derived class
• Derived-class pointer pointing to base-class object– Compilation error
![Page 15: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/15.jpg)
PolymorphismCS-2303, C-Term 2010 15
Questions?
![Page 16: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/16.jpg)
PolymorphismCS-2303, C-Term 2010 16
Abstract and Concrete Classes
• Abstract Classes– Classes from which it is never intended to instantiate any objects
• Incomplete—derived classes must define the “missing pieces”.• Too generic to define real objects.
– Normally used as base classes and called abstract base classes• Provide appropriate base class frameworks from which other classes
can inherit.
• Concrete Classes– Classes used to instantiate objects– Must provide implementation for every member function they
define
Definitions
![Page 17: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/17.jpg)
PolymorphismCS-2303, C-Term 2010 17
Pure virtual Functions
• A class is made abstract by declaring one or more of its virtual functions to be “pure”– I.e., by placing "= 0" in its declaration
• Examplevirtual void draw() const = 0;
– "= 0" is known as a pure specifier.– Tells compiler that there is no implementation.
![Page 18: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/18.jpg)
PolymorphismCS-2303, C-Term 2010 18
Pure virtual Functions (continued)
• Every concrete derived class must override all base-class pure virtual functions– with concrete implementations
• If even one pure virtual function is not overridden, the derived-class will also be abstract– Compiler will refuse to create any objects of the
class– Cannot call a constructor
![Page 19: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/19.jpg)
PolymorphismCS-2303, C-Term 2010 19
Purpose
• When it does not make sense for base class to have an implementation of a function
• Software design requires all concrete derived classes to implement the function
• Themselves
![Page 20: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/20.jpg)
PolymorphismCS-2303, C-Term 2010 20
Why Do we Want to do This?
• To define a common public interface for the various classes in a class hierarchy– Create framework for abstractions defined in
our software system
• The heart of object-oriented programming
• Simplifies a lot of big software systems• Enables code re-use in a major way• Readable, maintainable, adaptable code
![Page 21: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/21.jpg)
PolymorphismCS-2303, C-Term 2010 21
Abstract Classes and Pure virtual Functions
• Abstract base class can be used to declare pointers and references referring to objects of any derived concrete class
• Pointers and references used to manipulate derived-class objects polymorphically
• Polymorphism is particularly effective for implementing layered software systems – e.g.,
1. Reading or writing data from and to devices.2. Iterator classes to traverse all the objects in a container.
![Page 22: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/22.jpg)
PolymorphismCS-2303, C-Term 2010 22
Example – Graphical User Interfaces
• All objects on the screen are represented by derived classes from an abstract base class
• Common windowing functions• Redraw or refresh
• Highlight
• Respond to mouse entry, mouse clicks, user actions, etc.
• Every object has its own implementation of these functions
• Invoked polymorphically by windowing system
![Page 23: PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d805503460f94a64ddc/html5/thumbnails/23.jpg)
PolymorphismCS-2303, C-Term 2010 23
Questions?