深入函数

1:普通函数的重载

/*普通函数的重载:
我们可以将一个相同名字但是不同类型的函数重复调用多次来处理不同类型的数据
由于参数不同,编译器根据参数类型调用不同的函数,输出不同的结果*/
#include <iostream>
using namespace std;
void func(int);
void func(long);
void func(float);
void func(double);
int main()
{
    int a = 1;
    long b = 10000;
    float c = 1.5;
    double d = 6.4579;
    cout << "a:" << a << endl;
    cout << "b:" << b << endl;
    cout << "c:" << c << endl;
    cout << "d:" << d << endl;
    func(a);
    func(b);
    func(c);
    func(d);
    return 0;
}
void func(int a)
{
    cout << "a的平方为:" << a*a << endl;
}
void func(long a)
{
    cout << "b的平方为:" << a*a << endl;
}
void func(float a)
{
    cout << "c的平方为:" << a*a << endl;
}
void func(double a)
{
    cout << "d的平方为:" << a*a << endl;
}

 2:成员函数的重载

/*成员函数的重载*/
#include <iostream>
using namespace std;
class cube
{
public:
    void sum();
    void sum(int x, int y);
private:
    int i;
    int j;
};
int main()
{
    cube a;
    a.sum(2, 3);
    a.sum();
    return 0;
}
void cube::sum()
{
    cout << "i的平方为:" << i*i << endl;
    cout << "j的平方为:" << j*j << endl;
}
void cube::sum(int x, int y)
{
    i = x;
    j = y;
    cout << "i:" << i << "	" << "j:" << j << endl;
}
原文地址:https://www.cnblogs.com/rain-1/p/4855071.html