Overloading? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Overloading?

an easier way of understanding the concept of overloading

13th Feb 2017, 1:13 PM
Mich
Mich - avatar
3 Answers
+ 3
If there is a function which was declared two (or more) times with different number of parameters (or types of parameters), just consider it as two different functions which have just the same name. For example, int CalculateAreaOfTriangle (int x1, int y1, int x2, int y2, int x3, int y3); // this is to calculate the area of triangle with 3 coordinators. int CalculateAreaOfTriangle (int height, int width); // this is to calculate the area of triangle with height and width. The purposes of both functions are same hence it is convenient to name them same as "CalculateAreaOfTriangle" (it is easy to remember!) You can use it later for both cases where you have either coordinators or height & width.
17th Apr 2017, 2:28 PM
Yang-jin Kim
Yang-jin Kim - avatar
+ 2
overloading functions are basically the way in which a function having different data types and different parameters types and numbers can be called in main with different names ....hope this helps e.g. int sum (int x,int y){return x+y;} float sum(float x,float y){return x+y;} int sum (int x,int y,iny z) {return x+y+z;} main() {int a=1,b=2; int l=5,m=9,n=9; float i=4.9,j=0.2; cout<<sum(a,b); cout<<sum(i,j); cout<<sum(l,m,n); }
14th Feb 2017, 2:49 PM
Huma Rizwan
Huma Rizwan - 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:33 AM
Syeda Juveria Afreen
Syeda Juveria Afreen - avatar