6. Example:
For example:
The + (plus) operator in C++:
4 + 5 <-- Integer addition
3.14 + 2.0 <-- Floating point addition
s1 + "bar" <-- String concatenation!
7. Types of Polymorphism
• In compile time polymorphism, compiler is able to select
the appropriate function a particular call at the compile
time.
• In run time polymorphism, an appropriate member
function is selected while the program is running.
9. Kinds of Polymorphism
• Runtime/Dynamic polymorphism (Based on virtual
methods)
• Compile time/Static polymorphism (Based on
templates)
10. Runtime polymorphism
• Runtime binding between abstract type and concrete type
• Enforces a common interface for all derived types via the
base class
• Enforces an ‘Is-A’ relationship
• Used extensively in OO frameworks
• Templates eating into parts of its territory
11. Compile time Polymorphism
• Compile time binding between abstract type and concrete
type
• Concrete types need not belong to the same inheritance
hierarchy. They just need to meet the ‘Constraints’
imposed by the generic type. So, more generic than the
virtuals
• Foundation of ‘Generic programming’ or programming
based on ‘Concepts’ (Eg: STL)
• A very ‘Happening area’ in C++ right now (See TR1,
Boost, C++0x...)
12.
13. Introduction
• Operator overloading
– Enabling C++’s operators to work with class objects
– Using traditional operators with user-defined objects
– Requires great care; when overloading is misused, program difficult to
understand
– Examples of already overloaded operators
• Operator << is both the stream-insertion operator and the bitwise left-
shift operator
• + and -, perform arithmetic on multiple types
14. Introduction
• Overloading an operator
–Write function definition as normal
–Function name is keyword operator followed by the
symbol for the operator being overloaded
–operator+ used to overload the addition operator (+)
15. Restrictions on Operator Overloading
• Restrictions on Operator Overloading
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
16. C++ Operators that cannot be overloaded
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that cannot be overloaded
. .* :: ?: sizeof
17. Restrictions on Operator Overloading
• Overloading restrictions
– Precedence of an operator cannot be changed
– Associativity of an operator cannot be changed
– Arity (number of operands) cannot be changed
• Unary operators remain unary, and binary operators remain
binary
• Operators &, *, + and - each have unary and binary versions
• Unary and binary versions can be overloaded separately
• Thank You !!
18. Restrictions on Operator Overloading
• No new operators can be created
– Use only existing operators
• No overloading operators for built-in types
– Cannot change how two integers are added
– Produces a syntax error
19.
20. In C++, two or more functions can share
the same name as long as their parameter
declarations are different.
In this situation, the functions that share
the same name are said to be overloaded,
and the process is referred to as function
overloading
21. To see why function overloading is important,
first consider three functions defined by the C subset:
abs(), labs(), and fabs().
abs() returns the absolute value of an integer,
labs() returns the absolute value of a long, and
fabs() returns the absolute value of a double.
22. Function Overloading – DEFINITION
• It is the process of using the same name for two or
more functions.
The secret to overloading is that each redefinition of
the function must use
either-
• different types of parameters
• different number of parameters.
23. The key to function overloading is a function’s argument list.
A function’s argument list is known as its signature.
Example :
Different types of arguments
void print(int a);
void print (double b);
void print(char c);
Different number of
parameters
void area(float r);
void area(float l, float b);
void area(float a, float b, float
c);
24. STEPS FOR FINDING BEST MATCH
• The actual arguments are compared with formal
arguments of the overloaded functions and the best
match is found through a number of steps.
•
In order to find the best match, the compiler follows
the steps given below:
25. STEP - 1 : Search for an exact match
•
If the type of the actual and formal argument is
exactly the same, the compiler invokes that function.
void print(int x); // function #1
void print(float p); // function #2
void print(char c); // function #3
------------
-------------
print(5.3); //invokes the function #2
26. STEP- 2 : A match through promotion
•
If no exact match is found, an attempt is made to
achieve a match through promotion.
void print(int x); // function #1
void print(float p); // function #2
void print(double f); // function #3
------------
-------------
print(‘c’); //matches the function #1 through
promotion