what is operator overloading? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is operator overloading?

29th Sep 2016, 10:12 AM
Susmita Pal
Susmita Pal - avatar
3 Answers
+ 1
The meaning of operators are already defined and fixed for basic types like: int, float, double etc in C++ language. For example: If you want to add two integers then, + operator is used. But, for user-defined types(like: objects), you can define the meaning of operator, i.e, you can redefine the way that operator works. For example: If there are two objects of a class that contain string as its data member, you can use + operator to concatenate two strings. Suppose, instead of strings if that class contains integer data member, then you can use + operator to add integers. This feature in C++ programming that allows programmer to redefine the meaning of operator when they operate on class objects is known as operator overloading.
29th Sep 2016, 11:09 AM
Oliver Brown
Oliver Brown - avatar
0
#include<iostream. h> #include<conio. h> class test { int a, b; public: test(void) { a=10; b=20; } void print(void) { cout<<"\na="<<a<<"\nb="<<b; } test operator +(test); }; test test:: operator +(test t) { TEST TT; TT. A=A+T. A; TT. B=B+T. B; RETURN (TT); } void main() { clrscr(); test result, t1, t2; result=t1+t2; t1. print(); t2. print(); result. print(); getch(); } in this code some part are written in capital word... please explain me this area.... i can not understand what actually happening there.......
29th Sep 2016, 1:32 PM
Susmita Pal
Susmita Pal - avatar
0
You create a new Test object, assign its data values and return it. So in main if I made two test objects Test A Test B Test C it would be called like C.operator+(A,B) or what most people prefer C = A + B
1st Oct 2016, 9:56 PM
Bailey Kocin
Bailey Kocin - avatar