1、第七章习题答案一、选择填空 1、D 2、A 3、B 4、C 5、A 6、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,2)(6,9)5,6(6,9)3、该程序的输出结果如下:(13,22,30
2、,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、该程序输出结果如下所示:base classbase classbase classderive1 classderive2 class五、按下列规定编写程序。1、程序内容如下所示。#include #include
3、class person int no; char name10; public: void input() coutno; coutname; void disp() cout 编号:noendl; cout 姓名:nameendl; ;class student:public person private:char depart6;int degree; public:void input() person:input(); coutdepart; coutdegree;void disp() person:disp(); cout 班号:departendl; cout 成绩:degre
4、eendl;class teacher:public person private:char prof10;char depart10; public:void input() person:input(); coutprof; cout depart ;void disp() person:disp(); cout 职称: profendl; cout 部门: depart endl;void main() student s1; teacher t1; cout输入一个学生数据:n; s1.input(); cout输入一个教师数据:n; t1.input(); cout显示一个学生数据:
5、n; s1.disp(); cout显示一个教师数据:n; t1.disp();2、程序内容如下所示。#include #include class stringint length;char *contents; public:int get_length()return length;char *get_contents()return contents;string()delete contents;int set_contents(int in_length,char *in_contents);int set_contents(char *in_contents);void prin
6、t()coutcontentsget_length(); pt=new_text-get_contents(); cp=this-get_contents(); m=this-get_length(); char *news=new charm+n+1; for (int i=0;icursor;i+) newsi=cpi; k=i; for (int j=0;jn;i+,j+)newsi=ptj; cursor=i; for(j=k;jget_length(); pt=new_text-get_contents(); m=this-get_length(); news=new charmn+
7、cursor ? m+1 : n+cursor+1; news=this-get_contents(); for(int i=cursor,j=0;in+cursor;j+,i+)newsi=ptj; if (mget_contents(); m=this-get_length(); for(int i=cursor;im;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_conte
8、nts(); 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 class vehicle protected:int wheels;float weight;
9、 public:vehicle(int wheels,float weight);int get_wheels();float get_weight();float wheel_load();void print();class car:vehicleint passenger_load; public:car(int wheels,float weight,int passengers=4);int get_passengers();void print();class truck:vehicleint passenger_load;float payload; public:truck(i
10、nt wheels,float weight,int passengers=2,float max_load=240000.00);int get_passengers();float efficiency();void print();vehicle:vehicle(int wheels,float weight) vehicle:wheels=wheels; vehicle:weight=weight;int vehicle:get_wheels() return wheels;float vehicle:get_weight() return weight/wheels;void veh
11、icle:print() cout 车轮:wheels个。endl; cout 重量:wheels公斤。endl;car:car(int wheels,float weight,int passengers):vehicle(wheels,weight) passenger_load=passengers;int car:get_passengers() return passenger_load;void car:print() cout 小车:endl; vehicle:print(); cout 载人:passenger_load人。endl; coutendl;truck:truck(
12、int wheels,float weight,int passengers,float max_load):vehicle(wheels,weight) passenger_load=passengers; payload=max_load;int truck:get_passengers() return passenger_load;float truck:efficiency() return payload/(payload+weight);void truck:print() cout卡车endl; vehicle:print(); cout 载人:passenger_load人。
13、endl; cout 效率:efficiency()endl; coutendl;void main() car car1(4,1000,5); truck tru1(10,5000,3,340000); car1.print(); tru1.print();4、程序内容如下所示。#include #include class employee protected:int no;char name10;float salary; public:employee() coutno; coutname; salary=0;void pay()void display();class technic
14、ian:public employee private:float hourlyrate;int workhours; public:technician()hourlyrate=100;void pay() coutnameworkhours; salary=hourlyrate*workhours;void display() cout兼职技术人员:name(编号为:no)本月工资:salaryendl;class salesman:virtual public employee protected:float commrate;float sales; public:salesman()
15、commrate=0.04;void pay() coutnamesales; salary=sales*commrate;void display() cout销售员:name(编号为:no)本月工资:salaryendl;class manager:virtual public employee protected:float monthlypay; public:manager()monthlypay=8000;void pay()salary=monthlypay;void display() cout经理:name(编号为:no)本月工资:salaryendl;class sales
16、manager:public manager,public salesman public:salesmanager() monthlypay=5000; commrate=0.005;void pay() coutnamesales; salary=monthlypay+commrate*sales;void display() cout销售经理:name(编号为:no)本月工资:salaryendl;void main() manager m1; technician t1; salesman s1; salesmanager sm1; m1.pay(); m1.display(); t1.pay(); t1.display(); s1.pay(); s1.display(); sm1.pay(); sm1.display();