文档详情

c++课件继承和多态性.ppt

发布:2017-05-15约5.28千字共19页下载文档
文本预览下载声明
2011-4-26 Zhao Wei Introduction A template is a framework for generating a class or a function. Instead of explicitly specifying the data types used in a class or a function, parameters are used. When actual data types are assigned to the parameters, the class or function is generated by the compiler. 模板是生成类或函数的框架。 与类或函数显式指定数据类型不同,模板使用形参。当实际数据类型赋值给形参的时候,才由编译器生成类或函数。 Function templates 函数模板 int maximum( const int n1, const int n2 ) { if ( n1 n2 ) return n1 ; else return n2 ; } float maximum( const float n1, const float n2 ) { if ( n1 n2 ) return n1 ; else return n2 ; } Function templates GOES HERE please note, it is not overloading template typename T T maximum( const T n1, const T n2 ) { if ( n1 n2 ) return n1 ; else return n2 ; } int maximum( const int n1, const int n2 ) float maximum( const float n1, const float n2 ) main() { char c1 = a, c2 = b ; int i1 = 1, i2 = 2 ; float f1 = 2.5, f2 = 3.5 ; cout maximum( c1, c2 ) endl ; // a maximum() for chars cout maximum( i1, i2 ) endl ; // a maximum() for ints cout maximum( f1, f2 ) endl ; // a maximum() for floats } main() { char c1 = a, c2 = b ; int i1 = 1, i2 = 2 ; float f1 = 2.5, f2 = 3.5 ; cout maximum( c1, c2 ) endl ; //char maximum( const char n1, const char n2 ) ; cout maximum( i1, i2 ) endl ; // a maximum() for ints cout maximum( f1, f2 ) endl ; // a maximum() for floats } main() { char c1 = a, c2 = b ; int i1 = 1, i2 = 2 ; float f1 = 2.5, f2 = 3.5 ; cout maximum( c1, c2 ) endl ; //char maximum( const char n1, const char n2 ) ; cout maximum( i1, i2 ) endl ; //int maximum( const int n1, const int n2 ) ; cout maximum( f1, f2 ) endl ; // a maximum() for floats } main() { char c1 = a, c2 = b ; int i1 = 1, i2 = 2 ; float f1 = 2.5, f2 = 3.5 ; cout maximum( c1, c2 ) endl ; //char maximum( const char n1, const char n2 ) ; cout maximum( i1, i2 ) endl ; //int maximum( const int n1, const int n2 ) ; cout maximum( f1, f
显示全部
相似文档