Page 148 - DCAP404 _Object Oriented Programming
P. 148
Unit 7: Operator Overloading
4. Binary operators overloaded through a member function take one explicit argument and Notes
those which are overloaded through a friend function take two explicit arguments.
Table 7.2
Operator to Overload Arguments passed to the Argument passed to the Friend
Member Function Function
Unary Operator No 1
Binary Operator 1 2
5. Overloaded operators must either be a non-static class member function or a global function.
A global function that needs access to private or protected class members must be declared
as a friend of that class. A global function must take at least one argument that is of class
or enumerated type or that is a reference to a class or enumerated type.
Example:
class Point
{
public:
Point operator<( Point & ); // Declare a member operator
// overload.
// Declare addition operators.
friend Point operator+( Point&, int );
friend Point operator+( int, Point& );
};
int main()
{
}
7.3 Overloading Unary Operators
In case of unary operator overloaded using a member function no argument is passed to the
function whereas in case of a friend function a single argument must be passed.
Following program overloads the unary operator to negate an object. The operator function
defined outside the class negates the individual data members of the class integer.
#include <iostream.h>
#include <conio.h>
class integer
{
int x,y,z;
public:
void getdata(int a, int b, int c);
LOVELY PROFESSIONAL UNIVERSITY 141