18
1 The Meaning of the word. From the Greek: Polus + Morphe = Polumorphos (many ) (shape/form) POLYMORPHISM

Polymorph is m

Embed Size (px)

DESCRIPTION

c++

Citation preview

Page 1: Polymorph is m

1

The Meaning of the word.

From the Greek: Polus + Morphe = Polumorphos

(many ) (shape/form) POLYMORPHISM

Page 2: Polymorph is m

2

The Meaning of the word.

In object-oriented computing it means: different forms of data being handled by the same type of process.

Example: The operator + has a different meaning in the expression 2 + 3 (add two integers) than in 1.7 + 3.3 (add two floating point numbers)

Page 3: Polymorph is m

3

Types of Polymorphism

In Object Oriented Programming there are two types of polymorphism:

1. Compile time Polymorphism (method overloading, operator overloading and method overriding)

2. run-time polymorphism

Page 4: Polymorph is m

4

Types of Polymorphism

a) method overloading and operator overloading

b) method overriding

c) run-time polymorphism

Method overloading can also be applied in non-object oriented contexts and refers both to functions and methods.

Page 5: Polymorph is m

5

Types of Polymorphism

a) method overloading, with the special and important case of operator overloading

b) method overriding

c) run-time polymorphism

Method overriding and run-time polymorphism

are specific to inheritance hierarchies

and object oriented programming

(more about this next week..)

Page 6: Polymorph is m

6

Types of Polymorphism

In Object Oriented Programming there are three types of polymorphism:

a) method overloading, with the special and important case of operator overloading

b) method overriding

c) run-time polymorphism

Run-time polymorphism, also called dynamic binding, or late binding is often considered as the object oriented feature of C++.

Page 7: Polymorph is m

7

Method & Function

Overloading

Overloading a function simply means, that a function is not only defined by its name but by its name and parameter types.

The following functions are different in C++:

int add(int i, int k);

void add(sum x);

float add();

Page 8: Polymorph is m

8

Operator Overloading -

Motivation

Question: How many function calls are involved in the following statement?

a = 2 + 3

Page 9: Polymorph is m

9

Operator Overloading -

Motivation

Question: How many function calls are involved in the following statement?

a = 2 + 3

There are two functions implicitly involved: + and =.

Look at this statement as

“assign(a, add(2,3));”

Page 10: Polymorph is m

10

Operator Overloading

So, operators as +, -, *, <<, =, etc. can be seen as “functions” as well. That means we can overload operators.

The C++ syntax uses “function names” prefixed with “operator” for overloading operators.

Page 11: Polymorph is m

11

Operator Overloading

The following operators can be overloaded:

new, delete, +, -, *, /, %, ^, &, |, ~, !, =, <, >, +=, -=, *=, /=, %=, ^=, &=, |=, <<, >>, >>=, <<=, ==, !=, <=, >=, &&, ||, ++, --, , , ->*. ->, (), []

Note that "=" has already a default behaviour. When "overloaded" it will be in fact overridden.

Page 12: Polymorph is m

Precautions for Operator

overloading

Use Similar meaning : Do not overload „+‟ operator for subtraction.

Restrict the number: Avoid too many operator overloading methods.

12

Page 13: Polymorph is m

Operator that can not be

over loaded

Scope Resolution Operator ::

Conditional Operator ?

Dot operator .

Pointer to member operator .*

13

Page 14: Polymorph is m

Run time Polymorphism

Choosing methods during the execution of a program is called run time polymorphism. This type of polymorphism is also called late binding or dynamic binding because functions are not selected beforehand but rather are chosen at run time. Late binding requires some overhead but provides increased power and flexibility. Virtual functions implement the late binding concept.

14

Page 15: Polymorph is m

Redefining a base class function in the derived class to have our own implementation is referred as overriding.

Often confused with overloading which refers to using the same function name but with a different signature. However in overriding the function signature is the same.

15

C++ Overriding/virtual functions

Page 16: Polymorph is m

Virtual function is one that really does not exist but it appears real in some parts of a program.

When a function is made virtual, c++ determines which function to use at run time on the basis of the type of the object pointed at by the base pointer, rather than the type of the pointer.

16

C++ Overriding/virtual functions

Page 17: Polymorph is m

Difference between a non-virtual member function and a virtual member function is that the non-virtual member function is resolved at compile time.

17

Page 18: Polymorph is m

Pure Virtual Function

A pure virtual function is a function, which exists with only the function declaration and initialized to 0 (without function definition) in the base class.

The class which is having the pure virtual function is known as Abstract class.

Pure virtual function will be implemented in the derived classes.

18