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

Operator Overloading: Dr.S.Veena, Associate Professor/CSE

Operator overloading allows operators to work with user-defined types by defining operator functions. It is implemented by adding special member functions called operator functions to a class. Operator functions can be member functions or friend functions. Only existing operators can be overloaded, and their basic meanings cannot be changed. Common operators like +, -, *, / must return a value when overloaded. Certain operators like sizeof cannot be overloaded at all.

Uploaded by

vdjohn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Operator Overloading: Dr.S.Veena, Associate Professor/CSE

Operator overloading allows operators to work with user-defined types by defining operator functions. It is implemented by adding special member functions called operator functions to a class. Operator functions can be member functions or friend functions. Only existing operators can be overloaded, and their basic meanings cannot be changed. Common operators like +, -, *, / must return a value when overloaded. Certain operators like sizeof cannot be overloaded at all.

Uploaded by

vdjohn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Operator Overloading

Dr.S.Veena, Associate Professor/CSE 1


Operator Overloading

• Operator Overloading is performed by adding special member functions to the class, which is known as operator
functions. It is used to convert one object into another.
• Defining Operator Overloading
 Operator overloading is done through a special function called operator function. The special form of an operator
function is
retrun-type class-name :: operator op(arg-list)
{
function body // task defined
}
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.

Dr.S.Veena, Associate Professor/CSE


Operator Overloading
Operator Overloading

 Operator functions must be either member functions (non-static) or friend functions.

 A friend function will have only one argument for unary operators and two for binary operators. Arguments
may be passed either by value or by reference.
 A member function has no arguments for unary operators and only one for binary operators. This is because the
object used to invoke the member function is passed implicitly and therefore is available for the member
function.

 Operator functions are declared in the class using prototypes as follows:


•  matrix operator + (matrix); // matrix addition
• matrix operator – ( ); // unary minus
• friend matrix operator + (matrix,matrix); // matrix addition
• friend matrix operator - (matrix); // unary minus
• matrix operator – (matrix &a); // subtraction
• int operator = = (matrix); // comparison
• friend int operator = = (matrix,matrix); // comparision

Dr.S.Veena, Associate Professor/CSE


Operator Overloading

The process of overloading involves the following steps:


1. Create a class that defines the data type that is to be used in the overloaded operation.
2. Declare the operator function operator op( ) in the public part of the class. It may be either a member function
or a friend function.
3. Define the operator function to implement the required operations.

• Operators should follow syntactical rules for the original meaning when overloaded. Following are some
examples of the same
1. All arithmetic operators must return a value. However, it can be of a different type than what is returned to by
original operator.
2. Unary operators, do not have any explicit arguments. If it overloaded as friend, they take one reference
argument.
3. Binary operators take one explicit argument as member and take two explicit arguments as friend.

Dr.S.Veena, Associate Professor/CSE


Rules for overloading operators

 Only existing operators can be overloaded. New operators cannot be created.


 The overloaded operator must have at least one operand that is of user-defined type.
 We cannot change the basic meaning of an operator. That is to say, we cannot redefine the
plus(+) operator to subtract one value from the other.
 Overloaded operators follow the syntax rules of the original operators. They cannot be
overridden.
 There are some operators that cannot be overloaded.
 Only existing operators can be overloaded. New operators cannot be created.
 The overloaded operator must have at least one operand that is of user-defined type.
 We cannot change the basic meaning of an operator. That is to say, we cannot redefine the
plus(+) operator to subtract one value from the other.

Dr.S.Veena, Associate Professor/CSE


Rules for overloading operators

 Overloaded operators follow the syntax rules of the original operators. They cannot be overridden.
 There are some operators that cannot be overloaded.

 We cannot use friend functions to overload certain operators. However, member functions can be used to
overload them.
 Unary operators, overloaded by means of a member function, take no explicit arguments 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 2 explicit arguments.
 When using binary operators overloaded through a member function, the left hand operand must be an
object of the relevant class.

 Binary arithmetic operators such as +, -, *, and / must explicitly return a value. They must not attempt to
change their own arguments.

Dr.S.Veena, Associate Professor/CSE


Operators that cannot be overloaded

Operators Meaning
Sizeof sizeof operator
. Membership operator
.* Point-to-Member
operator
:: Scope Resolution
Operator
?: Conditional operator

Dr.S.Veena, Associate Professor/CSE


Operators that cannot be overloaded
through friend function

Operators Meaning

= Assignment operator

() Function call operator

[] Subscripting operator

-> Class member access


Operator

Dr.S.Veena, Associate Professor/CSE


OPERATOR OVERLOADING THROUGH MEMBER FUNCTION

Dr.S.Veena, Associate Professor/CSE


Binary Operators

Dr.S.Veena, Associate Professor/CSE


Unary Operator overloading using Friend function

Dr.S.Veena, Associate Professor/CSE


Binary Operator Overloading using Friend function

Dr.S.Veena, Associate Professor/CSE


Overloading Assignment Operator

Dr.S.Veena, Associate Professor/CSE

You might also like