Polymorphism Lecture Notes
Polymorphism Lecture Notes
poly morphism
Compile-time/early binding/static binding
Runtime /late binding /dynamic binding
Runtime polymorphism
being able to assign different meaning to the same function at
runtime
Checking c
Savings Checking
c. withdraw() //Checking : : withdraw
Withdraw() Withdraw()
Trust d
d. withdraw() //Trust : : withdraw Trust
Withdraw()
Trust d;
display_account(d);
Virtual Function
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();
...
}
Virtual Functions
Concrete Classes
Classes used to instantiate objects
Must provide implementation for every member
function they define
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
Example
virtual void draw() const = 0;