2. . i.e. to overload an operator is to provide it a new meaning for
user defined data types.
In simple words we can say that by using Operator Overloading we can perform
basic operations like (Addition, Subtraction, multiplication, Division and so
on………..) on our own defined objects of the class
By Overloading the appropriate Operators, you can use Objects in
expressions in just the same way that you use built in data types in C++
Operator overloading allows full integration of new data types into the
programming environment because operators can be extended to work not just
with built-in data types but also with classes.
Operator Overloading is One of the most exciting feature
of Object oriented programming.
In C++ the Overloading principle applies not only to function
but to operators as well.
3. O
P
E
R
A
T
O
R
O
V
E
R
L
O
A
D
I
N
G
Explanation
Normally a=b+c ;
works only with basic data types such as int
and float, and attempting to apply it when a
b and c are objects of primitive data types.
However we can make this statement legal
even when a b and c are user defined
data_types.
And actually oprator_overloading
is the only feature that gives you
opportunity to redefine the C++
Language.
5. An Example that will illustrate how to overload unary
operators:
//Increment counter variable with ++ Operator
//////////////////////////////////////////////////////////////////////////////////////
#include<iostream.h>
#include<conio.h>
Class counter
{
private:
int count; //count
public:
counter() : count(0) //constructor
{ }
int get_count() //return count
Void operator ++() //increment (prefix)
{
++count;
}
};
6. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
counter c1, c2; //define and initialize
cout<<“n c1=“<<c1.get_count(); //display
cout<<“n c2=“<<c2.get_count();
++c1; //increment c1
++c2; //increment c2
++c2; //increment c2
cout<<“n c1=“<<c1.get_count(); //display again
cout<<“n c2=“<<c2.get_count() <<endl;
getch();
} // - - - -- end of program - - - - - -- //
Here is the program’s output:
C1=0 counts are initially 0
C2=0
C1=1 incremented once
C2=2 incremented twice
7. OPERATOR ARGUMENTS
In main () the ++ operator is applied to a specific object, as in
the expression ++c1. Yet
operator++() takes no arguments. What does this operator
increment? It increments the Count data in the object of
which it is a member. Since member functions can always
access
the particular object for which they’ve been invoked, this
operator requires no arguments.
8.
9. Binary Operators can be overloaded just
as easily as Unary operators, we
frequently use binary operators to
perform various arithmetic operations,
but by overloading binary operators we
are able to perform various arithmetic
operations on our own user-defined data
types.
Binary
Arithmetic
Operators
Relational
Operators
Logical
Operators
10. OVERLOADING ARITHMETIC OPERATOR
Some of the most commonly used operators in C++ are
the arithmetic operators i.e. addition operator (+),
subtraction operator (-), multiplication operator (*) and
division operator(/).
All the Arithmetic operators are Binary operators and
binary operators and binary operators are overloaded in
exactly the same way as the unary operators.
Binary operators operate on two operands. i.e. One from
each side of the operator.
For example: a + b, a – b, a * b, a / b.
11. Here is a program that will show you how to
overload arithmetic operator.
// overloaded ‘+’ operator adds two Distances
#include <iostream.h>
class Distance //English Distance class
{
private:
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
void getdist() //get length from user
{
cout << “nEnter feet: “; cin >> feet;
cout << “Enter inches: “; cin >> inches;
}
void showdist() const //display distance
{ cout << feet << “’-” << inches << ‘”’; }
Distance operator + ( Distance ) const; //add 2 distances
};
12. Distance Distance::operator + (Distance d2) const //return sum
{
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; //add the inches
if(i >= 12.0) //if total exceeds 12.0,
{ //then decrease inches
i -= 12.0; //by 12.0 and
f++; //increase feet by 1
} //return a temporary Distance
return Distance(f,i); //initialized to sum
}
int main()
{
Distance dist1, dist3, dist4; //define distances
dist1.getdist(); //get dist1 from user
Distance dist2(11, 6.25); //define, initialize dist2
dist3 = dist1 + dist2; //single ‘+’ operator
dist4 = dist1 + dist2 + dist3; //multiple ‘+’ operators
//display all lengths
cout << “dist1 = “; dist1.showdist(); cout << endl;
cout << “dist2 = “; dist2.showdist(); cout << endl;
cout << “dist3 = “; dist3.showdist(); cout << endl;
cout << “dist4 = “; dist4.showdist(); cout << endl;
return 0;
}
13. Explanation
To show that the result of an addition can be used in another
addition as well as in an assignment,
another addition is performed in main(). We add dist1, dist2, and
dist3 to obtain dist4
(which should be double the value of dist3), in the statement dist4
= dist1 + dist2 + dist3;
Here’s the output from the program:
Enter feet: 10
Enter inches: 6.5
dist1 = 10’-6.5” ← from user
dist2 = 11’-6.25” ← initialized in program
dist3 = 22’-0.75” ← dist1+dist2
dist4 = 44’-1.5” ← dist1+dist2+dist3
14. OVERLOADING RELATIONAL OPERATORS
There are various relational operators in C++ like (< ,>, <=, >=, == ,
etc.) these operators can be used to compare C++ built-in data types.
Any of these operators can be overloaded, which can be used to
compare the objects of a class.
Following EXAMPLE explains how a < operator can be overloaded,
and in the same way we are able to overload the other relational
operators.
All relational operators are binary, and should return either true or
false. Generally, all six operators can be based off a comparison
function, or each other, although this is never done automatically (e.g.
overloading > will not automatically overload < to give the opposite).
18. ARITHMETIC ASSIGNMENT OPERATORS
Arithmetic Assignment operators are overloaded in exactly the
same way as we have overloaded all other arithmetic operators
and these operators can be used to create an object just like
copy constructors.
The following example explains how to overload an arithmetic
operator.
``````````````````````````````````````````````````````````````````````````````````
// overload ‘+=‘ assignment operator
#include<iostream.h>
class Distance //English Distance class
{
private:
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
21. The Operator Keyword
Sometimes question arises, How a normal C++ operator act
on a user-defined operand?
The Operator keyword is used to overload the ++ operator in
this declarator.
e.g. void operator ++().
Where void is the return type, the operator keyword is the
name of a function and the operator ++().
This declarator syntax tells the compiler to call this member
function, whenever the ++ operator is encountered.
If the operand is a basic type such as int as in ++ intvar, then
compiler use it’s built in routine to increment as int.
The compiler will know to use user-written operator ++()
instead.
27. Restrictions On Operator Overloading
There are Some Restrictions that apply to operator overloading,
for example we can not alter the precedence of an operator.
We cannot change the number of operands, that an operator
takes.
Operator function can not have default arguments.
No new operators can be created.
Finally the following operators ca not be overloaded:
. :: .* ?