Operator Overloading in C++ With Example

Posted on

Operator Overloading

Operator Overloading

Operator overloading is a crucial idea in C++. It’s a sort of polymorphism during which an operator is overloaded to offer consumer outlined which means to it. Overloaded operator is used to carry out operation on user-defined knowledge sort. For instance ‘+’ operator will be overloaded to carry out addition on varied knowledge varieties, like for Integer, String(concatenation) and so forth.

operator overloading - Operator Overloading in C++ With Example

Virtually any operator will be overloaded in C++. Nevertheless there are few operator which cannot be overloaded. Operator that aren’t overloaded are follows

  • scope operator – ::
  • sizeof
  • member selector – .
  • member pointer selector – *
  • ternary operator – ?:

Operator Overloading Syntax

operator overloading syntax - Operator Overloading in C++ With Example


Implementing Operator Overloading

Operator overloading will be completed by implementing a operate which will be :

  1. Member Perform
  2. Non-Member Perform
  3. Pal Perform

Operator overloading operate generally is a member operate if the Left operand is an Object of that class, but when the Left operand is completely different, then Operator overloading operate have to be a non-member operate.

Operator overloading operate will be made pal operate if it wants entry to the personal and guarded members of sophistication.


Restrictions on Operator Overloading

Following are some restrictions to be stored in thoughts whereas implementing operator overloading.

  1. Priority and Associativity of an operator can’t be modified.
  2. Arity (numbers of Operands) can’t be modified. Unary operator stays unary, binary stays binary and so forth.
  3. No new operators will be created, solely current operators will be overloaded.
  4. Can’t redefine the which means of a process. You can not change how integers are added.

Operator Overloading Examples

Virtually all of the operators will be overloaded in infinite alternative ways. Following are some examples to study extra about operator overloading. All of the examples are carefully linked.


Overloading Arithmetic Operator

Arithmetic operator are mostly used operator in C++. Virtually all arithmetic operator will be overloaded to carry out arithmetic operation on user-defined knowledge sort. Within the under instance now we have overridden the + operator, so as to add to Time(hh:mm:ss) objects.


Instance: overloading ‘+’ Operator so as to add two time object

#embody< iostream.h>  #embody< conio.h>  class time  { int h,m,s; public:time(){ h=0, m=0; s=0;}void getTime();void present()  { cout<< h<< ":"<< m<< ":"<< s;} time operator+(time); //overloading '+' operator};  time time::operator+(time t1)//operator operate  { 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::getTime()  {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;  } void predominant()  { clrscr(); time t1,t2,t3; cout<<"n Enter the primary time "; t1.getTime(); cout<<"n Enter the second time "; t2.getTime(); t3=t1+t2;//including of two time object utilizing '+' operator cout<<"n First time "; t1.present(); cout<<"n Second time "; t2.present(); cout<<"n Sum of instances "; t3.present(); getch();  } 

Overloading I/O operator

  • Overloaded to carry out enter/output for consumer outlined datatypes.
  • Left Operand will probably be of varieties ostream& and istream&
  • Perform overloading this operator have to be a Non-Member operate as a result of left operand is just not an Object of the category.
  • It have to be a pal operate to entry personal knowledge members.

You may have seen above that << operator is overloaded with ostream class object cout to print primitive sort worth output to the display. Equally you'll be able to overload << operator in your class to print user-defined sort to display. For instance we are going to overload << in time class to show time object utilizing cout.

time t1(3,15,48);cout << t1;

NOTE: When the operator doesn't modify its operands, the easiest way to overload the operator is through pal operate.


Instance: overloading ‘<<‘ Operator to print time object

#embody< iostream.h>  #embody< conio.h>  class time  { int hr,min,sec; public:time(){ hr=0, min=0; sec=0;}  time(int h,int m, int s){ hr=h, min=m; sec=s;} pal ostream& operator << (ostream &out, time &tm);  //overloading '<<' operator};ostream& operator<< (ostream &out, time &tm)//operator operate { out << "Time is " << tm.hr << "hour : " << tm.min << "min : " << tm.sec << "sec";  return out;}void predominant(){ time tm(3,15,45); cout << tm;}

Output

Time is 3 hour : 15 min : 45 sec 

Overloading Relational operator

You may also overload Relational operator like == , != , >= , <= and so forth. to check two user-defined object.


Instance

class time  {  int hr,min,sec; public:time(){ hr=0, min=0; sec=0;}  time(int h,int m, int s){ hr=h, min=m; sec=s;} pal bool operator==(time &t1, time &t2); //overloading '==' operator};bool operator== (time &t1, time &t2)  //operator operate  {return ( t1.hr == t2.hr && t1.min == t2.min &&  t1.sec == t2.sec );} 

Copy constructor Vs. Task operator

Task operator is used to repeat the values from one object to a different already current object. For instance

time tm(3,15,45);//tm object created and initializedtime t1;  //t1 object createdt1 = tm; //initializing t1 utilizing tm

Copy constructor is a particular constructor that initializes a new object from an current object.

time tm(3,15,45);//tm object created and initializedtime t1(tm);  //t1 object created and initialized utilizing tm object