0% found this document useful (0 votes)
14 views

6 - Function & Operator Overloading

a

Uploaded by

sanasabirr7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

6 - Function & Operator Overloading

a

Uploaded by

sanasabirr7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Object Oriented

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.

• Function overloading is done with functions


having same name but carrying different
parameters.
• The parameters can be different in three ways
o In numbers
o In data types
o In order

4
Function Overloading -
Example
int intsquare ( int i ){
return i * i;
}
double doublesquare ( double d ){
return d * d;
}

• Function Overloading is the alternate to this situation. Instead to create


functions with different names, create functions having same name but perform
different tasks depending on the situation.

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.

• The compiler selects the proper function to


call by examining the number, types and
order of the arguments in the call.

9
Function Overloading

Functions having same name but


different parameters are called
overloaded functions.

10
Try it now

I think no need to try


;)

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

• An operator is always overloaded relative to a class (an


user defined data type).

• An overloaded operator gets a special meaning relative


to its class.

• However, the operator does not loose its original


meaning relative to other data types

• To overload an operator an operator function is defined


for the class

• The operator function can be a member or a friend


function of the class
13
Overloading
• Using operator overloading we can perform
basic operations on our own defined class’s
objects in the similar way as we perform them
on basic built-in types(like int, float, long, double)

• For example we can write


• Object3=Object1+object2;

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

• Member access operator (.)


• Scope resolution operator (::)
• Arrow operator or pointer to member
operator (->)
• .* (member selection through pointer to
function),
• and ? : (ternary conditional) 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

void operator ++()


{
// code here
}

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

• The number of operands on which


the operator would be applicable
cannot be changed too.

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

You might also like