2022年面向对象程序设计山师第七章习题答案 .docx
精品_精品资料_一、挑选填空第七章习题答案可编辑资料 - - - 欢迎下载精品_精品资料_1、D 2 、A 3、B 4、C 5、A6 、C 7、B 8、D 9 、二、判定以下描述的正确性,对者划,错者划×.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,26,95,66,93、该程序的输出结果如下:( 13,22, 30,40)4、运行该程序输出结果如下所示.D2:displaypri1=4,pri2=5 pri4=6 pri12=7D2:display pri1=12 ,pri2=9可编辑资料 - - - 欢迎下载精品_精品资料_pri4=7 pri12=85、该程序输出结果如下所示:D2: display pri1=1,pri2=4 pri4=6pri12=7D2:display pri1=9,pri2=8 pri4=7 pri12=86、该程序输出结果如下所示:base class base class base class derive1 class derive2 class五、按以下要求编写程序.1、程序内容如下所示.#include <iostream.h> #include <iomanip.h>class personint no .char name10 .public:void inputcout<<" 编号: ".cin>>no .cout<<" 姓名: ".cin>>name.void dispcout<<" 编号: "<<no<<endl .cout<<" 姓名: "<<name<<endl . .class student:public personprivate:char depart6 .int degree.public:可编辑资料 - - - 欢迎下载精品_精品资料_void inputperson:input .cout<<" 班号: ". cin>>depart .cout<<" 成果: ". cin>>degree.void dispperson:disp .cout<<" 班号: "<<depart<<endl .cout<<" 成果: "<<degree<<endl . .class teacher:public personprivate:char prof10 .char depart10 .public:void inputperson:input .cout<<" 职称: ". cin>>prof .cout<<" 部门: ". cin>> depart .void dispperson:disp .cout<<" 职称: "<< prof<<endl .cout<<" 部门: "<< depart <<endl . .void mainstudent s1.teacher t1.cout<<" 输入一个同学数据:n".s1.input .cout<<" 输入一个老师数据:n".t1.input .cout<<" 显示一个同学数据:n".s1.disp.cout<<" 显示一个老师数据:n".t1.disp .2、程序内容如下所示.#include <iostream.h> #include <string.h>可编辑资料 - - - 欢迎下载精品_精品资料_class stringint length .char *contents .public:int get_lengthreturn length.char *get_contentsreturn contents .stringdelete contents .int set_contentsint in_length,char *in_contents .int set_contentschar *in_contents .void printcout<<contents<<endl. .class edit_string:public stringint cursor .public:int get_cursor_posreturn cursor .void move_cursorint how_muchcursor=how_much. int add_at_cursorstring *new_text .int repl_at_cursorstring *new_text.void dele_at_cursorint how_much . .int string:set_contentsint in_length,char *in_contentslength=in_length .if .contents delete contents .contents=new charlength+1 .strcpycontents,in_contents .return length .int string:set_contentschar *in_contentslength=strlenin_contents .if .contents delete contents .contents=new charlength+1 .strcpycontents,in_contents .return length .int edit_string:add_at_cursorstring *new_textint n,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=new charm+n+1 .for int i=0 .i<cursor .i+ newsi=cpi .k=i .for int j=0 .j<n .i+,j+ newsi=ptj .cursor=i .forj=k .j<m .j+,i+ newsi=cpj .newsi='0' .set_contentsnews.delete news.return cursor.int edit_string:repl_at_cursorstring *new_textint n,m .char *pt,*news .n=new_text->get_length .pt=new_text->get_contents .m=this->get_length .news=new charm>n+cursor . m+1 : n+cursor+1.news=this->get_contents .forint i=cursor,j=0 .i<n+cursor .j+,i+ newsi=ptj .if m<n+cursor newsi='0'.cursor=i .set_contentsnews.delete news.return cursor.void edit_string:dele_at_cursorint how_muchint m .char *cp,*news .cp=this->get_contents .m=this->get_length .forint i=cursor .i<m .i+ cpi=cpi+how_much.cpi='0' .void main可编辑资料 - - - 欢迎下载精品_精品资料_string s1.edit_string s2 .char *cp .s1.set_contents"Object_Oriented Programming" .cp=s1.get_contents.s2.set_contentscp. s2.print .s2.move_cursor15 .s1.set_contents"Windwos" .s2.add_at_cursor&s1 .s2.print .s2.move_cursor6 .s2.dele_at_cursor9.s2.print .s1.set_contents" TTT" .s2.repl_at_cursor&s1 .s2.print .3、程序内容如下所示.#include <iostream.h> class vehicleprotected:int wheels.float weight .public:vehicleint wheels,float weight.int get_wheels .float get_weight .float wheel_load .void print . .class car:vehicleint passenger_load.public:carint wheels,float weight,int passengers=4 .int get_passengers.void print . .class truck:vehicleint passenger_load.可编辑资料 - - - 欢迎下载精品_精品资料_float payload .public:truckint wheels,float weight,int passengers=2,float max_load=240000.00.int get_passengers.float efficiency .void print . .vehicle:vehicleint wheels,float weightvehicle:wheels=wheels .vehicle:weight=weight .int vehicle:get_wheelsreturn wheels.float vehicle:get_weightreturn weight/wheels .void vehicle:printcout<<"车轮: "<<wheels<<" 个."<<endl .cout<<"重量: "<<wheels<<" 公斤. "<<endl .car:carint wheels,float weight,int passengers:vehiclewheels,weightpassenger_load=passengers.int car:get_passengersreturn passenger_load.void car:printcout<<" 小车: "<<endl .vehicle:print .cout<<" 载人: "<<passenger_load<<" 人. "<<endl .cout<<endl .truck:truckint wheels,float weight,int passengers,float max_load:vehiclewheels,weightpassenger_load=passengers. payload=max_load .可编辑资料 - - - 欢迎下载精品_精品资料_int truck:get_passengersreturn passenger_load.float truck:efficiencyreturn payload/payload+weight .void truck:printcout<<" 卡车 "<<endl .vehicle:print .cout<<" 载人: "<<passenger_load<<" 人. "<<endl .cout<<" 效率: "<<efficiency<<endl.cout<<endl .void maincar car14,1000,5 .truck tru110,5000,3,340000 .car1.print .tru1.print .4、程序内容如下所示.#include <iostream.h> #include <string.h>class employeeprotected:int no .char name10 .float salary .public:employeecout<<" 职工编号: ".cin>>no .cout<<" 职工姓名: ".cin>>name .salary=0 .void pay void display可编辑资料 - - - 欢迎下载精品_精品资料_ .class technician:public employeeprivate:float hourlyrate .int workhours .public:technicianhourlyrate=100 . void paycout<<name<<" 本月工作时数: ".cin>>workhours .salary=hourlyrate*workhours .void displaycout<<" 兼职技术人员: "<<name<<" (编号为: "<<no<<" ) "<<" 本月工资: "<<salary<<endl . .class salesman:virtual public employeeprotected:float commrate .float sales.public:salesmancommrate=0.04 . void paycout<<name<<" 本月销售额: " .cin>>sales.salary=sales*commrate .void displaycout<<" 销售员: "<<name<<" (编号为: "<<no<<" ) "<<" 本月工资: "<<salary<<endl . .class manager:virtual public employeeprotected:float monthlypay .public: managermonthlypay=8000 .可编辑资料 - - - 欢迎下载精品_精品资料_void paysalary=monthlypay. void displaycout<<" 经理: "<<name<<" (编号为: "<<no<<" ) "<<" 本月工资: "<<salary<<endl . .class salesmanager:public manager,public salesmanpublic: salesmanagermonthlypay=5000 .commrate=0.005 .void paycout<<name<<" 所管部门月销售量: ".cin>>sales.salary=monthlypay+commrate*sales .void displaycout<<" 销售经理: "<<name<<" (编号为: "<<no<<" ) "<<" 本月工资: "<<salary<<endl . .void mainmanager m1.technician t1 .salesman s1.salesmanager sm1.m1.pay .m1.display .t1.pay.t1.display .s1.pay.s1.display .sm1.pay.sm1.display .可编辑资料 - - - 欢迎下载