0% found this document useful (0 votes)
86 views26 pages

Operator Overloading

Operator overloading in C++ allows operators to be redefined for user-defined types. This involves defining operator functions that specify the behavior for operators when used with class types. Operators can be overloaded as member functions or non-member friend functions. Binary operators are typically overloaded as member functions that take the right operand as a parameter, while friend functions are used when the left operand is not of the class type. The examples demonstrate overloading unary and binary operators as member functions and using friend functions when overloading binary operators involving a built-in type as the left operand.

Uploaded by

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

Operator Overloading

Operator overloading in C++ allows operators to be redefined for user-defined types. This involves defining operator functions that specify the behavior for operators when used with class types. Operators can be overloaded as member functions or non-member friend functions. Binary operators are typically overloaded as member functions that take the right operand as a parameter, while friend functions are used when the left operand is not of the class type. The examples demonstrate overloading unary and binary operators as member functions and using friend functions when overloading binary operators involving a built-in type as the left operand.

Uploaded by

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

OPERATOR OVERLOADING

• C++ has an ability to provide the operators with a


special meaning for a user define data type(class).

OPERATOR OVERLOADING
• The “mechanism of giving such special meanings to
an operator is known as operator overloading.”

• Using traditional operators with user-defined objects


• Examples of already overloaded operators
•Operator << is both the stream-insertion operator
and the bitwise left-shift operator

2
•Precedence of an operator cannot be changed

RESTRICTIONS ON OPERATOR OVERLOADING


•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
•No new operators can be created
•No overloading operators for built-in types

3
RESTRICTIONS ON OPERATOR OVERLOADING
4
Syntax:
return_type operator op(parameters);

• return_type is the type of value returned

GENERAL FORMAT
•op is the operator being overloaded
•op is preceded by the keyword operator which is the
function name

5
•Operator functions as member functions

OPERATOR FUNCTIONS AS CLASS MEMEBR VS AS FRIEND FUNCTION


• Leftmost operand must be an object (or reference to an
object) of the class
• If left operand of a different type, operator function must be a non-
member function
• A member function has no arguments for unary operators
and one argument for binary operators

• Operator functions as non-member functions


• Must be friends if needs to access private or protected
members
• friend functions require one argument for unary operator
and two arguments for binary operator

6
• Operator that takes single operand in an expression
or statement

OVERLOADING UNARY OPERATOR


• Can be overloaded with no argument or one
argument
• Usually implemented using member functions
• Avoid friend function and class as they violate
encapsulation of class

7
#include <iostream>
using namespace std;
class space
{
int x,y,z;

EXAMPLE
public:
void getdata(int a,int b,int c);
void display(void);
void operator-(); //Overloading unary minus
};

8
void space :: getdata(int a,int b,int c)
{
x=a; y=b; z=c;
}
void space :: display (void)
{

EXAMPLE
cout << x << " ";
cout << y << " ";
cout << z << "\n";
}
void space :: operator-() //Defining operator-()
{
x=-x; y=-y; z=-z;
}
9
int main ()
{
space S;
S.getdata(5,-15,25);
cout << "Value before overloading" << "\n";
cout << "S =";

EXAMPLE
S.display();
-S; //activates operator-()
cout << "Value after overloading" << "\n";
cout << "S =";
S.display();
return 0;
}

10
#include <iostream>
counter operator ++ ( )
using namespace std; {
class counter   count++;
{ counter temp;
int count; temp.count = count;
public: return temp;

EXAMPLE
counter( ) }
{ };//class end
count = 0;
}
int getcount( )
{
return count;
}
11
int main( )
{
counter c1,c2;
cout<<"\nc1="<<c1.getcount( );
cout<<"\nc2="<<c2.getcount( );
++ c1;

EXAMPLE
c2=++c1;
cout<<"\nc1="<<c1.getcount( );
cout<<"\nc2="<<c2.getcount( );
return 0;
}

12
PROBLEM:

LIMITATIONS TO INCREMENT OPERATOR


• When applied to basic data types prefix and post fix
operators are two different operators.
• But when they are overloaded there is no distinction
(difference) between prefix and post fix notation.
• The expression c2=c1++ has exactly the same effect
as c2=++c1.
SOLUTION:
• two overloaded operators, one for prefix and one for
post fix.
operator ++ ( ); //prefix
operator ++ (int ); //post fix 13
• Operator that takes two operands and computes the
result

OVERLOADING BINARY OPERATOR


int a=10,b=5; int c = a + b;
where a is known as left-hand operand as it is in the
left side of operator (+) and b is known as right hand
operand as it is right side of operator (+).
• In overloading binary operators the left hand operand
is used to invoke the operator function and the right
operand is passed as an argument.
• In binary operator overloading using member
function, we need to define operator function inside
class with right hand operand as an argument to it.
The left hand operand must be the object of class
where an operator function is defined.
14
• Binary operator overloaded through
• member function take one explicit argument

OVERLOADING BINARY OPERATOR


return_type operator op (classs_name object)
{
function body;
}
• friend function take two explicit argument
friend return_type operator op (classs_name object1,
classs_name object2)
{
function body;
}
15
#include <iostream>
using namespace std;

EXAMPLE USIING MEMBER FUNCTION


class complex
{
float x; //real part
float y; //imaginary part
public:
complex () {} //constructor1
complex (float real , float imag) //constructor2
{ x=real; y=imag; }
complex operator+(complex);
void display(void);
};
16
complex complex :: operator+(complex c)
{

EXAMPLE USIING MEMBER FUNCTION


complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}
void complex :: display(void)
{
cout << x << "+j" << y <<"\n";
}

17
int main()
{

EXAMPLE USIING MEMBER FUNCTION


complex c1,c2,c3;
c1=complex (2.5,3.5);
c2=complex (1.6,2.7);
c3=c1+c2;
cout << "c1 = “<<c1.display();
cout << "c2 = “<<c2.display();
cout << "c3 = c1 + c2"<<endl;
cout << "c3 = “<<c3.display()<<endl;
return 0;
}

18
EXAMPLE USIING MEMBER FUNCTION
19
• We can use a friend function with a built-in type data

OVERLOADING BINARY OPERATOR USING FRIEND FUNCTION


as the left-hand operand and an object as the right
hand operand, whereas in the case of member
function the left hand operand must be an object of
the class which is responsible to invoke (call) the
overloaded member function.
• Therefore, when we need to use two different types of
operands for a binary operator we can use friend
function to overload the operator.
• Hence, below statement will not work for a member
function but for a friend function
A= 2+B; A= 2*B;
where A and B are objects of the class and 2 is built
in data type.
20
#include <iostream>
using namespace std;

EXAMPLE USIING FRIEND FUNCTION


const int size=3;
class vector
{
int v[size];
public:
vector();
vector (int *x);
friend vector operator *(int a , vector b);
friend vector operator *(vector b, int a);
friend istream & operator >>(istream &, vector &);
friend ostream & operator <<(ostream &, vector &);
};
21
vector::vector()
{

EXAMPLE USIING FRIEND FUNCTION


for(int i=0;i<size;i++)
{
v[i]=0;
}
}
vector::vector(int *x)
{
for(int i=0;i<size;i++)
{
v[i]=x[i];
}
}

22
vector operator *(int a, vector b)
{ vector c;

EXAMPLE USIING FRIEND FUNCTION


for(int i=0;i<size;i++)
{
c.v[i]=a*b.v[i];
}
return c;
}
vector operator *(vector b, int a)
{ vector c;
for(int i=0;i<size;i++)
{
c.v[i]=b.v[i]*a;
}
return c;
} 23
istream & operator >>(istream & din, vector & b)
{

EXAMPLE USIING FRIEND FUNCTION


for(int i=0;i<size;i++)
{
din>>b.v[i];
}
return din;
}
ostream & operator <<(ostream & dout, vector & b)
{ dout<<“(“<<b.v[0];
for(int i=1;i<size;i++){
dout<<“,”<<b.v[i];
}
dout<<“)”;
return dout;
} 24
int x[size]={2,4,6};
int main()

EXAMPLE USIING FRIEND FUNCTION


{ vector m;
vector n=x;
cout<<“enter the elements of vector m”<<endl;
cin>>m;
cout<<endl<<“m = ”<<m<<endl;
vector p,q;
p=2*m;
q=n*2;
cout<<“p = “<<p<<endl<<“q = “<<q<<endl;
return 0;
}
25
• We can not use friend function to overload following

OVERLOADING BINARY OPERATOR USING FRIEND FUNCTION


operators:
• Assignment operator (=)
• Function call operator [ ( ) ]
• Sub – scripting operator ( [ ] )
• Class member access operator (->)
However, these operators can be overloaded by using
member functions.

26

You might also like