收藏 分销(赏)

2023年面向对象程序设计C++山师习题答案.doc

上传人:二*** 文档编号:4519771 上传时间:2024-09-26 格式:DOC 页数:17 大小:59.04KB
下载 相关 举报
2023年面向对象程序设计C++山师习题答案.doc_第1页
第1页 / 共17页
本文档共17页,全文阅读请下载到手机保存,查看更方便
资源描述
第七章习题答案 一、选择填空 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,40) 4、运营该程序输出结果如下所示。 D2::display() pri1=4,pri2=5 pri4=6 pri12=7 D2::display() pri1=12 ,pri2=9 pri4=7 pri12=8 5、该程序输出结果如下所示: D2::display() pri1=1,pri2=4 pri4=6 pri12=7 D2::display() pri1=9,pri2=8 pri4=7 pri12=8 6、该程序输出结果如下所示: base class base class base class derive1 class derive2 class 五、按下列规定编写程序。 1、程序内容如下所示。 #include <iostream.h> #include <iomanip.h> class person { int no; char name[10]; public: void input() { cout<<" 编号:";cin>>no; cout<<" 姓名:";cin>>name; } void disp() { cout<<" 编号:"<<no<<endl; cout<<" 姓名:"<<name<<endl; } }; class student:public person { private: char depart[6]; int degree; public: void input() { person::input(); cout<<" 班号:"; cin>>depart; cout<<" 成绩:"; cin>>degree; } void disp() { person::disp(); cout<<" 班号:"<<depart<<endl; cout<<" 成绩:"<<degree<<endl; } }; class teacher:public person { private: char prof[10]; char depart[10]; public: void input() { person::input(); cout<<" 职称:"; cin>>prof; cout<<" 部门:"; cin>> depart ; } void disp() { person::disp(); cout<<" 职称:"<< prof<<endl; cout<<" 部门:"<< depart <<endl; } }; void main() { student 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 string { int 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 print(){cout<<contents<<endl;} }; class edit_string:public string { int cursor; public: int get_cursor_pos(){return cursor;} void move_cursor(int how_much){cursor=how_much;} int add_at_cursor(string *new_text); int repl_at_cursor(string *new_text); void dele_at_cursor(int how_much); }; int string::set_contents(int in_length,char *in_contents) { length=in_length; if (!contents) delete contents; contents=new char[length+1]; strcpy(contents,in_contents); return length; } int string::set_contents(char *in_contents) { length=strlen(in_contents); if (!contents) delete contents; contents=new char[length+1]; strcpy(contents,in_contents); return length; } int edit_string::add_at_cursor(string *new_text) { int 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 char[m+n+1]; for (int i=0;i<cursor;i++) news[i]=cp[i]; k=i; for (int j=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); delete news; return cursor; } int edit_string::repl_at_cursor(string *new_text) { int n,m; char *pt,*news; n=new_text->get_length(); pt=new_text->get_contents(); m=this->get_length(); news=new char[m>n+cursor ? m+1 : n+cursor+1]; news=this->get_contents(); for(int i=cursor,j=0;i<n+cursor;j++,i++) news[i]=pt[j]; if (m<n+cursor) news[i]='\0'; cursor=i; set_contents(news); delete news; return cursor; } void edit_string::dele_at_cursor(int how_much) { int m; char *cp,*news; cp=this->get_contents(); m=this->get_length(); for(int i=cursor;i<m;i++) cp[i]=cp[i+how_much]; cp[i]='\0'; } void main() { string s1; edit_string s2; char *cp; s1.set_contents("Object_Oriented Programming"); 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> class vehicle { protected: int wheels; float weight; public: vehicle(int wheels,float weight); int get_wheels(); float get_weight(); float wheel_load(); void print(); }; class car:vehicle { int passenger_load; public: car(int wheels,float weight,int passengers=4); int get_passengers(); void print(); }; class truck:vehicle { int passenger_load; float payload; public: truck(int 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 vehicle::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; cout<<endl; } truck::truck(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<<"人。"<<endl; cout<<" 效率:"<<efficiency()<<endl; cout<<endl; } void main() { car car1(4,1000,5); truck tru1(10,5000,3,340000); car1.print(); tru1.print(); } 4、程序内容如下所示。 #include <iostream.h> #include <string.h> class employee { protected: int no; char name[10]; float salary; public: employee() { cout<<"职工编号:"; cin>>no; cout<<"职工姓名:"; cin>>name; salary=0; } void pay(){} void display(){} }; class technician:public employee { private: float hourlyrate; int workhours; public: technician(){hourlyrate=100;} void pay() { cout<<name<<"本月工作时数:"; cin>>workhours; salary=hourlyrate*workhours; } void display() { cout<<"兼职技术人员:"<<name<<"(编号为:"<<no\ <<")"<<"本月工资:"<<salary<<endl; } }; class salesman:virtual public employee { protected: float commrate; float sales; public: salesman(){commrate=0.04;} void pay() { cout<<name<<"本月销售额:"; cin>>sales; salary=sales*commrate; } void display() { cout<<"销售员:"<<name<<"(编号为:"<<no<<")"<<"本月工资:"<<salary<<endl; } }; class manager:virtual public employee { protected: float monthlypay; public: manager(){monthlypay=8000;} void pay(){salary=monthlypay;} void display() { cout<<"经理:"<<name<<"(编号为:"<<no<<")"<<"本月工资:"<<salary<<endl; } }; class salesmanager:public manager,public salesman { public: salesmanager() { monthlypay=5000; commrate=0.005; } void pay() { cout<<name<<"所管部门月销售量:"; cin>>sales; salary=monthlypay+commrate*sales; } void display() { cout<<"销售经理:"<<name<<"(编号为:"<<no<<")"<<"本月工资:"<<salary<<endl; } }; 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(); }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服