ADS

Operator overloading and its types in C++

Operator Overloading

It is the mechanism of giving special meanings to an operator. By overloading operators, we can give additional meanings to operators like +, *, -, <=, >= etc. which by default are supposed to work only on standard data types like ints, floats.


In general,  a = b + c; works only with basic types like ‘int’ and ‘float’, and attempting to apply it when a, b and c are objects of a user-defined class will cause complaints from the compiler. But, using overloading, we can make this statement legal even when a, b and c are user-defined types (objects).

Even though the semantics of an operator can be expressed, we cannot change its syntax. When an operator overloaded, its original meaning is not lost. The grammar rules defined by C++ that govern its use such as the number of operands, precedence and associatively of the operator remains the same for overloaded operators. 
We can overload (give additional meanings to) all the C++ operators except the following:
  • Class member access operators (. , .*)
  • Scope resolution operator (::)
  • Size of operator (sizeof)
  • Conditional operator (?:)

There are two types of operator overloading
  • Unary operator overloading and
  • Binary operator overloading


Unary operator overloading


Unary operators are those operators that act on a single operand. ++, -- are unary operators. The following program overloads the ++ unary operator for the distance class to increment the data number by one.

class Distance
int feet; 
float inch; 
public: 
Distance (int f, float i);
 void operator++(void); 
void display();
};
Distance :: Distance (int f, float i)
feet = f ; inch = i;
}
void Distance :: display()
cout<<”Distance in feet”< 
cout<<”Distance in inch”<
}
void Distance :: operator ++(void)
feet++;  inch++;
}
void main()
Distance dist(10,10); 
++dist; 
dist.display();
}

In the above example, the ++ unary operator has been overloaded in the function void Distance :: operator ++(void). In this overloaded function, data members feet and inch are increased by one. This function is called at the second line ‘++dist’ in the main.
General syntax for defining operator overloading

return-type   
classname :: operator  operator-to-overload (arg. list)
//func body
}

The keyword ‘operator’ is used to overload an operator. This declaration tells the compiler to call this member function whenever the ++ operator is encountered, provided the operands are of the user-defined type.

//Another Example - overloading unary minus operator


class abc
int x,y,z; 
public: 
void getd/ata(int a,int b,int c) 
{  
x = a;  
y = b;  
z = c; 
void display() 
{  
cout< 
void operator -();
};
 void abc::operator-()
{
 x = -x;
y = -y; 
z = -z;
}

main()
abc a; 
a.getdata(4,-5,6); 
a.display();    
-a; 
a.display(); 
getch(); 
return 0;
}

Overloading Unary operator that return a value:
If we must use overloaded operator function for return a value as:
 Obj2=obj1++;  //returned object of obj++ is assigned to //obj2 

 

 //Example: unary operator overloading  with return type.

class index
int count;
public: 
index() 
{  
count=0; 
void display() 
{  
cout< 
index operator++() 
{  
++count;  
index temp;  
temp.count=count;  
return temp; 
} };
int main()
clrscr(); 
index c,d; 
cout< 
c.display();  ++c; 
cout< 
c.display();  d=++c; 
cout< 
c.display(); 
cout< 
d.display(); 
getch(); 
return 0;
}

Here the operator ++ ( ) function increments the count in its own objects as before, then creates the new temp object and assigns count in the new object the same value as in its own object. Finally it returns the temp object. This has the desired effect. Expression like ++c now return a value, so they can be used in other expressions such as d= ++c;
In this case the value returned from ++c is assigned to d.


Program’s output would look like this: 
C=0 
C=1 
C=2 
D=2  
In our program we created a temporary object called temp. Its purpose was to provide a return value for the ++ operator. We could have achieved the same effect using the nameless temporary object.

 
Rules for overloading operators

  • Only existing operators can be overloaded. New operators cannot be created.
  • The overloaded operator must have at least one operand that is of user-defined type.
  • We cannot change the meaning of an operator. That is, we cannot redefine the plus (+) operator to subtract one value from others.
  • Overloaded operators follow the syntax rules of the original operators. That cannot be overridden.
  • Friend functions cannot be used to overload certain operators like =, ( ), [ ] and >.
  • Binary operators such as +, -, *, and / must explicitly return a value
  • As described above, all operators cannot be overloaded.
  • Unary operators overloaded by means of a member function take no explicit arguments and return no explicit values. But, those overloaded by means of friend function take one argument.
  • Binary operators overloaded by means of a member function take one explicit argument. But, those overloaded by means of friend function take two arguments.
  • The process of operator overloading generally involves following steps.
  • Declare a class whose objects are to be manipulated using operators.
  • Declare the operator function, in public part of class. It can either normal member function or friend function.
  • Define operator function within the body of a class or outside the body of the class but function prototype must be inside the class body.

    Post a Comment

    0 Comments