0% found this document useful (0 votes)
154 views

Polymorphism PolymorphicAssignment

1. Polymorphic assignment allows a pointer of the superclass type to point to objects of its concrete subclasses. 2. When the calculateFee() method in the Registration superclass is declared virtual, it can be overridden in subclasses StudentRegistration and GuestRegistration. 3. Polymorphism allows the same function call to return different results depending on the actual object type pointed to by the superclass pointer, due to the execution of different overridden functions in subclasses.

Uploaded by

Rian
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

Polymorphism PolymorphicAssignment

1. Polymorphic assignment allows a pointer of the superclass type to point to objects of its concrete subclasses. 2. When the calculateFee() method in the Registration superclass is declared virtual, it can be overridden in subclasses StudentRegistration and GuestRegistration. 3. Polymorphism allows the same function call to return different results depending on the actual object type pointed to by the superclass pointer, due to the execution of different overridden functions in subclasses.

Uploaded by

Rian
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Polymorphic Assignment

&
Polymorphism

Registration class hierarchy

Valid Assignment Statements

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");

Polymorphic Assignment
Polymorphic
assignment
allows
a
pointer of type superclass to point to
objects of its concrete (not abstract)
subclasses.

Polymorphic Assignment: Examples

Registration *r3 = new StudentRegistration("L


Kgomotso", "TUT", "B-Tech");

Registration *r4 = new GuestRegistration("M Cloete",


"Wits", "Presentation Judge");

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");

Polymorphic Assignment: Another Example


void doSomething(Registration* r){
//do something with the object pointed to by r
}
doSomething(new Registration("G Smit", "UP"));
doSomething(new StudentRegistration("T Moyo",
"UNISA", "MSc Computing"));
doSomething(new GuestRegistration("K John", "UCT",
"Keynote Speaker"));

Polymorphism
Polymorphism can only be utilized when a virtual
function of the superclass is overridden in its
subclass(es).

Define and Implement: calculateFee()


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;}

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

void displayConferenceFee(Registration* r){


cout << r->calculateFee();
}

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!

You might also like