0% found this document useful (0 votes)
5 views7 pages

Operator Overloading

Operator overloading in C++ allows existing operators to be redefined for user-defined classes, enabling natural usage of objects as if they were basic data types. It is a form of polymorphism that enhances flexibility but cannot change operator syntax or semantics. The process involves defining operator functions within a class, which can be member or friend functions, to implement specific operations for the class's objects.

Uploaded by

kumaranurag3813
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)
5 views7 pages

Operator Overloading

Operator overloading in C++ allows existing operators to be redefined for user-defined classes, enabling natural usage of objects as if they were basic data types. It is a form of polymorphism that enhances flexibility but cannot change operator syntax or semantics. The process involves defining operator functions within a class, which can be member or friend functions, to implement specific operations for the class's objects.

Uploaded by

kumaranurag3813
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
You are on page 1/ 7

OPERATOR OVERLOADING:-

The capability to relate the existing operator with a member function and use the resulting operator
with objects of its class as its operands is called operator overloading. It is one of the most valuable
concepts introduced by c++ language. It is a type of polymorphism. Polymorphism permits to write of
multiple definitions for functions and operators. C++ has the number of standard data types like int,
float, char etc. The operators +,-,*, and = are used to carry out operations with these data types.
Operator overloading helps the programmer to use these operators with the objects of classes. The
outcome of operator overloading is that objects can be used in a natural manner as the variables of
basic data types.
Operator overloading provides a flexible option for the creation of new definitions for most
of the C++ operators. We can overload all the C++ operators except the following:

• Class members access operator (. , .*)


• Scope resolution operator (: :)
• Size operator(sizeof)
• Condition operator (? :)
Although the semantics of an operator can be extended, we can't change its syntax, the
grammatical rules that govern its use such as the no of operands precedence and associativety. For
example the multiplication operator will enjoy higher precedence than the addition operator.
When an operator is overloaded, its original meaning is not lost. For example,
the operator +, which has been overloaded to add two vectors, can still be used to add two integers.

DEFINING OPERATOR OVERLOADING:


To define an additional task to an operator, we must 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, which describes the task.
Syntax:-
return-type class-name :: operator op( arg-list)
{
function body
}
Where return type is the type of value returned by the specified operation and
op is the operator being overloaded. The op is preceded by the keyword operator, operator op is the
function name.
operator functions must be either member function, or friend
function. A basic defference between them is that a friend function will have only one argument for
unary operators and two for binary operators, This is because the object used to invoke the member
function is passed implicitly and therefore is available for the member functions. Arguments may be
either by value or by reference.

operator functions are declared in. the class using prototypes as follows:-
vector operator + (vector); /./ vector addition
vector operator-( ); //unary minus
friend vector operator + (vuelor, vector); // vector add
friend vector operator -(vector); // unary minus
vector operator - ( vector &a); // substraction
int operator = =(vector); //comparision
friend int operator = =(vector ,vrctor); // comparision
vector is a data type of class and may represent both magnitude and direction or a series
of points called elements.
The process of overloading involves the following steps:-
1. Create a class that defines the data type that is used in the overloading operation.
2. Declare the operator function operator op() in the public part of the class
3. It may be either a member function or friend function.
4. Define the operator function to implement the required operations.
Overloaded operator functions can be invoked by expressions such as
op x or x op;
for unary operators and
x op y
for binary opearators.
operator op(x);
for unary operator using friend function
operator op(x,y);
for binary operator usinf friend function.

Unary – operator overloading(using member function):


class abc
{
int m,n;
public:
abc()
{
m=8;
n=9;
}
void show()
{
cout<<m<<n;
}
operator -- ()
{
--m;
--n;
}
};
void main()
{
abc x;
x.show();
--x;
x.show();
}

Unary – - operator overloading(using friend function):


class abc
{
int m,n;
public:
abc()
{
m=8;
n=9;
}
void show()
{
cout<<m<<n;
}
friend operator --(abc &p);
};
operator -- (abc &p)
{
--p.m;
--p.n;
}
};
void main()
{
abc x;
x.show();
operator--(x);
x.show();
}
Unary operator+ for adding two complex numbers (using member function)
class complex
{
float real,img;
public:
complex()
{
real=0;
img=0;
}
complex(float r,float i)
{
real=r;
img=i;
}
void show()
{
cout<<real<<”+i”<<img;
}
complex operator+(complex &p)
{
complex w;
w.real=real+q.real;
w.img=img+q.img;
return w;
}
};
void main()
{
complex s(3,4);
complex t(4,5);
complex m;
m=s+t;
s.show();
t.show();
m.show();
}
Unary operator+ for adding two complex numbers (using friend function)
class complex
{
float real,img;
public:
complex()
{
real=0;
img=0;
}
complex(float r,float i)
{
real=r;
img=i;
}
void show()
{
cout<<real<<”+i”<<img;
}
friend complex operator+(complex &p,complex &q);
};
complex operator+(complex &p,complex &q)
{
complex w;
w.real=p.real+q.real;
w.img=p.img+q.img;
return w;
}
};
void main()
{
complex s(3,4);complex t(4,5);
complex m;
m=operator+(s,t);
s.show();t.show();
m.show();
}

Overloading an operator does not change its basic meaning. For example assume the +
operator can be overloaded to subtract two objects. But the code becomes unreachable.
class integer
{
intx, y;
public:
int operator + ( ) ;
}
int integer: : operator + ( )
{
return (x-y) ;
}
Unary operators, overloaded by means of a member function, take no explicit argument and
return no explicit values. But, those overloaded by means of a friend function take one
reference argument (the object of the relevant class).
Binary operators overloaded through a member function take one explicit argument and those
which are overloaded through a friend function take two explicit arguments.

Table 1
Operator to Arguments passed to the Arguments passed to the Friend
Overload Member Function Function
Unary Operator No 1
Binary Operator 1 2

You might also like