6 - Function & Operator Overloading
6 - Function & Operator Overloading
Programming
Overloading
1
Review
• Static as Keyword
• Static Members
o Member Variable / Data Member
o Member functions
• Friend Functions
• Friend Classes
2
Objective
• Function Overloading
• Operator Overloading
3
Function Overloading
• Function Overloading means, function having
same name but perform different tasks
depending on the situation.
4
Function Overloading -
Example
int intsquare ( int i ){
return i * i;
}
double doublesquare ( double d ){
return d * d;
}
5
Example
// Overloadedfunctions.
#include <iostream>
Using namespace std; //for running program in
dev c++
// function squarefor int values
int square( int x)
{
cout << "squareofinteger " << x<< "is";
return x* x;
} // end function square with int
argument
// function squarefor double values
double square( double y)
{
cout << "squareofdouble " << y<< "is";
return y* y;
}/ / end function square with doubleargument 6
Example (Cont.)
int main()
{
cout <<square(7) ; // calls int
version
cout <<endl;
cout << square(7.5); // calls
doubleversion
cout << endl;
} // end main
7
Output
square of integer 7 is 49
square of double 7.5 is
56.25
8
How its works
• Now, the compiler will automatically match
the prototype and will call the square()
according to parameters passed.
9
Function Overloading
10
Try it now
11
Operator Overloading
• Operator overloading is one of the most exciting
features of object-oriented programming.
• Similar to function overloading, with Operator
overloading you can perform different task
depending on the situation where it is being used.
• It can transform complex or obscure program listings
obvious ones. For example
• statements like
d3.addobjects(d1, d2);
or the similar but equally obscure
d3 = d1.addobjects(d2);
can be changed to the much more readable
d3 = d1 + d2;
12
Operator Overloading
• Operator overloading is a type of function overloading
14
Example
Date Class
15
Example
class Date
{
int day;
int month;
int year;
};
16
Example
Date mydate; //object of the class date
• We want to increment one day to this date
mydate.increment();
17
Using operator
• By using operator overloading we can
write
mydate++;
• The term operator overloading refers to
giving the normal C++ operators such as
+, *, <=, and +=, additional meanings
when they are applied to user-defined
data types.
18
Overload able
Operators
19
We can’t overload
these operators
20
Example
class Date
{
int day, month, year;
public:
void incrementDay()
{
day++;
}
};
21
Example
void main()
{
Date mydate;
mydate.incrementDay();
}
22
Return Syntax
Type
Operator
keyword Operator
23
Example with
Operator Overloading
class Date
{
int day, month, year;
public:
Date() //constructor
{ day=0; month=0; year=0; }
//prefix ++ operator
void operator ++()
{
++day;
}
};
24
Example with
Operator Overloading
int main()
{
Date mydate;
++mydate;
mydate.displayDate();
}
25
More about
Date mydate, tomorrow;
tomorrow=++mydate; //error
•We have to understand basic concept
of increment operator
int a, b;
a=0;
b=++a;
26
More about
• Overloading cannot change the
original precedence of the operator
27
Revised date class
example 2
class Date
{
int day, month, year;
public:
Date operator ++()
{
Date temp;
temp.day=++day;
return temp;
}
};
28
Revised Date example
2
Void main()
{
Date mydate,
tomorrow;
tomorrow=++mydate;
}
29
Benefits of operator
overloading
• In effect, operator overloading gives you the
opportunity to redefine the C++ language.
• If you find yourself limited by the way the C++
operators work, you can change them to do
whatever you want.
• Increases program readability
• We using classes to create new kinds of variables,
and operator overloading to create new
definitions for operators
30
Reading Material
• Chapter 9
o Innovative’s Object Oriented programming Using C++
31