///C++初步 ///C++对C的扩展 #include <iostream> using namespace std; ///const定义常量 const int inf=0x3f3f3f3f; int main() { cout<<inf<<endl; return 0; }
///C++初步 ///C++对C的扩展 #include <iostream> using namespace std; ///函数重载 ///函数重载,一名多用,参数个数,类型要不同 int _max(int a,int b) { return (a>b)?a:b; } float _max(float a,float b) { return (a>b)?a:b; } int _max(int a,int b,int c) { a=(a>b)?a:b; a=(a>c)?a:c; return a; } int main() { int a=12,b=13,c=28; cout<<_max(a,b)<<endl; cout<<_max(a,b,c)<<endl; float e=10.3,f=13.5; cout<<_max(e,f)<<endl; return 0; }
///C++初步 ///C++对C的扩展 #include <iostream> using namespace std; ///函数模板 ///函数体完全相同,只有形参不同 /*int _max(int a,int b) { return (a>b)?a:b; } float _max(float a,float b) { return (a>b)?a:b; } long _max(long a,long b) { return (a>b)?a:b; }*/ template <typename T> T _max(T a,T b) { return (a>b)?a:b; } int main() { int a=12,b=13; cout<<_max(a,b)<<endl; float e=10.3,f=13.5; cout<<_max(e,f)<<endl; double g=12.546,m=234.356; cout<<_max(g,m)<<endl; return 0; }
///C++初步 ///C++对C的扩展 #include <iostream> using namespace std; ///有默认参数的函数,在构造函数中经常遇到 ///求圆柱体体积 float volume(float h,float r=9) { return 3.14*h*r; } int main() { cout<<volume(9)<<endl; cout<<volume(9,10)<<endl; return 0; }
///C++初步 ///C++对C的扩展 #include <iostream> using namespace std; ///变量的引用 void _swap(int &a,int &b) { int temp; temp=a; a=b; b=temp; } int main() { int a=2,b=3; _swap(a,b); cout<<a<<" "<<b<<endl; return 0; }
时间: 2024-12-17 21:30:58