1 of 24

CS300: Object Oriented Analysis and Design

Polymorphism

(Using C++)

2 of 24

Recap

  • Class and Objects
    • Identification
    • Design of attributes and functionalities
    • Getter and Setters
  • Class Interface
  • Constructor and Destructor

Object Oriented Analysis and Design (CS 212)

3 of 24

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)

4 of 24

Meaning of Modularity

  • The act of partitioning a program into individual components can reduce its complexity to some degree

  • It creates a number of well-defined, documented boundaries within the program.

  • Classes and objects form the logical structure of a system

  • Abstractions are kept in modules to produce the system’s physical architecture

Object Oriented Analysis and Design (CS 212)

5 of 24

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)

6 of 24

GRASP

  • Name chosen to suggest the importance of grasping fundamental principles to successfully design object-oriented software

  • Acronym: General Responsibility Assignment Software Patterns

  • Describe fundamental principles of object design and responsibility

  • General

Object Oriented Analysis and Design (CS 212)

7 of 24

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)

8 of 24

  • Refers to ‘one name having many forms

  • Different behaviour of an instance depending upon the situation’.

  • An ‘overloaded function’ refers to a function having (one name and) more than one distinct meanings.

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)

9 of 24

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

10 of 24

Function signature/ Extended name

  • The key to function overloading is a function’s argument list
  • This is also known as the function signature/ extended name.
  • It is the signature, not the function type that enables function overloading.
  • Two functions with same number and types of arguments in the same order are said to have the same signature

int myfunc(int i)

int myfunc(int i, float j)

void myfunc(int x, float y)

11 of 24

Overloading 🡪 Different signature

  • To overload a function name
    • Declared and defined with the same name
    • Characterized by different signatures.

  • For instance, following code fragment overloads a function name

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)

12 of 24

Task of a compiler

  • Signatures of two functions are identical, and the return types are same, then the second is treated as a re-declaration of the first.

  • Signatures of two functions are identical, however the return type differ, the second is an erroneous re-declaration

  • Signature of the two functions differ in either the number or type of their arguments, the two functions are considered to be overloaded

13 of 24

C++ compiler model

Pre-processor

Compiler

Assembly code

Assembler

Object code

Linker

Executable code

14 of 24

Name Mangling/ Name Decoration

  • Technique used to resolve unique names for programming entities

  • It provides a way of encoding additional information – function, structure, class, and other data type

  • The need arises where the language allows different entities to be named with the same name

15 of 24

Name mangling in C++

  • C++ allows overloading, however linker does not
  • Each function name must be unique
  • Generates a new name for each function – mangling/ decoration
  • The process of name mangling is compiler dependent.
  • The name mangled by a compiler may not be same as mangled by other compilers.
  • The mangled name contains all the necessary information that the linker needs
    • Linkage type, scope, calling convention, number of arguments, etc

16 of 24

Mangled name for g++ compiler

  • Mangling pattern

_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

17 of 24

Restrictions

  • Any two functions in a set of overloaded functions must have different argument lists.

  • Overloading functions with argument lists of the same types, based on return type alone, is an error.

  • Member functions cannot be overloaded solely on the basis of one being static and the other non-static.

  • Typedef

18 of 24

Steps involved in finding match

  • Argument matching

  • A match: A match is found for the function call

  • No match: No match is found for the function call

  • Ambiguous match: More than one defined instance for the function call

19 of 24

Function selection flow-chart

Start

Exact match?

Call that function

Stop

Match by promotion?

No

Yes

Match by conversion?

No

Error!!

20 of 24

Search for an Exact Match

  • The compiler first tries to find an exact match

  • Types of actual arguments are exactly same

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)

21 of 24

A match through promotion

  • If exact match is not found

  • Compiler uses integral promotion to the actual arguments

int add(int a, int b)

cout << add(10, ‘A’);

22 of 24

A match through application of standard C++ conversion rules

  • Compiler attempts built-in conversion
  • Implicit assignment conversion
  • Then finds a unique match for the function
  • If conversion results multiple match – compiler error !!

void aFunc(char);

void aFunc(double);

aFunc(10);

C++ standard conversion rules

void aFunc(long);

void aFunc(double);

aFunc(10);

Error !! Ambiguous match

23 of 24

A match through application of a user-defined conversion

  • If all the three methods fails

  • Then the compiler will try the user-defined conversion in the combinations to find a unique match.

24 of 24

Thank you

Next Lecture: Polymorphism

Object Oriented Analysis and Design (CS 212)