C ++ study Note 6: function Overloading

Source: Internet
Author: User
// C ++ allows functions with similar functions to be declared with the same function name in the same scope to form overloading. Easy to use and easy to remember. // Int add (int x, int y); // float add (float X, float y); different parameter types // int add (int x, int y ); // int add (int x, int y, int Z); different parameters must have different numbers or different types. // The Compiler selects the function to be called Based on the optimal match between the type and number of real parameters. // 1. the compiler does not distinguish them by form parameters; // 2. the compiler does not distinguish between return values. // 3. do not declare functions of different functions as overload functions to avoid misunderstanding or confusion of call results. # Include <iostream. h> struct Complex {double real; double imaginary;}; int add (int m, int N) {return m + n;} double add (Double X, Double Y) {return X + Y;} complex add (complex C1, complex C2) {Complex C; C. real = c1.real + c2.real; C. imaginary = c1.imaginary + c2.imaginary; return C ;}int main (void) {int M, N; Double X, Y; complex C1, C2, C3; int add (INT m, int N); double add (Double X, Double Y); complex add (complex C1, complex C2); cout <"enter two integer :"; cin> m> N; cout <"integer" <m <'+' <n <"=" <add (m, n) <Endl; cout <"enter two real number:"; CIN> x> Y; cout <"real number" <x <'+' <Y <"=" <add (x, y) <Endl; cout <"Enter the first complex number:"; CIN> c1.real> c1.imaginary; cout <"enter the second complex number:"; CIN> c2.real> c2.imaginary; c3 = add (C1, C2); cout <"complex number (" <c1.real <',' <c1.imaginary <") + ("<c2.real <',' <c2.imaginary <") = ("<c3.real <',' <c3.imaginary <") \ n "; return 0 ;}

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.