第14讲模板.doc-IIS7.doc

第14讲模板.doc-IIS7.doc

第14模板 板是++的精华所在! 它是面向对象编程能给码农带来的极大福利! 同一个结构的不同数据类型的函数,可以统一定义, 相当于模式设计吧!可以大大减轻设计的工作量,尤其是重复性的工作量! 函数模板Lt14A //Lt14A.cpp #include iostream #include string using namespace std; int sum(int a[],int n) { int x=0; int i=0; for (i=0;in;i++) { x=x+a[i]; } return x; } float sum(float a[],int n) { float x=0; int i=0; for (i=0;in;i++) { x=x+a[i]; } return x; } double sum(double a[],int n) { double x=0; int i=0; for (i=0;in;i++) { x=x+a[i]; } return x; } //同一个函数的不同实现,称为函数重载,可少用一个函数名称 //从写代码角度看,并没有少写一句话 int main() { int a[]={1,2,3,4,5,6,7,8,9,10}; float af[]={1.0,2.0,3.0,4.0,50.1,6.0,7.0

文档评论(0)

1亿VIP精品文档

相关文档