#include<iostream>
#include<stdlib.h>
using namespace std;
void func(int);//函数重载
void func(long);
float func(float);
void func(double);
int main()
{
int a = 1;
long b = 100000;
float c = 1.5;
double d = 6.45679;
func(a);//根据参数的不同选择不同的函数调用
func(b);
func(c);
func(d);
//system("pause");
return 0;
}
void func(int a)
{
cout << "a的平方为:" << a*a << endl;
}
void func(long b)
{
cout << "b的平方为:" << b*b << endl;
}
float func(float c)
{
cout << "c的平方为:" << c*c << endl;
return 2 * c;
}
void func(double d)
{
cout << "d的平方为:" << d*d << endl;
}
另外,程序调试时“出现无法打开PDB文件”,不需要理会,对程序运行没有影响。
对于DOS窗口一闪而过的情景,在程序最后包含system(“pause”),并在头文件中加入第二行中的语句
#include<iostream>
#include<stdlib.h>
using namespace std;
class cube
{
public:
void sum()//成员函数的重载
{
cout << "i的立方:" << i*i*i << "\t" << "j的立方:" << j*j*j << endl;
}
void sum(int x, int y)
{
i = x; j = y;
cout << "i:" << i << "\t\t" << "j:" << j << endl;
}
private:
int i;
int j;
};
int main()//
{
cube a;
a.sum(2, 3);//此处将私有成员赋值i=2,j=3
a.sum();//此处私有成员的值仍保留为上面函数调用时的值。
//system("pause");
}