+ 4

What is operator overloading?

hi there, please help me out with the logical part of the overloading concept. thanks

23rd Mar 2017, 11:05 PM
Akshayarekha
Akshayarekha - avatar
4 Answers
+ 2
Hi! To overload an operator, for example '+', means to define how it works on different types. Example: It is clear how '+' operates on two numbers: int x=1; int y=2; int z=x+y; // z is equal to 3 But now imagine you have defined a class called myFood and you create: myFood apple; myFood banana; If you now try to use '+' for something like apple + banana you will no get an error because the program does not know how to use '+' on myFood objects. Parameter overloading gives you the possibility to define what the program should do in such cases. The following website has a nice description of the technical details for operator overloading and the related concept of function overloading: https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm Hope that helps! :)
24th Mar 2017, 12:06 AM
Denis Lebedev
Denis Lebedev - avatar
+ 2
Overloading Operator..... C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation). When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution. *************************************************************************** Function overloading in C++: You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type. Following is the example where same function print() is being used to print different data types: #include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return 0; } When the above code is compiled and executed, it produces the following result: Printing int: 5 Printing float: 500.263 Prin
25th Mar 2017, 12:34 AM
Syeda Juveria Afreen
Syeda Juveria Afreen - avatar
+ 1
in short opretor name is same but thir given value is diffrent its called opretor overloading
1st Mar 2019, 6:24 PM
Fenil Ajmera
Fenil Ajmera - avatar
0
It can be used all over the place, but a common example is using operator overloads for math symbols inside a class for a set of x, y, and z coordinates, which allows you to do things with coordinate objects like point1 = point2 + point3*5; Here the overloaded * operator would multiply each value in point3 by 5 and return a new object with the result. Then the overloaded + operator would add point2 to the previous result and return a new object with the x, y, and z variables each added together. The overloaded = operator would then take that resulting coordinate from all the math and assign its values into point1. Overloading the operators takes a little work, but for certain classes like this it can make the rest of your code much easier to read and write.
5th Mar 2019, 7:02 AM
Sparrow Elle
Sparrow Elle - avatar