Saturday, 2 October 2010

Operator overloading

Prerequisite for operator overloading    is we should know about friend function


Overloading an operator means programming an operator to work on operands of types it has not been designed to operate.
Overloading an operator can be done in 2 ways:
1)      By using member functions

Syntax using member functions:

class < class_name >
{
< return_type > operator < op > (<arg_list>);                        //prototype
};
< return_type> < class_name >:: operator <op> (<arg_list>)           // definition
{
//function body
}

Syntax using friend functions:

class < class_name >
{
friend < return_ type > operator < op > (<arg_ list> );       //prototype
};
< return_type> operator <op> (<arg list>)                                      //definition
{
//function body
}





No comments:

Post a Comment