C++程序设计基础课后答案 第九章_第1页
C++程序设计基础课后答案 第九章_第2页
C++程序设计基础课后答案 第九章_第3页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

阅读下列程序,写出执行结果#include<iostream.h>template<typenameT>voidfun(T&x,T&y){Ttemp;temp=x;x=y;y=temp;}voidmain(){inti,j;i=10;j=20;fun(i,j);cout<<"i="<<i<<'\t'<<"j="<<j<<doublea,b;a=1.1;b=2.2;fun(a,b);cout<<"a="<<a<<'\t'<<"b="<<b<<endl;}#include<iostream.h>template<typenameclassBase{public:Base(Ti,Tj){x=i;y=j;}Tsum(){returnx+y;}private:Tx,y;};voidmain(){Base<double>obj2(3.3,5.5);cout<<obj2.sum()<<endl;Base<int>obj1(3,5);cout<<obj1.sum()<<endl;}思考题抽象类和模板都是提供抽象的机制,请分析它们的区别和应用场合。类属参数可以实现类型转换吗?如果不行,应该如何处理?吗?请你写个验证程序试一试。4试。抽象类和类模板都是提供抽象的机制,请分析它们的区别和应用场合。【答案】也没有操作定义。派生类必须定义实现版本。抽象类用于程序开发时对功能的统一策划,利用程序运行的多态性自动匹配实行不同版本的函数。类模板抽象了数据类型,称为类属参数。成员函数描述了类型不同,逻辑操作相同的功能集。编译器用建立对象的数据类型参数实例化为模板类,生成可运行的实体。类模板用于抽象数据对象类型不同,逻辑操作完全相同类定义。这种数据类型的推导必须在语言功能的范畴之内的。类属参数可以实现类型转换吗?如果不行,应该如何处理?【答案】类属参数不可以实现类型转换。为了解决参数隐式类型转换的问题,可以用类型参数把函数模板重载为非模板函数。吗?请你写个验证程序试一试。【答案】类模板可以声明的友员形式有:普通函数、函数模板、普通类成员函数、类模板成员函数以及普通类、类模板。当类模板的友员是函数模板时,它们可以定义不同形式的类属参数。程序略。试。【答案】类模板的静态数据成员可以是抽象类型。它们的存储空间在生成具体模板类的时候建立,即每生成一个模板类同时建立静态储存空间并做一次文件范围的初始化。程序略。编程题main(精度浮点型数组的平均值。答案9.3-1#include<iostream.h>template<typenameT>floataverage(T*array,intsize){Tsum=0;for(inti=0;i<size;i++)sum+=array[i];returnsum/size;}voidmain(){inta[]={1,2,3,4,5,6,7,8,9,10};floatb[]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10};cout<<"Averageofarraya:"<<average(a,10)<<endl;cout<<"Averageofarrayb:"<<average(b,10)<<endl;}建立结点包括一个任意类型数据域和一个指针域的单向链表类模板。在main(立数据域为整型的单向链表,并把链表中的数据显示出来。答案9.3-2#includetemplate<typenameclassList{public:List(Tx){data=x;}voidappend(List*node){node->next=this;next=0;}List*getnext(){returnnext;}Tgetdata(){returndata;}private:Tdata;List*next;};voidmain(){inti,idata,n,fdata;cout<<"输入结点的个数:";cin>>n;cout<<"输入结点的数据域:";cin>>fdata;List<int>headnode(fdata);List<int>*p,*last;last=&headnode;for(i=1;i<n;i++){cin>>idata;p=newlist<int>(idata);p->append(last);last=p;}cout<<"链表已经建立!"<<endl;cout<<"链表中的数据为:"<<endl;p=&headnode;while(p){cout<<p->getdata()<<endl;p=p->getnext();}}T_Counter*T_VectorT_MatrixT_VectorT_MatrixVectorMatrix(88.34)的语法区别和运算功能区别。【答案】略MSDNLibraryVisualC++的STL55.4第4题,建立职工信息链表,并完成题中要求的各种操作。答案9.3-4#include<iostream>#include<list>#include<iterator>usingnamespacestd;structemployee{intnum;intage;charsex;}voidcreateList(list<employee>&,int);voidoutList(list<employee>&);voidcount(list<employee>&);voidinsert(list<employee>&);voiddel(list<employee>&eList,int);voiddelcreate(list<employee>&);voidmain(){intchoice,n,bh;list<employee>List1;list<employee>::iteratorp;L:cout<<"\n\t\t请键入操作选择\n"<<endl;cout<<"\t1---建立单向链表"<<endl;cout<<"\t2---显示单向链表中全部职工信息"<<endl;cout<<"\t3---统计男女职工人数"<<endl;cout<<"\t4---在职工尾部插入新结点"<<endl;cout<<"\t5---删除指定编号的结点"<<endl;cout<<"\t6---删除指定年龄段的结点,并把被删除结点保存在另一链表中"<<endl;cout<<"\t0---退出"<<endl;cout<<"\t\t";cin>>choice;switch(choice){case1: cout<<"\tcin>>n;createList(List1,n);gotoL;case2:outList(List1);gotoL;case3:count(List1);gotoL;case4:insert(List1);gotoL;case5:cout<<"\t输入需删除结点编号:";cin>>bh;del(List1,bh);gotoL;case6:delcreate(List1);gotoL;case0: cout\t\n"<<endlbreak;default:cout<<"\t!\n"<<endl;goto}}//建立职工信息单向链表voidcreateList(list<employee>&List1,intlen){list<employee>::iteratorp;inti;employeedata;p=List1.begin();for(i=0;i<len;i++,p++){cout<<"\t编号:";cin>>data.num;cout<<"\t年龄:";cin>>data.age;cout<<"\t性别:";cin>>data.sex;p=List1.begin();List1.insert(p,data);}}//显示单向链表中全部职工信息voidoutList(list<employee>&List1){list<employee>::iteratorp;cout<<"\tp=List1.begin();while(p!=List1.end()){cout<<"\t编号:";cout<<(*p).num<<endl;cout<<"\t年龄:";cout<<(*p).age<<endl;cout<<"\t性别:";cout<<(*p).sex<<endl;p++;}cout<<endl;}//统计男女职工人数voidcount(list<employee>&List1){list<employee>::iteratorintm=0,f=0;p=List1.begin();while(p!=List1.end()){if((*p).sex=='m')m=m+1;if((*p).sex=='f')f=f+1;p++;}cout<<endl;cout<<"\t男职工人数:"<<m<<endl;cout<<"\t女职工人数:"<<f<<endl;}//在链表尾部插入新结点voidinsert(list<employee>&List1){list<employee>::iteratorpemployeedata;cout<<"\tcout<<"\tcin>>data.num;cout<<"\tcin>>data.age;cout<<"\tcin>>data.sex;cout<<endl;p=List1.begin();while(p!=List1.end(){p++;};List1.insert(p,data);}//删除指定编号的结点voiddel(list<employee>&List1,intbh){list<employee>::iteratorp;charch;intflag=0;p=List1.begin();while(p!=List1.end(){if((*p).num==bh){flag=1;break;}p++;};ifflag==0cout"\tendl;gotoL1;cout<<"\tendl;cout<<"\t编号:";cout<<(*p).num<<endl;cout<<"\t年龄:";cout<<(*p).age<<endl;cout<<"\t性别:";cout<<(*p).sex<<endl;cout<<"\t确实要删除?(y/n)";cin>>ch;if(ch=='y'||ch=='Y')List1.erase(p);L1:;}//删除指定年龄段的结点,并把被删除结点保存在另一链表中voiddelcreate(list<employee>&List1){list<employee>::iteratorp1,p2,p3;list<employee>List2,List3;p1=List1.begin();p2=List2.begin();p3=List3.begin();while(p1!=List1.end()){if((*p1).age>=55&&(*p1).age<=60){List2.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论