Upload
ujjwal-mahajan
View
213
Download
0
Embed Size (px)
DESCRIPTION
c++
Citation preview
1
The Meaning of the word.
From the Greek: Polus + Morphe = Polumorphos
(many ) (shape/form) POLYMORPHISM
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)
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
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.
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..)
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++.
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();
8
Operator Overloading -
Motivation
Question: How many function calls are involved in the following statement?
a = 2 + 3
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));”
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.
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.
Precautions for Operator
overloading
Use Similar meaning : Do not overload „+‟ operator for subtraction.
Restrict the number: Avoid too many operator overloading methods.
12
Operator that can not be
over loaded
Scope Resolution Operator ::
Conditional Operator ?
Dot operator .
Pointer to member operator .*
13
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
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
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
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
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