C + + overloaded functions

Source: Internet
Author: User

General function Overloading Sample program:

#include <iostream>#include<string>using namespacestd;//overloading: 1. In the same scope (in the same class, in the same file),//2. The function method name is the same,//3. But the parameters are different (number, type, order)//Special overloads: 1, Constant function overloading 2, operator overloading//function overloading is the same as function name, there are 3 cases of function overloading: Different parameter types, different number of parameters, different order of parameters//1. Different parameter typesintAddintNUM1,intnum2) {cout<<"int Add (int num1,int num2)"<<Endl; returnnum1+num2;}//--floatAddfloatNUM1,floatnum2) {cout<<"float Add (float num1,float num2)"<<Endl; returnnum1+num2;}//2. Different number of parametersintAddintNUM1,intNUM2,intnum3) {cout<<"int Add (int num1,int num2,int num3)"<<Endl; returnnum1+num2+num3;}//3. Parameter order is differentfloatAddintNUM1,floatnum2) {cout<<"float Add (int num1,float num2)"<<Endl; returnnum1+num2;}//--floatAddfloatNUM1,intnum2) {cout<<"float Add (float num1,int num2)"<<Endl; returnnum1+num2;}intMain () {//1. Different parameter types    intnum; Num= Add (2,3);//Show "int add (int num1,int num2)"cout << Num <<Endl; //2. Different number of parametersnum = Add (1,2,3);//Show "int add (int num1,int num2,int num3)"cout << Num <<Endl; //3. Parameter order is different    floatFnum; Fnum= Add (1,1.1f);//Show "float Add (int num1,float num2)"cout << Fnum <<Endl; Fnum= Add (1.1f,1);//Show "float Add (float num1,int num2)"cout << Fnum <<Endl;}

C + + overloaded functions

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.