Operator Overloading
Operator Overloading
Operator overloading is a crucial idea in C++. It’s a kind of polymorphism during which an operator is overloaded to provide consumer outlined that means to it. Overloaded operator is used to carry out operation on user-defined knowledge kind. For instance ‘+’ operator may be overloaded to carry out addition on numerous knowledge sorts, like for Integer, String(concatenation) and many others.
Nearly any operator may be overloaded in C++. Nonetheless there are few operator which can’t be overloaded. Operator that aren’t overloaded are follows
- scope operator –
::
sizeof
- member selector –
.
- member pointer selector –
*
- ternary operator –
?:
Operator Overloading Syntax
Implementing Operator Overloading
Operator overloading may be executed by implementing a operate which may be :
- Member Operate
- Non-Member Operate
- Buddy Operate
Operator overloading operate generally is a member operate if the Left operand is an Object of that class, but when the Left operand is totally different, then Operator overloading operate have to be a non-member operate.
Operator overloading operate may 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 saved in thoughts whereas implementing operator overloading.
- Priority and Associativity of an operator can’t be modified.
- Arity (numbers of Operands) can’t be modified. Unary operator stays unary, binary stays binary and many others.
- No new operators may be created, solely current operators may be overloaded.
- Can not redefine the that means of a process. You can’t change how integers are added.
Operator Overloading Examples
Nearly all of the operators may be overloaded in infinite alternative ways. Following are some examples to study extra about operator overloading. All of the examples are carefully related.
Overloading Arithmetic Operator
Arithmetic operator are mostly used operator in C++. Nearly all arithmetic operator may be overloaded to carry out arithmetic operation on user-defined knowledge kind. Within the under instance we have now overridden the + operator, so as to add to Time(hh:mm:ss) objects.
Instance: overloading ‘+’ Operator so as to add two time object
1 |
#embrace< iostream.h> #embrace< conio.h> class time { int h,m,s; public:time(){ h=0, m=0; s=0;}void getTime();void present() { cout<< h<< ":"<< m<< ":"<< s;} <b>time operator+(time);</b> <span style="color: green;">//overloading '+' operator</span>}; <b>time time::operator+(time t1)</b><span style="color: green;">//operator operate</span> { 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 foremost() { clrscr(); time t1,t2,t3; cout<<"n Enter the primary time "; t1.getTime(); cout<<"n Enter the second time "; t2.getTime(); t3=t1+t2;<span style="color: green;">//including of two time object utilizing '+' operator</span> 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 can be of sorts
ostream&
andistream&
- Operate overloading this operator have to be a Non-Member operate as a result of left operand will not be an Object of the category.
- It have to be a pal operate to entry personal knowledge members.
You could have seen above that << operator is overloaded with ostream class object cout
to print primitive kind worth output to the display screen. Equally you may overload << operator in your class to print user-defined kind to display screen. For instance we’ll overload << in time class to show time object utilizing cout
.
1 |
time t1(3,15,48);cout << t1; |
NOTE: When the operator doesn’t modify its operands, the easiest way to overload the operator is by way of pal operate.
Instance: overloading ‘<<‘ Operator to print time object
1 |
#embrace< iostream.h> #embrace< 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;} <b>pal ostream& operator << (ostream &out, time &tm);</b> //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 foremost(){ time tm(3,15,45); cout << tm;} |
Output
1 |
Time is 3 hour : 15 min : 45 sec |
Overloading Relational operator
You can even overload Relational operator like ==
, !=
, >=
, <=
and many others. to match two user-defined object.
Instance
1 |
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;} <b>pal bool operator==(time &t1, time &t2);</b> <span style="color: green;">//overloading '==' operator</span>};<b>bool operator== (time &t1, time &t2)</b> <span style="color: green;">//operator operate</span> {return ( t1.hr == t2.hr && t1.min == t2.min && t1.sec == t2.sec );} |
Copy constructor Vs. Project operator
Project operator is used to repeat the values from one object to a different already current object. For instance
1 |
time tm(3,15,45);<span style="color: green;">//<b>tm</b> object created and initialized</span>time t1; <span style="color: green;">//<b>t1</b> object created</span>t1 = tm; <span style="color: green;">//initializing <b>t1</b> utilizing <b>tm</b></span> |
Copy constructor is a particular constructor that initializes a new object from an current object.
1 |
time tm(3,15,45);<span style="color: green;">//<b>tm</b> object created and initialized</span>time t1(tm); <span style="color: green;">//<b>t1</b> object created and initialized utilizing <b>tm</b> object</span> |
- ← Prev
- Subsequent →