0% found this document useful (0 votes)
9 views10 pages

4 Operator Overloading

The document explains operator overloading in C++, detailing how operators can be given special meanings for user-defined data types. It outlines the rules and steps for overloading operators, including examples of unary and binary operator overloading. Additionally, it provides code snippets demonstrating the implementation of operator overloading for complex numbers and other classes.

Uploaded by

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

4 Operator Overloading

The document explains operator overloading in C++, detailing how operators can be given special meanings for user-defined data types. It outlines the rules and steps for overloading operators, including examples of unary and binary operator overloading. Additionally, it provides code snippets demonstrating the implementation of operator overloading for complex numbers and other classes.

Uploaded by

Chaya Anu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OVERLOADING

OPERATOR OVERLOADING
C++ has the ability to provide the operators with as special meaning for a data type. The mechanism of
giving such special meanings to an operator is known as operator overloading. We can overload all the
operators except the following:
Class member access operator (“.” And “.*”)
Scope resolution operator “::”
Size operator (sizeof)
Conditional operator
To define an additional task to an operator, specify what it means in relation to the class to which
the operator is applied. This is done with the help of a special function, called operator function.
The process of overloading involves following steps:
1. Create a class that defines the data type that is to be used in the overloading operation.
2. Declare the operator function operator op() in the public part of the class. It may be a member
function or a friend function.
3. Here op is the operator to be overloaded.
4. Define the operator function to implement the required operations.

General Form:-

return-type classname :: operator op(arglist)

Function body
Ex:
complex complex::operator+(complex c)
{
complex t;
[Link]=real+[Link];
[Link]=img+[Link];
return t;
}
Concept of Operator Overloading
One of the unique features of C++ is Operator Overloading. Applying overloading to operators means,
same operator in responding different manner. For example operator + can be used as concatenate
operator as well as additional operator.

That is 2+3 means 5 (addition), whereas


"2"+"3" means 23 (concatenation).
Performing many actions with a single operator is operator overloading. We can assign a user defined
function to an operator. We can change function of an operator, but it is not recommended to change the
actual functions of operator. We can't create new operators using this operator overloading.
Operator overloading concept can be applied in following two major areas (Benefits)
1. Extension of usage of operators
2. Data conversions
Rules to be followed for operator overloading:-
1. Only existing operators can be overloaded.
2. Overloaded operators must have at least one operand that is of user defined operators
[Link] cannot change basic meaning of an operator.
4. Overloaded operator must follow minimum characteristics that of original operator
5. When using binary operator overloading through member function, the left hand operand must be
an object of relevant class
The number of arguments in the overloaded operator‟s arguments list depends

1. Operator function must be either member function or friend function.


2. If operator function is a friend function then it will have one argument for unary operator
& two arguments for binary operator
3. If operator function is a member function then it will have Zero argument for unary
operator & one arguments for binary operator

Unary Operator Overloading


An unary operator means, an operator which works on single operand. For example, ++ is an unary
operator, it takess single operand (c++). So, when overloading an unary operator, it takes no argument
(because object itself is considered as argument).

Syntax for Unary Operator (Inside a class)

return-type operator operatorsymbol( )


{
//body of the function
}

Ex:
void operator-()
{
real=-real;
img=-img;
}
Syntax for Unary Operator (Outside a class)
return-type classname::operator operatorsymbol( )
{
//body of the function
}

Example 1:-
void operator++()
{
counter++;
}

Example 2:-

void complex::operator-()
{
real=-real;
img=-img;
}
The following simple program explains the concept of unary overloading.
#include < iostream.h >
#include < conio.h >
// Program Operator
Overloading class fact
{
int a;

public:
fact ()
{
a=0;
}
fact (int i)
{
a=i;
}
fact operator!()
{
int f=1,i;
fact t;
for (i=1;i<=a;i++)
{
f=f*i;
}
t.a=f;
return t;
}
void display()
{
cout<<”The factorial ”<< a;
}
};
void main()
{
int x;
cout<<”enter a number”;
cin>>x;
fact s(x),p;
p=!s;
[Link]();
}
Output for the above program:
Enter a number 5
The factorial of a given number 120

Explanation:
We have taken „!‟ as operator to overload. Here class name is fact. Constructor without parameters to take
initially value of „x‟ as 0. Constructor with parameter to take the value of „x‟ . We have create two
objects one for doing the factorial and the other for return the factorial. Here number of parameter for an
overloaded function is 0. Factorial is unary operator because it operates on one dataitem. operator
overloading find the factorial of the object. The display function for printing the result.

Overloading Unary Operator -

Example 1:-
Write a program to overload unary operator –
#include<iostream>
using namespace std;
class complex
{
float real,img;
public:
complex();
complex(float x, float y);
void display();
void operator-();
};
complex::complex()
{
real=0;img=0;
}
complex::complex(float x, float y)
{
real=x;
img=y;
}
void complex::display()
{
int imag=img;

if(img<0)
{
imag=-img;
cout<<real<<" -i"<<imag<<endl;
}
else
cout<<real<<" +i"<<img<<endl;
}
void complex::operator-()
{
real=-real;
img=-img;
}
int main()
{
complex c(1,-2);
[Link]();
cout<<"After Unary - operation\n";
-c;
[Link]();
}
Example 2:-
#include<iostream.h>
using namespace std;
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display();
void operator-();
};
void space :: getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space :: display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
void space :: operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
space s;
[Link](10,-20,30);
[Link]();
-s;
cout<<"after negation\n";
[Link]();
}
Output:
x=10
y=-20
z=30
after negation
x=-10
y=20
z=-30

It is possible to overload a unary minus operator using a friend function as follows:

friend void operator-(space &s);

Example 3:-
Unary minus operator using a friend function
#include<iostream.h>
#include<iostream.h>
using namespace std;
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display();
friend void operator-(space &);
};
void space :: getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}

void space :: display()


{
cout<<x<<" "<<y<<" "<<z<<endl;
}
void operator-(space &s)
{
s.x=-s.x;
s.y =-s.y;
s.z =-s.z;
}

int main()
{
space S;
[Link](10,-20,30);
[Link]();
-S;
cout<<"after negation\n";
[Link]();
}

Output:
10 -20 30
after negation
-10 20 -30

Binary Operator Overloading


A binary operator means, an operator which works on two operands. For example, + is an binary
operator, it takes single operand (c+d). So, when overloading a binary operator, it takes one argument
(one is object itself and other one is passed argument).

Syntax for Binary Operator (Inside a class)

return-type operator operatorsymbol(argument)


{
//body of the function
}

Syntax for Binary Operator definition (Outside a class)

return-type classname::operator operatorsymbol(argument)


{
//body of the function
}

Example
complex operator+(complex s)
{
complex t;
[Link]=real+[Link];
[Link]=img+[Link];
return t;
}
The following program explains binary operator overloading:
#include < iostream.h >
#include < conio.h >
class sum
{
int a;
public:
sum()
{
a=0;
}
sum(int i)
{
a=i;
}
sum operator+(sum p1)
{
sum t;

t.a=a+p1.a;
return t;

}
void main ()
{
cout<<”Enter Two
Numbers:” int a,b;
cin>>a>>b; sum x(a),y(b),z; [Link]();
z=x+y;
cout<<”after applying operator \n”;
[Link]();
getch();
}
Output:
Enter two numbers 5 6
After applying operator
The sum of two numbers 11

Explanation: The class name is “sum”. We have to create three objects two for to do the sum
and the other for returning the sum. “+” is a binary operator operates on members of two objects
and returns the result which is member of object. Here number of parameters are 1. The sum is
displayed in display function.

Write a program to over load arithmetic operators on complex numbers using member
function
#include<i
ostream.h>
class
complex
{

public:
float real,img;

complex(){ } complex(float x, float y)


{
real=x; img=y;

}
complex
operator+(complex
c) void display();
};
complex complex::operator+(complex c)
{
complex temp;
[Link]=real+[Link];
[Link]=img+[Link];
return temp;
}

void complex::display()
{
int imag=img;
if(img<0)
{

imag=-imag;
cout<<real<<”-i”<<imag;
}
else
cout<<real<<”+i”<<img;
}

int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
[Link]();
return 0;
}

Overloading Binary Operators Using Friends

1. Replace the member function declaration by the friend function


declaration in the class friend complex operator+(complex,
complex)
2. Redefine the operator function as follows:
complex operator+(complex a, complex b)
{
return complex((a.x+b.x),(a.y+b.y));
}
Write a program to over load arithmetic operators on complex numbers using
friend function

#include<iostream.h>
class complex
{

public:float real,img;

complex(){ }
complex(float x, float y)
{
real=x; img=y;

}
friend complex
operator+(complex); void
display();
};
complex operator+(complex c1, complex c2)
{
complex temp;
[Link]=[Link]
al+[Link];
[Link]=[Link]
g+[Link];
return temp;
}
void complex::display()
{
If(img<0)
{
img=-img; cout<<real<<”-i”<<img;

}
else
cout<<real<<”+i”<<img;
}
int main()
{
complex c1,c2,c3;
c1=comple
x(2.5,3.5);
c2=comple
x(1.6,2.7);
c3=c1+c2;
[Link](
);
return 0;
}

You might also like