How to overload C ++ Functions

Source: Internet
Author: User

Next, we will discuss how to study C ++ function overloading. C ++ function overloading means that the same function name can correspond to the implementation of multiple functions, if you want to overload the function of the compiler, use extern "C.

Another implementation is to calculate the sum of the two floating point types, and the other is to calculate the sum of the two plural numbers. Each implementation corresponds to a function body. These functions have the same name, but their parameters have different types. This is the concept of function overloading. Function Overloading is particularly important in applications of classes and objects.

Function overloading requires the compiler to uniquely determine which function code should be executed when a function is called, that is, which function is used for implementation. When determining the function implementation, the number and type of function parameters must be distinguished. This means that during function overloading, functions with the same name are required to have different numbers of parameters or different parameter types. Otherwise, the overload cannot be implemented.

For different overload functions of different parameter types, the following is an example of an overload function with different parameter types:

 
 
  1. #include  
  2. int add(int, int);  
  3. double add(double, double);  
  4.  
  5. void main()  
  6. {  
  7. cout< cout< }  
  8.  
  9. int add(int x, int y)  
  10. {  
  11. return x+y;  
  12. }  
  13.  
  14. double add(double a, double b)  
  15. {  
  16. return a+b;  

In this program, the main () function calls two functions with the same name as add. The add () function at the front corresponds to the function implementation of the sum of two int numbers, the next add () function corresponds to the implementation of two double-type number summation functions. This is the function overload.

The following is an example of an overloaded function with different numbers of parameters:

 
 
  1. #include  
  2. int min(int a, int b);  
  3. int min(int a, int b, int c);  
  4. int min(int a, int b, int c, int d);  
  5.  
  6. void main()  
  7. {  
  8. cout< cout< }  
  9.  
  10. int min(int a, int b)  
  11. {  
  12. return a }  
  13.  
  14. int min(int a, int b, int c)  
  15. {  
  16. int t = min(a, b);  
  17. return min(t,c);  
  18. }  
  19.  
  20. int min(int a, int b, int c, int d)  
  21. {  
  22. int t1 = min(a, b);  
  23. int t2 = min(c, d);  
  24. return min(t1, t2);  

This program contains C ++ function overloading. The function name min pair should have three different implementations. The function differentiation varies depending on the number of parameters. In the three function implementations, the number of parameters is 2, 3, and 4, respectively. Different function implementations are selected based on the number of real parameters when a function is called.

C ++ function Overloading is widely used in classes and objects, especially in class polymorphism. In the future, we will encounter more function overloading in different types, especially in combination with the inheritance of classes and pointer types, these are all frequently used in VC programming.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.