Operator Overloading In C++
Operator Overloading :
cout << a;
}
This is effective with integer arguments only. Overloading it will make it available for other types, such as char and float.
void printNumber(char a) {
cout << a;
}
Now the same printNumber function works for both int and char.
You can consider the function as the job and the data types int and char as difference in the same job.(Both functions are doing the same job ( printing the values ) but one is printing int value and the other is printing char value).
When overloading function, the definition of the function must differ from each other by the types and/or the number of arguments in the argument list.
#include <iostream>
using namespace std;
void printNumber(int x)
{
cout << "integer: " << x << endl;
}
void printNumber(float x)
{
cout << "float: " << x << endl;
}
int main() {
int a = 18;
float b = 34.267;
printNumber(a);
printNumber(b);
}
Output: integer: 18 float: 34.267
int printName(int a) { } float printName(int b) { } double printName(int c) { }
int printName(int a) { } float printName(float b) { } double printName(double c) { }
Now let's move our attention towards operator overloading.
Operator overloading is just like function overloading. Like an overloaded function that do multiple tasks an overloaded operator also do multiple tasks.
Operator Overloading of unary operator:
#include<iostream>using namespace std;class Demo{private:int a,b;public:void setData(int x,int y){a=x;b=y;}void showData(){cout<<"a:"<<a<<"b:"<<b<<endl;}Demo operator-(){Demo temp;temp.a=-a;temp.b=-b;return temp;}};int main(){Demo c1,c2;c1.setData(3,4);c2=c1.operator-(); // can also be written as: "c2=-c1" means c1 called - operator and passed no parameter. some people say that using - first is not correct.//but if we want negation of some value do we add - at first or last. we write like "x=-3" not "x=3-" so - will come first.c2.showData();return 0;}This program outputs as:
a:-3b:-4Operator overloading of unary operator ( increment operator ++ ) pre and post:
#include <iostream>using namespace std;
class Time{
public:
int hour,minute,seconds;
// constructor to assign values
Time(int h,int m,int s){
hour=h;
minute=m;
seconds=s;
}
// To Show Time
void show(){
cout<<"Time is:"<<endl<<hour<<":"<<minute<<":"<<seconds<<endl;
}
// overloaded unary ++ operator
int operator++(){
minute=++minute;
return minute;
}
// overloaded unary -- operator
int operator--(){
minute=--minute;
return minute;
}
};
int main() {
int x,p;
Time t(12,23,20);
cout<<"Before:"<<endl;
t.show();
x=t.operator++();
t.minute=x;
cout<<"Minute decrement:"<<endl;
t.show();
p=t.operator--();
t.minute=p;
cout<<"Minute again increment:"<<endl;
t.show();
return 0;
}





Nicely explained. ...
ReplyDeleteHelpful ,
Thankyou
Useful information
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteGood explanation
ReplyDeleteInformative 👍
ReplyDeletenicely explained
ReplyDeletevery helpful
interesting information
Nicely Explained....
ReplyDeleteAnd help me very effectively.
Thank you. 😊
Informative
ReplyDeleteNice
Nicely explained. Got the concept.
ReplyDelete