Programming in C++ – Declaration And Definition Of Overloading

class to which it (the operator) is applied. The operator function helps us in doing so. The Syntax of declaration of an Operator function is as follows:

Operator Operator_name


For example, suppose that we want to declare an Operator function for ‘=’. We can do it as follows:

operator =


A Binary Operator can be defined either a member function taking one argument or a global function taking one arguments. For a Binary Operator X, a X b can be interpreted as either an operator X (b) or operator X (a, b).


For a Prefix unary operator Y, Ya can be interpreted as either a.operator Y ( ) or Operator Y (a).

For a Postfix unary operator Z, aZ can be interpreted as either a.operator Z(int) or Operator (Z(a),int).


The operator functions namely operator=, operator [ ], operator ( ) and operator? must be nonstatic member functions. Due to this, their first operands will be lvalues.


An operator function should be either a member or take at least one class object argument. The operators new and delete need not follow the rule. Also, an operator function, which needs to accept a basic type as its first argument, cannot be a member function.


Some examples of declarations of operator functions are given below:


class P
 P operator ++ (int);//Postfix increment
 P operator ++ ( ); //Prefix increment
 P operator || (P); //Binary OR

Some examples of Global Operator Functions are given below:

P operator – (P); // Prefix Unary minus
P operator – (P, P); // Binary “minus”
P operator - - (P &, int); // Postfix Decrement


We can declare these Global Operator Functions as being friends of any other class.

Examples of operator overloading:

Operator overloading using friend.

Class time
 int r;
 int i;
 friend time operator + (const time &x, const time &y );
 // operator overloading using friend
 time ( ) { r = i = 0;}
 time (int x, int y) {r = x; i = y;}
 time operator + (const time &x, const time &y)
 time z; 
 z.r = x.r +y.r;
 z.i = x.i + y.i;
 return z;

 main ( )
 time x,y,z;
 x = time (5,6);
 y = time (7,8);
 z = time (9, 10);
 z = x+y; // addition using friend function +


Operator overloading using member function:

Class abc
 char * str;
 int len ; // Present length of the string
 int max_length; // (maximum space allocated to string)
 abc ( ); // black string of length 0 of maximum allowed length of size 10.
 abc (const abc &s ) ;// copy constructor
 ~ abc ( ) {delete str;}
 int operator = = (const abc &s ) const; // check for equality
 abc & operator = (const abc &s );
 // overloaded assignment operator
 friend abc operator + (const abc &s1, const abc &s2);
 } // string concatenation
 abc:: abc ()
 max_length = 10;
 str = new char [ max_length];
 len = 0;
 str [0] = ‘\0’;
 abc :: abc (const abc &s )
 len = s. len;
 max_length = s.max_length;
 str = new char [max_length];
 strcpy (str, s.str); // physical copying in the new location.

You may be interested in:
Programming In C MCQs
Programming In C++ MCQs
Object Oriented Programming Using C++ Short Questions Answers