函数重载#include <iostream.h>int abs(int x);double abs(double x);long abs(long x);void main(){ ...
函数重载
#include <iostream.h>
int abs(int x);
double abs(double x);
long abs(long x);
void main()
{
cout << abs(-6) << endl;
cout << abs(-6L)<< endl;
cout << abs(15.3)<< endl;
}
函数重载的二义性
(下面会引起编译错误)
#include<iostream.h>
float abs(float x)
{
return (x>=0?x:-x);
}
double abs(double x)
{
return (x>=0?x:-x);
}
void main()
{
cout << abs(3.14) << endl;
cout << abs(3.14f) << endl;
cout << abs(-5) << endl;
}
函数的作用域
1.函数的声明和定义默认情况下在这整个程序中是extern的
2.静态函数:函数前加上static
实参与形参之间的值传递 #include<iostream.h>
void main()
{
int a = 5, b= 10;
void swap(int x , int y );
cout << a << ' ' << b >> endl;
}
void swap(int x , int y)
{
int temp ;
temp = x;
x = y;
y = temp;
}
学员评论