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

Operator Overloading

Operator overloading allows operators like + and - to be used with user-defined types by extending their functionality. It is a type of polymorphism that allows expressions to be built using objects. The general syntax for operator overloading defines a method with the operator as the name that takes an object as a parameter and returns the result. For example, the + operator can be overloaded for a complex number class to add the real and imaginary parts of two complex number objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Operator Overloading

Operator overloading allows operators like + and - to be used with user-defined types by extending their functionality. It is a type of polymorphism that allows expressions to be built using objects. The general syntax for operator overloading defines a method with the operator as the name that takes an object as a parameter and returns the result. For example, the + operator can be overloaded for a complex number class to add the real and imaginary parts of two complex number objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Operators overloading

S.S.R.K.M.GUPTA

Expression :
An expression is a statement which
has the combination of operators and
operands arranged in a meaningful
way.
Operator overloading:
Operator overloading is a concept
and it is a type of polymorphism
which provides to build expressions
using objects as objects.

Operator overloading
Generally we can use operators with predefined data
types only, while constructing the expressions.
i.e., the operations of a particular operator is limited
to (or) restricted to predefined data types only.
So, we cannot write expressions with user-defined
data types as operands.
Operator overloading facilitates us to extending the
operation for user defined data types also.
Providing additional functionality for an operator,
working with user defined data types or objects is
known as operator overloading.

General syntax for operator overloading


The general syntax for operator overloading is:
<return type> operator <symbol>(<class type obj>)
{
// code
}
Ex:
cno oprator + (cno obj)
{
float r = rpart + obj2.rpart;
float i = ipart + obj2.ipart;
return cno(r,i);
}
Usage :
c1 + c2 => c1.operator + (c2)

You might also like