4. ⢠In C++ operator overloading is a specific case of polymorphism,
where different operator have different implementations
depending upon their arguments.
⢠Overloading an operator is basically means assigning additional
operation to operator.
⢠C++ allows to define the behaviour of operators when applied
to objects of the class.
⢠Compiler generates appropriate code based on the manner in
which operator is used
5. OPERATORS FUNCTION
⢠Operators are overloaded by creating operators functions.
⢠Operator function defines the operations that are
overloaded operator will perform relative to the class upon
which it will work.
⢠syntax:
6. ⢠To overload unary operator, operator function have single
argument.
⢠To overload binary operator, operator function have two
arguments.
⢠when an operator is used, the operands become the actual
arguments of the âfunction callâ.
⢠operator function can be implemented using function
which can be:
1. Member function
2. Non-Member function
3. Friend function
7. ⢠Operator overloading function can be a member function if
the Left operand is an Object of the class.
⢠Operator overloading function can be non-member
function if the left operand is not a object of the class.
⢠Operator overloading function can be friend function if it
needs access to private and protected members of class
8. #include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{
real = r; imag = i;
}
void print()
{
cout << real << " + i" << imag << endl;
}
Complex operator + (Complex const &obj);
{
Complex res;
res.real=real+obj.real;
res.imag=imag+obj.imag;
return res ;
}
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
return 0;
}
OUTPUT: 12+i9
10. RESTRICTIONS ON OPERATOR OVERLOADING
⢠Precedence and associativity of an operator cannot be
changed.
⢠No new operators can be created.
⢠Number of operands cannot be changed.i.e. Unary
operators remains unary, binary operators remains binary.
⢠Meaning of operator should not be changed.i.e. + should
not mean subtraction.
11. ADVANTAGES OF OPERATOR OVERLOADING
⢠Extensibility: operator act differently depending on the
operands provided.
⢠Not limited to work with primitive data type.
⢠User can easily access the object to perform any operation.
14. DEFINITION
⢠Function Overloading is defined as the process
of having two or more function with the same
name, but different in parameters is known as
function overloading in C++.
⢠Function overloading is commonly used to
create several function of the same name that
perform similar task, but on different data type.
16. WHY OVERLOADING IS USEFUL ?
⢠Function overloading allows function that
conceptually perform the same task on object
of different type to be given same the same
name.
⢠It provides a convenient notation for
manipulating user defined object with
conventional operator.
18. EXAMPLE CONTINUED:
⢠When the above code is compiled and
executed, it produces the following result:
⢠Printing int: 5
⢠Printing float: 500.263
⢠Printing character: Hello C++
19. ADVANTAGES
⢠Overloaded methods give programmers the
flexibility to call a similar method for different
types of data.
⢠Function overloading is done for code
reusability, to save efforts, and also to save
memory.