Topic05 Polymorphism
Topic05 Polymorphism
Topic 06
Polymorphism, Dynamic
Binding, Abstract Classes
2
What is Polymorphism?
• The Greek meaning of the words "poly" and
"morph" together imply that "a single entity can
take on multiple forms"
• We will see
– How a base-class pointer can be used to point to a
derived class object
– How such a pointer can be used to invoke the base
class member functions that are in fact members of the
derived class
5
class Shape {
public:
Shape (int = 0, int = 0 ); // default constructor
void setX(int); // set x in coordinate pair
int getX() const; // return x from coordinate pair
private:
int x; // x part of coordinate pair
int y; // y part of coordinate pair
};
6
// shape.cpp – implementation of Shape
#include <iostream>
using std::cout;
#include “shape.h" // Shape class definition
// constructor
Shape::Shape( int xValue, int yValue) {
x = xValue;
y = yValue ;
}
Class Circle
// circle.h
#include “shape.h" // Shape class definition
private:
double radius;
};
9
// circle.cpp
#include <iostream>
using std::cout;
#include "circle.h" // Circle class definition
// Constructor
Circle::Circle( int xValue, int yValue, double radiusValue ) : Shape( xValue, yValue )
{
radius = radiusValue;
}
// set radius
void Circle::setRadius( double radiusValue )
{
radius = radiusValue;
}
// return radius
double Circle::getRadius() const
{
return radius;
}
10
int main()
{
Shape* shape = NULL; // You can also use 0
Circle* circle = NULL; // derived-class pointer
shape = circle;
shape->print(); // Which one is invoked?
return 0;
}
12
Static Binding
• Static binding means that the pointer can only
invoke the member functions of the class used
in its definition
– Although the same pointer can point to an object of
another class (a derived class)
14
Dynamic Binding
• Dynamic binding occurs when the decision regarding
which member function to execute is delayed till run-
time
– Depending on the object on which the member
function is invoked
class Shape {
public:
Shape (int = 0, int = 0 ); // default constructor
void setX(int); // set x in coordinate pair
int getX() const; // return x from coordinate pair
private:
int x; // x part of coordinate pair
int y; // y part of coordinate pair
};
16
Class Circle
// circle.h
#include “shape.h" // Shape class definition
private:
double radius;
};
17
int main()
{
Shape* shape = NULL; // You can also use 0
Circle* circle = NULL; // derived-class pointer
shape = circle;
shape->print(); // print of Circle will be invoked?
return 0;
};
18
Example
• Consider this member function
class A {
…
void refresh(Shape *);
….
};
void A::refresh(Shape *s) {
s->draw();
}
• Consider the following code. We only only had to define refresh once using
Shape and pass to it an object Rectangle, and then an object Circle.
Important Rule
• When a member function is declared virtual, it
is virtual in all derived classes
Virtual Destructors
• Consider a derived class object that is dynamically
allocated to a base class pointer
– Deleting such an object by applying “delete” operator to the
base class pointer will only invoke the destructor of the base
class
Abstract Classes
• An abstract class, also called abstract base
class, is a class that is meant to be a base
class for other classes for design quality
raisons
– We cannot instantiate them
protected:
int id; // employee id
char* name; // name of the employee
char* address; // address of the employee
char* phone; // telephone number, e.g., 514-234-5454
char* email; // email address
int vacation; // days of vacation
public:
Employee(); // default constructor
Employee (int, const char* ); // regular constructor, used to set id and name
virtual ˜Employee(); // virtual destructor
// Same as before
virtual void Manager::print() const {
Employee::print();
cout << salary << endl;
}
Example of an
Inheritance
Hierarchy
30
Discussion
• We will discuss the following questions using
the board
– What classes should be abstract classes?
– What member functions should be abstract, i.e.,
pure virtual?
– What member functions should be virtual?