2023年面向对象程序设计C++山师习题答案_第1页
2023年面向对象程序设计C++山师习题答案_第2页
2023年面向对象程序设计C++山师习题答案_第3页
2023年面向对象程序设计C++山师习题答案_第4页
2023年面向对象程序设计C++山师习题答案_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

第七章习题答案一、选择填空1、D2、A3、B4、C5、A6、C7、B8、D9、二、判断下列描述的对的性,对者划√,错者划×。1、√2、×3、×4、×5、√6、×7、√8、√9、√10、×11、√12、√13、×14、√15、√16、×17、√18、√三、分析下列程序的输出结果。1、(1)上述结构的DAG图如下所示。(2)无二义性(3)无二义性2、(1)无(2无(3)有(4)无(5)有四、分析下列程序的输出结果1、运营该程序输出如下结果。(1,2)5,6(6,9)2、该程序的输出结果如下所示(1,2)(6,9)5,6(6,9)3、该程序的输出结果如下:(13,22,30,40)4、运营该程序输出结果如下所示。D2::display()pri1=4,pri2=5pri4=6pri12=7D2::display()pri1=12,pri2=9pri4=7pri12=85、该程序输出结果如下所示:D2::display()pri1=1,pri2=4pri4=6pri12=7D2::display()pri1=9,pri2=8pri4=7pri12=86、该程序输出结果如下所示:baseclassbaseclassbaseclassderive1classderive2class五、按下列规定编写程序。1、程序内容如下所示。#include<iostream.h>#include<iomanip.h>classperson{intno;charname[10];public:voidinput(){cout<<"编号:";cin>>no;cout<<"姓名:";cin>>name;}voiddisp(){cout<<"编号:"<<no<<endl;cout<<"姓名:"<<name<<endl;}};classstudent:publicperson{private:chardepart[6];intdegree;public:voidinput(){person::input();cout<<"班号:";cin>>depart;cout<<"成绩:";cin>>degree;}voiddisp(){person::disp();cout<<"班号:"<<depart<<endl;cout<<"成绩:"<<degree<<endl;}};classteacher:publicperson{private:charprof[10];chardepart[10];public:voidinput(){person::input();cout<<"职称:";cin>>prof;cout<<"部门:";cin>>depart;}voiddisp(){person::disp();cout<<"职称:"<<prof<<endl;cout<<"部门:"<<depart<<endl;}};voidmain(){students1;teachert1;cout<<"输入一个学生数据:\n";s1.input();cout<<"输入一个教师数据:\n";t1.input();cout<<"显示一个学生数据:\n";s1.disp();cout<<"显示一个教师数据:\n";t1.disp();}2、程序内容如下所示。#include<iostream.h>#include<string.h>classstring{intlength;char*contents;public:intget_length(){returnlength;}char*get_contents(){returncontents;}~string(){deletecontents;}intset_contents(intin_length,char*in_contents);intset_contents(char*in_contents);voidprint(){cout<<contents<<endl;}};classedit_string:publicstring{intcursor;public:intget_cursor_pos(){returncursor;}voidmove_cursor(inthow_much){cursor=how_much;}intadd_at_cursor(string*new_text);intrepl_at_cursor(string*new_text);voiddele_at_cursor(inthow_much);};intstring::set_contents(intin_length,char*in_contents){length=in_length;if(!contents)deletecontents;contents=newchar[length+1];strcpy(contents,in_contents);returnlength;}intstring::set_contents(char*in_contents){length=strlen(in_contents);if(!contents)deletecontents;contents=newchar[length+1];strcpy(contents,in_contents);returnlength;}intedit_string::add_at_cursor(string*new_text){intn,k,m;char*cp,*pt;n=new_text->get_length();pt=new_text->get_contents();cp=this->get_contents();m=this->get_length();char*news=newchar[m+n+1];for(inti=0;i<cursor;i++)news[i]=cp[i];k=i;for(intj=0;j<n;i++,j++)news[i]=pt[j];cursor=i;for(j=k;j<m;j++,i++)news[i]=cp[j];news[i]='\0';set_contents(news);deletenews;returncursor;}intedit_string::repl_at_cursor(string*new_text){intn,m;char*pt,*news;n=new_text->get_length();pt=new_text->get_contents();m=this->get_length();news=newchar[m>n+cursor?m+1:n+cursor+1];news=this->get_contents();for(inti=cursor,j=0;i<n+cursor;j++,i++)news[i]=pt[j];if(m<n+cursor)news[i]='\0';cursor=i;set_contents(news);deletenews;returncursor;}voidedit_string::dele_at_cursor(inthow_much){intm;char*cp,*news;cp=this->get_contents();m=this->get_length();for(inti=cursor;i<m;i++)cp[i]=cp[i+how_much];cp[i]='\0';}voidmain(){strings1;edit_strings2;char*cp;s1.set_contents("Object_OrientedProgramming");cp=s1.get_contents();s2.set_contents(cp);s2.print();s2.move_cursor(15);s1.set_contents("Windwos");s2.add_at_cursor(&s1);s2.print();s2.move_cursor(6);s2.dele_at_cursor(9);s2.print();s1.set_contents("TTT");s2.repl_at_cursor(&s1);s2.print();}3、程序内容如下所示。#include<iostream.h>classvehicle{protected:intwheels;floatweight;public:vehicle(intwheels,floatweight);intget_wheels();floatget_weight();floatwheel_load();voidprint();};classcar:vehicle{intpassenger_load;public:car(intwheels,floatweight,intpassengers=4);intget_passengers();voidprint();};classtruck:vehicle{intpassenger_load;floatpayload;public:truck(intwheels,floatweight,intpassengers=2,floatmax_load=240000.00);intget_passengers();floatefficiency();voidprint();};vehicle::vehicle(intwheels,floatweight){vehicle::wheels=wheels;vehicle::weight=weight;}intvehicle::get_wheels(){returnwheels;}floatvehicle::get_weight(){returnweight/wheels;}voidvehicle::print(){cout<<"车轮:"<<wheels<<"个。"<<endl;cout<<"重量:"<<wheels<<"公斤。"<<endl;}car::car(intwheels,floatweight,intpassengers):vehicle(wheels,weight){passenger_load=passengers;}intcar::get_passengers(){returnpassenger_load;}voidcar::print(){cout<<"小车:"<<endl;vehicle::print();cout<<"载人:"<<passenger_load<<"人。"<<endl;cout<<endl;}truck::truck(intwheels,floatweight,intpassengers,floatmax_load):vehicle(wheels,weight){passenger_load=passengers;payload=max_load;}inttruck::get_passengers(){returnpassenger_load;}floattruck::efficiency(){returnpayload/(payload+weight);}voidtruck::print(){cout<<"卡车"<<endl;vehicle::print();cout<<"载人:"<<passenger_load<<"人。"<<endl;cout<<"效率:"<<efficiency()<<endl;cout<<endl;}voidmain(){carcar1(4,1000,5);trucktru1(10,5000,3,340000);car1.print();tru1.print();}4、程序内容如下所示。#include<iostream.h>#include<string.h>classemployee{protected:intno;charname[10];floatsalary;public:employee(){cout<<"职工编号:";cin>>no;cout<<"职工姓名:";cin>>name;salary=0;}voidpay(){}voiddisplay(){}};classtechnician:publicemployee{private:floathourlyrate;intworkhours;public:technician(){hourlyrate=100;}voidpay(){cout<<name<<"本月工作时数:";cin>>workhours;salary=hourlyrate*workhours;}voiddisplay(){cout<<"兼职技术人员:"<<name<<"(编号为:"<<no\<<")"<<"本月工资:"<<salary<<endl;}};classsalesman:virtualpublicemployee{protected:floatcommrate;floatsales;public:salesman(){commrate=0.04;}voidpay(){cout<<name<<"本月销售额:";cin>>sales;salary=sales*commrate;}voiddisplay(){cout<<"销售员:"<<name<<"(编号为:"<<no<<")"<<"本月工资:"<<

温馨提示

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

评论

0/150

提交评论