0% found this document useful (0 votes)
6 views5 pages

Operators Overloading & Type Conversion

The document explains operator overloading in C++, allowing user-defined types to use standard operator syntax. It details the process of defining operator functions, the rules for overloading operators, and provides examples for both unary and binary operators. Additionally, it specifies operators that cannot be overloaded and the differences between member functions and friend functions in this context.

Uploaded by

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

Operators Overloading & Type Conversion

The document explains operator overloading in C++, allowing user-defined types to use standard operator syntax. It details the process of defining operator functions, the rules for overloading operators, and provides examples for both unary and binary operators. Additionally, it specifies operators that cannot be overloaded and the differences between member functions and friend functions in this context.

Uploaded by

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

OPERATORS OVERLOADING AND TYPE CONVERSION

C++ allows to add two variables of user defined types with the same syntax that is
applied to the basic types. This means that C++ has the ability to provide the operators with a
special meaning for a data type. The mechanism of giving such special meanings to an
operator is known as operator overloading.
Operator overloading provides a flexible option for the creation of new definitions for
most of C++ operators. We can overload all the C++ operators except the following :-
1. Class member access operator (. , .*)
2. Scope resolution Operator ( : :)
3. Size operator ( Size of )
4. Conditional Operator ( ? : )
Although the semantics of an operator can be extended, we cannot change its syntax, the
grammatical rules that govern its use such as the number of operands, precedence and
associativity.

DEFINING OPERATOR OVERLOADING


This is done with the help of a special function called operator function. Which
describes the task. The general form of an operator function is :-

Return type classname : : operator ( Op.argulist)


{
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.operatio OP is the
function name.
Operator function must be either member function ( non static) or friend functions. A
basic difference b/w them is that a friend function will have only one argument for unary
operators and two for binary operators. While a member function has no arguments for unary
operators and only one for binary operators.
THE PROCESS OF OVERLOADING INVOLVES THE FOLLOWING STEPS
1. Creates a class that defines the data type that is obtained in the overloading
operation.
2. Declare the operator function operator Op( ) in the public part of the class.
3. Define the operator function to implement the required operation.

Overloaded operator functions can be invoked by expressions such as :-


Op x or x Op.
For unary operator and
x Op y
for binary operator

Op x ( or x.Op) would be interpreted as operator Op(x).


For friend function.

OVERLOADING UNARY OPERATORS


A minus operator when used as a unary takes one operands. We know that this
operator changes the sign of an operand when applied to a basic data item.
The unary minus when applied to an object should change the sign of each of its data
items.
The functions operator ( ) takes no arguments. Then what does this operator function
do ? it changes the sign of data members of the object S. Since the function is a member
function of the same class, it can directly access the members of the object which activated it.
e.g.
S2 = - S1;
Will not work because, the function operator – ( ) does not return any value. It can
work if the function is modified to return the object.
It is possible to overload a unary minus operator using a friend function as follows :-
Friend void operator – (Space &S);
void operator – (Space &S);
{
S.x = - S.x;
S.y = - S.y;
S.z = - S.z;
}
Note that the argument is passed by reference. It will not work if we pass the
argument by value because only a copy of the object that activated the call is passed to
operator – ( ). Therefore the changes made inside the operator function will not reflect in the
called object.

OVERLOADING BINARY OPERATORS


e.g. To add two complex numbers using a friend function. A statement like :
C = Sum ( A, B); // Functional Notation.
Or C = A + B // Arithmetic Notation. Can be used by overloading the + operator using an
operator +( ) function.

E.g. Complex complex : : operator +(complex)


{
Complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return (temp);
}
The following features of this function are :-
1. It receives only one complex type argument explicitly.
2. It returns a complex type value.
3. It is a member function of complex.

The function is expected to add two complex values and return a complex value as the
result but receives only one value as argument. Where does the other value come from.
C3 = C1 + C2; //invokes operator + ( ) function.
We know that a member function can be invoked only by an object of the same class.
Here the object C1 takes the responsibility of invoking the function and C2 Plays the role of
an argument that is passed to the function. The above invocation statement is equivalent to

C3 = C1.operator + (C2) // usual function call.


Therefore in the operator + ( ) function, the data member of C1 are accessed directly
and the data members of C2 (that is passed as an argument) are accessed using the dot
operator. Thus both the objects are available for the function.
e.g.
Temp.x = x + c.x;
As a rule, in overloading of binary operators, the left hand operand is used to invoke
the operator function and the right hand operand is passed as an argument.
When we use a class name with an argument list the compiler invokes an appropriate
constructor, initializes an object with no name and return the contents for copying into an
object. Such an object is called a temporary object and goes out of space as soon as the
contents are assigned to another object. Using temporary objects can make the code shorter,
more efficient and better to read.

RULES FOR OVERLOADING OPERATOR


1. Only existing operators can be overloaded New operator cannot be created.
2. The overloaded operator must have at-least one operand that is of user defined
type.
3. We cannot redefine the plus (+) operator to subtract one value from the other.
4. Overloaded operators follow the syntax rules of the original operators. They
cannot be over ridden.
5. 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
functions, take one reference argument.
6. Binary operators overloaded through a member function take one explicit
argument and those which are overloaded through a friend function take two
explicit arguments.
7. When using binary operators, overloaded through a member function, the left
hand operand must be an object of the relevant class.
8. Binary arithmetic operators such as + , - , * , and / must explicitly return a value.
They must not attempt to change their own arguments.
9. The following operators cannot be overloaded :-
(i) Size of operator
(ii) Membership operator :- .
(iii) Pointer to member :- .*
(iv) Scope resolution operator :- ::
(v) Conditional Operator :- ?:

You might also like