C + + function overloading

Source: Internet
Author: User

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 arguments, return values, and function functions of the same name functions can be completely different.

I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!

2 . function Overloading Considerations

    • A function overload cannot be just a function that has a different return value, at least in the number, type, or order of formal parameters.
    • You should make the overloaded functions you use the same functionality. If you let overloaded functions do different things, it is a bad programming style, which destroys the readability of the program.

Example: Define and test overloaded functions.

1#include <iostream>2 using namespacestd;3 4 intAbsoluteintx)5 {6         returnx<0?-x:x;7 }8 9 DoubleAbsoluteDoublex)Ten { One         returnx<0?-x:x; A } -  - intMinintXinty) the { -         returnX<y?x:y; - } -  + intMinintXintYintz) - { +         returnX<y? (x<z?x:z):(y<z?y:z); A } at  - intMain () - { -         intA =3; -         intb =-8; -         intc =6; in         DoubleD =-5.23; -cout << min (b) <<Endl; tocout << min (a,b,c) <<Endl; +cout << Absolute (a) <<Endl; -cout << Absolute (b) <<Endl; thecout << Absolute (d) <<Endl; *         return 0; $ }Panax Notoginseng  - //Rookie, please criticize advice, code writing habits and norms and so on!!! Thank you!!! 

I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!

C + + function overloading

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.