overload abs-重载绝对值函数
//overload abs-重载绝对值函数 #include<iostream> using namespace std; int abs(int n); //long abs(long n); float abs(float n); double abs(double n); int main() { int a = -3; long int b = -5; float c = - 3.5; double d = - 4.4; cout<<"int abs: "<<abs(a)<<endl; cout<<"long int abs: "<<abs(b)<<endl; cout<<"float abs: "<<abs(c)<<endl; cout<<"double abs: "<<abs(d)<<endl; return 0; } int abs(int n) { if(n > 0) return n; return -n; } long int abs(long n) { if(n > 0) return n; return -n; } float abs(float n) { if(n > 0) return n; return -n; } double abs(double n) { if(n > 0) return n; return -n; }
结果:
int abs: 3 long int abs: 5 float abs: 3.5 double abs: 4.4
原文地址:http://9320314.blog.51cto.com/9310314/1546908