Operator Overloading C++





In C++ the overloading principle applies not only to functions, but to operators too. That is, of operators can be extended to work not just with built-in types but also classes.

  • Operator overloading means giving capability to the operator to work on different types of operand.

  • The operators +, * etc, works with type of int, float etc. We can overload these operators by giving them the capability to work on objects.

  • This done with the help special function called operator function.

    Syntax:

    
    return_type operator operator-symbol ( parameter-list )
     {
        //function body
     }
                                

Rules to Operator Overload :
  1. Only existing operator can be overload. New operator cannot be created.

  2. You cannot redefine the meaning of operators when applied to built-in data types.

  3. Precedence and Associativity of an operator cannot be changed. However, if you want to change the order of evaluation, parenthesis should be used.

  4. Arity (numbers of Operands) cannot be changed. Unary operator remains unary, binary remains binary etc.

  5. Overloaded operators cannot have default arguments.

  6. All overloaded operators except assignment (operator=) are inherited by derived classes.

  7. Few operators are restricted to overload, means we cannot overload them:

    • The size of operator ( sizeof ).
    • The member selection operator ( . ).
    • The pointer to member operator ( . * ).
    • The scope resolution operator ( :: ).
    • The conditional operator ( ? : ).
    • Preprocessor convert to string ( # ).
    • Preprocessor concatenate ( ## ).

  8. 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.


Operator Overloading With Member Function :
  • A member function has no arguments for unary operators.

  • A member function should have only only one argument for binary operators.

  • Arguments may be passed either by value or pass by reference ( & ).

Code Example: overloading '+' Operator to add two time object
#include<iostream>  
using namespace std;
class time  
{  
 int h,m,s;  
 public:  
 time()  
 {  
  h=0, m=0; s=0;  
 }  
 void setTime();  
 void show()
 {
  cout<< h<< ":"<< m<< ":"<< s;  
 } 
//overloading '+' operator 
 time operator+(time);   
};  
//operator function
time time::operator+(time t1)	  
{  
 time t;  
 int a,b;  
 a=s+t1.s;  
 t.s=a%60;  
 b=(a/60)+m+t1.m;  
 t.m=b%60;  
 t.h=(b/60)+h+t1.h;  
 t.h=t.h%12;  
 return t;  
} 
void time::setTime()  
{  
 cout<<"\n Enter the hour(0-11) ";  
 cin>>h;  
 cout<<"\n Enter the minute(0-59) ";  
 cin>>m;  
 cout<<"\n Enter the second(0-59) ";  
 cin>>s;  
} 
int main()  
{    
 time t1,t2,t3;  
 cout<<"\n Enter the first time ";  
 t1.setTime();  
 cout<<"\n Enter the second time ";  
 t2.setTime();
//adding of two time object using '+' operator   
 t3=t1+t2;	
 cout<<"\n First time ";  
 t1.show();  
 cout<<"\n Second time ";  
 t2.show();  
 cout<<"\n Sum of times ";  
 t3.show();  
 return 0; 
}                         
                    
OUTPUT:
Enter the first time
Enter the hour(0-11) 4

Enter the minute(0-59) 34

Enter the second(0-59) 24

Enter the second time
Enter the hour(0-11) 2

Enter the minute(0-59) 25

Enter the second(0-59) 32

First time 4:34:24
Second time 2:25:32
Sum of times 6:59:56               
              

Operator Overloading With Global/Friend Function :
  • It will take one argument for unary operator.

  • It will take two argument for binary operator.

  • Arguments may be passed either by value or pass by reference ( & ).

#include<iostream>
using namespace std;
class time  
{  
 int h,m,s;  
 public:  
 time()  
 {  
  h=0, m=0; s=0;  
 }  
 void setTime();  
 void show()
 {
  cout<< h<< ":"<< m<< ":"<< s;  
 } 
 //overloading '+' operator 
 time friend operator+(time &, time &);
};
//operator function    
time operator+(time &t1,time &t2)
{  
 time t;  
 int a,b;  
 a=t1.s+t2.s;  
 t.s=a%60;  
 b=(a/60)+t1.m+t2.m;  
 t.m=b%60;  
 t.h=(b/60)+t1.h+t2.h;  
 t.h=t.h%12;  
 return t;  
} 
void time::setTime()  
{  
 cout<<"\n Enter the hour(0-11) ";  
 cin>>h;  
 cout<<"\n Enter the minute(0-59) ";  
 cin>>m;  
 cout<<"\n Enter the second(0-59) ";  
 cin>>s;  
} 
int main()  
{    
 time t1,t2,t3;  
 cout<<"\n Enter the first time ";  
 t1.setTime();  
 cout<<"\n Enter the second time ";  
 t2.setTime();
 //adding of two time object using '+' operator  
 t3=t1+t2;	  
 cout<<"\n First time ";  
 t1.show();  
 cout<<"\n Second time ";  
 t2.show();  
 cout<<"\n Sum of times ";  
 t3.show();  
 return 0; 
}                        
                    
OUTPUT:
Enter the first time
Enter the hour(0-11) 4

Enter the minute(0-59) 34

Enter the second(0-59) 24

Enter the second time
Enter the hour(0-11) 2

Enter the minute(0-59) 25

Enter the second(0-59) 32

First time 4:34:24
Second time 2:25:32
Sum of times 6:59:56               
                    

Next topic is Constructor





 






Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved