CS300: Object Oriented Analysis and Design
Polymorphism
(Using C++)
Recap
Object Oriented Analysis and Design (CS 212)
Objects collaborate with other objects to achieve a task
How objects cooperate with one another define the boundaries of each abstraction and thus the responsibilities and protocol of each object.
Object Oriented Analysis and Design (CS 212)
Meaning of Modularity
Object Oriented Analysis and Design (CS 212)
Deciding on the right set of modules for a given problem is almost as hard a problem as deciding on the right set of abstractions.
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules
Object Oriented Analysis and Design (CS 212)
GRASP
Object Oriented Analysis and Design (CS 212)
Nine GRASP Principles
Principle | | Meaning |
Creator | Creation of objects | |
Information Expert | Delegation of responsibility | |
Low Coupling | Model View Controller Design | |
Controller | A non-user interface design | |
High Cohesion | Less Inter-class dependencies | |
Polymorphism | Variation in behavior | |
Indirection | Focused, manageable, understandable | |
Pure fabrication | Artificial class, Open-close principle | |
Protected variation | Avoid impact between objects | |
| |
Object Oriented Analysis and Design (CS 212)
Polymorphism in C++
Compile Time
Function overloading
Operator overloading
Runtime
Virtual Function
Not only does it provide support for compile-time polymorphism, it also adds flexibility and convenience.
Object Oriented Analysis and Design (CS 212)
Function overloading
A function name having several definitions that are differentiable by the number or types of their arguments.
OR
Function Overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby makes the program run faster.
For example;
float add (int a, int b);
float add (float x, float y);
Function signature/ Extended name
int myfunc(int i)
int myfunc(int i, float j)
void myfunc(int x, float y)
Overloading 🡪 Different signature
int add(int a, int b)
int add(int a, int b, int c)
double add(double x, double y)
double add(double p, int q)
Task of a compiler
C++ compiler model
Pre-processor
Compiler
Assembly code
Assembler
Object code
Linker
Executable code
Name Mangling/ Name Decoration
Name mangling in C++
Mangled name for g++ compiler
_Z N OriginalFunctionName parameter-names-encoded
N = Number Of Chars In Original Function Name
Original Name | Mangled Name |
void myFun(); | _Z5myFunv |
void myFun(int a, char c); | _Z5myFunic |
Restrictions
Steps involved in finding match
Function selection flow-chart
Start
Exact match?
Call that function
Stop
Match by promotion?
No
Yes
Match by conversion?
No
Error!!
Search for an Exact Match
1. int add(int a, int b)
2. int add(int a, int b, int c)
3. double add(double x, double y)
4. double add(double p, int q)
1. cout << add(5,10)
2. cout << add(6,-5,15)
3. cout << add(12.5,7.5)
4. cout << add(0.145, 2)
A match through promotion
int add(int a, int b)
cout << add(10, ‘A’);
A match through application of standard C++ conversion rules
void aFunc(char);
void aFunc(double);
aFunc(10);
C++ standard conversion rules
void aFunc(long);
void aFunc(double);
aFunc(10);
Error !! Ambiguous match
A match through application of a user-defined conversion
Thank you
Next Lecture: Polymorphism
Object Oriented Analysis and Design (CS 212)