2. Introduction
It gives additional meaning to the C++ operator
when applied to the user defined data types.
We can create our own language by using the
concept of operator overloading appropriately.
It require great care when misused program became
very difficult to understand
Compiled By: Kamal Acharya
3. Operator Overloading Restriction
Following C++ Operator can’t be overloaded
Class member access operators(. & .*)
Scope Resolution Operator(::)
Sizeof Operator(sizeof())
Conditional Operator(? :)
Precedence of an operator cannot be changed
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
Compiled By: Kamal Acharya
4. No new operators can be created
Use only existing operators
No overloading operators for built-in types
Cannot change how two integers are added
Compiled By: Kamal Acharya
5. General Rules for overloading Operator
Syntax:
returnType classname::operator op(arguments)
{
body;
}
Example
void Integer::operator op()
{
Body;
}
Compiled By: Kamal Acharya
6. Steps
Create a class that is to be used
2. Declare the operator function in the public part of
the class. It may be either member function or
friend function.
3. Define operator function to implement the
operation required
4. Overloaded can be invoked using the syntax such
as:
op x;
1.
Compiled By: Kamal Acharya
7. Overloading Unary Operator
Unary operator are those operator which works only
on the single operand. Eg. ++, --, - etc
Unary operator acts on only one operand and can be
overloaded in two ways:
1.
2.
Using non-static member function with no arguments
Using friend function with one argument where the
argument must be either an object of the class or an
reference to an object of the class
Compiled By: Kamal Acharya
8. Using non static member function
Example
class Test
{
…………
public:
void operator op()
{……… }
};
Compiled By: Kamal Acharya
Void main()
{
…….
…….
op obj1; /* Same as
obj1.operator
op()*/
………………..
}
12. Using Friend Function
Example
class Test
{
…………
public:
friend void operator
op(Test);
};
Compiled By: Kamal Acharya
Void main()
{
…….
…….
op obj1; /* Same as
operator op(obj1)*/
………………..
}
16. Overloading Binary Operator
Binary operator are those operator which works on
two operands. Eg. +, -,*,/ etc
Binary operator acts on two operands and can be
overloaded in two ways:
1.
2.
Using non-static member function with single argument.
Using friend function with two arguments where the
arguments must be either an object of the class or an
reference to an object of the class.
Compiled By: Kamal Acharya
17. Using non static member function
Example
class Test
{
…………
public:
void operator op(Test)
{……… }
};
Compiled By: Kamal Acharya
Void main()
{
…….
…….
obj1 op obj2; /* Same as
obj1.operator op(obj2)*/
………………..
}
21. Using Friend Function
Example
class Test
{
…………
public:
friend void operator
op(Test, Test);
};
Compiled By: Kamal Acharya
Void main()
{
…….
…….
obj1 op obj2; /* Same as
operator op(obj1, obj2)*/
………………..
}