C ++ function overloading and function Overloading

Source: Internet
Author: User

C ++ function overloading and function Overloading

1. Concept of function Overloading

Function overloading means that a function can have the same name as other functions in the same scope, but the parameter types, number of parameters, return values, and function functions of these functions with the same name can be completely different.

 

I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!

2. Precautions for function Overloading

  • Function overloading cannot be a function with different return values, at least in terms of the number, type, or sequence of parameters.
  • The functions of the used overload functions should be the same. It is not a good programming style to enable the overload function to complete different functions, which will damage the readability of the program.

Example: define and test overload functions.

 

1 # include <iostream> 2 using namespace std; 3 4 int absolute (int x) 5 {6 return x <0? -X: x; 7} 8 9 double absolute (double x) 10 {11 return x <0? -X: x; 12} 13 14 int min (int x, int y) 15 {16 return x <y? X: y; 17} 18 19 int min (int x, int y, int z) 20 {21 return x <y? (X <z? X: z) :( y <z? Y: z); 22} 23 24 int main () 25 {26 int a = 3; 27 int B =-8; 28 int c = 6; 29 double d =-5.23; 30 cout <min (a, B) <endl; 31 cout <min (a, B, c) <endl; 32 cout <absolute (a) <endl; 33 cout <absolute (B) <endl; 34 cout <absolute (d) <endl; 35 return 0; 36} 37 38 // cainiao. I 'd like to ask for comments and comments, code writing habits and specifications, and so on !!! Thank you !!!

 

 

I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!

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.