Polymorphism PolymorphicAssignment
Polymorphism PolymorphicAssignment
&
Polymorphism
Polymorphic Assignment
Polymorphic
assignment
allows
a
pointer of type superclass to point to
objects of its concrete (not abstract)
subclasses.
To reiterate
All these assignment statements are correct for the given class
hierarchy:
Registration *r1 = new Registration("G Smit", "UP");
StudentRegistration *r2 = new StudentRegistration("T
Moyo", "UNISA", "MSc Computing");
GuestRegistration *r3 = new GuestRegistration("K John",
"UCT", "Keynote Speaker");
Registration *r3 = new StudentRegistration("L
Kgomotso", "TUT", "B-Tech");
Registration *r4 = new GuestRegistration("M Cloete",
"Wits", "Presentation Judge");
Polymorphism
Polymorphism can only be utilized when a virtual
function of the superclass is overridden in its
subclass(es).
Definition
(in .h file)
Implementation
(in .cpp file)
Registration
virtual double
calculateFee() const;
double Registration::
calculateFee() const
{ return STANDARD_FEE;}
StudentRegistration
double calculateFee()
const;
double
StudentRegistration::
calculateFee() const
{ return
Registration::calculate
Fee()* 50/100;}
GuestRegistration
double calculateFee()
const;
double
GuestRegistration::
calculateFee() const
{ return
Registration::calculate
Fee()* 10/100;}
Polymorphism
A function invocation on a pointer of
type superclass can give different
results based on the object pointed to
by the pointer
The different results is due to the
execution of different overridden
functions in the subclasses
Polymorphism: Example
Assume that the data member STANDARD_FEE in Registration
is initialized to 1000.00.
void displayConferenceFee(Registration* r){
cout << r->calculateFee();
}
displayConferenceFee(new StudentRegistration("T Moyo",
"UNISA", "MSc Computing"));
Output: 500.00
displayConferenceFee(new GuestRegistration("K John",
"UCT", "Keynote Speaker"));
Output: 100.00
displayConferenceFee(new Registration("G Smit", "UP"));
Output: 1000.00
Summary
Pointer of
type
superclass
virtual in
Registration
It is overridden in
StudentRegistration
and
GuestRegistration
Without virtual
Class
Definition
(in .h file)
Implementation
(in .cpp file)
Registration
virtual double
calculateFee() const;
double Registration::
calculateFee() const
{ return STANDARD_FEE;}
StudentRegistration
double calculateFee()
const;
double
StudentRegistration::
calculateFee() const
{ return
Registration::calculate
Fee()* 50/100;}
GuestRegistration
double calculateFee()
const;
double
GuestRegistration::
calculateFee() const
{ return
Registration::calculate
Fee()* 10/100;}
Without virtual
Assume that the data member STANDARD_FEE in Registration
is initialized to 1000.00.
void displayConferenceFee(Registration* r){
cout << r->calculateFee();
}
displayConferenceFee(new StudentRegistration("T Moyo",
"UNISA", "MSc Computing"));
Output: 1000.00
displayConferenceFee(new GuestRegistration("K John",
"UCT", "Keynote Speaker"));
Output: 1000.00
displayConferenceFee(new Registration("G Smit", "UP"));
Output: 1000.00
Conclusion
Polymorphism
and
polymorphic
assignment are two great features of
object oriented (OO) programming.
Use them well in your OO programs!