• 59.00 KB
  • 10页

C++面向对象课后练习答案.doc

  • 10页
  • 关注公众号即可免费下载文档
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档由网友投稿或网络整理,如有侵权请及时联系我们处理。
'类与对象习题程序阅读题1.分析以下程序执行的结果#include#includeclassSample{public:intx,y;Sample(){x=y=0;}Sample(inta,intb){x=a;y=b;}voiddisp(){cout<<"x="<classSample{intx,y;public:Sample(){x=y=0;}Sample(inta,intb){x=a;y=b;}~Sample(){if(x==y)cout<<"x=y"<classSample{intx;public:Sample(inta){x=a;cout<<"constructingobject:x="<classSample{intx,y;public:Sample(){x=y=0;}Sample(inta,intb){x=a;y=b;}voiddisp(){cout<<"x="<disp();}分析:本题说明了对象指针的使用方法。这里通过指向对象的指针来调用对象的成员函数。对象指针p指向对象s,p->disp()等价于s.disp()。所以输出为:x=2,y=3题5分析以下程序的执行结果#includeclassSample{public:intx;inty; voiddisp(){cout<<"x="<classTest{ intx,y;public:voidinit(int,int);voidprint();};voidTest::init(inti,intj){x=i;y=j;}voidTest::print(){cout<<"测试结果:"<#include#defineMax100classBank{inttop;chardate[Max][10];//日期intmoney[Max];//金额intrest[Max];//余额staticintsum;//累计余额public:Bank(){top=0;}voidbankin(chard[],intm){strcpy(date[top],d);money[top]=m;sum=sum+m; rest[top]=sum;top++;}voidbankout(chard[],intm){strcpy(date[top],d);money[top]=-m;sum=sum-m;rest[top]=sum;top++;}voiddisp();};intBank::sum=0;voidBank::disp(){inti;printf("日期存入取出余额n");for(i=0;i#includeclassemployee{protected:charname[10];//姓名charstreet[20];//街道地址charcity[10];//市charprov[10];//省charpost[7];//邮政编码intno;//记录序号public:employee(char[],char[],char[],char[],char[],int);voidchangename(charstr[]);voiddisplay();};employee::employee(charn[],chars[],charc[],charp1[],charp2[],intnum){strcpy(name,n);strcpy(street,s);strcpy(city,c);strcpy(prov,p1);strcpy(post,p2);no=num;}voidemployee::changename(charn[]){strcpy(name,n);}voidemployee::display(){cout<<"输出记录"<#includeclassUser{charuser[10];charpass[7];public: User(char[],char[]);char*getuser();char*getpass();};User::User(charu[],charp[]){strcpy(user,u);strcpy(pass,p);}char*User::getuser(){returnuser;}char*User::getpass(){returnpass;}voidmain(){Userua[]={User("Li","123456"),User("wang","654321"),User("Chen","213412"),User("Xu","878616"),User("Ma","876574"),User("Song","666666")};charname[10];while(1){cout<<"输入用户名:";cin>>name;if(strcmp(name,"end")==0)break;for(inti=0;i<6;i++)if(strcmp(name,ua[i].getuser())==0){cout<<"用户口令:"<