In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to overload function in C++". In daily operation, I believe many people have doubts about how to reload function in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to reload function in C++". Next, please follow the editor to study!
Function overloading requires the compiler to determine which function code should be executed when a function is called, that is, which function should be implemented. When determining the implementation of a function, it is required to distinguish from the number and type of function parameters. That is to say, when a function is overloaded, the function of the same name is required to be different in the number of parameters, or in the type of parameters. Otherwise, overloading cannot be achieved.
The following is an example of an overloaded function with different parameter types:
# include int add (int, int); double add (double, double); void main () {cout < cout <} int add (int x, int y) {return x;} double add (double a, double b) {return astatb;}
In this program, two functions with the same name add are called in the main () function, the first add () function corresponds to the function implementation of the summation of two int numbers, and the latter add () function corresponds to the function implementation of the summation of two intel numbers. This is the overloading of the function.
Here is an example of an overloaded function that varies in the number of arguments:
# include int min (int a, int b); int min (int a, int b, int c); int min (int a, int b, int c, int d); void main () {cout < cout <} int min (int a, int b) {return a} int min (int a, int b, int c) {int t = min (a, b); return min (tQuery c) } int min (int a, int b, int c, int d) {int T1 = min (a, b); int T2 = min (c, d); return min (T1, T2);}
The C++ function is overloaded in the program, and there should be three different implementations of the function name min. The distinction of the function is based on the number of parameters. In the three function implementations here, the number of parameters is 3 and 4 respectively. When calling the function, different function implementations are selected according to the number of arguments.
C++ function overloading is widely used in classes and objects, especially in class polymorphism. In the future, we will encounter more function overloads in different types, especially in the combination of class inheritance and pointer types, which are often used in VC programming.
At this point, the study of "how to overload functions in C++" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.