【计算机类职业资格】C++语言笔试-12及答案解析.doc
《【计算机类职业资格】C++语言笔试-12及答案解析.doc》由会员分享,可在线阅读,更多相关《【计算机类职业资格】C++语言笔试-12及答案解析.doc(11页珍藏版)》请在麦多课文档分享上搜索。
1、C+语言笔试-12 及答案解析(总分:100.00,做题时间:90 分钟)一、操作题(总题数:3,分数:100.00)1.请使用 VC6 或使用答题菜单打开考生文件夹 proj1 下的工程 proj1,此工程中包含一个源程序文件main.cpp,其中有类 Book(“书”)和主函数 main 的定义。程序中位于每个“/ERROR*found*”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:书名:C+语句程序设计总页数:299已把“C+语言程序设计”翻到第 50 页已把“C+语言程序设计”翻到第 51 页已把“C+语言程序设计”翻到第 52 页已把“C+语言程序设计”翻到第 51 页
2、已把书合上。当前页:0注意:只修改每个“/ERROR *found*”下的那一行,不要改动程序中的其他内容。#includeiostreamusing namespace std;class Bookchar*title;int num_pages;/页数int cur_page;/当前打开页面的页码,0 表示书未打开public:/ERROR *found*Book(const char*theTitle,int pages)num_pages(pages)title=new charstrlen(theTitle)+1;strcpy(title,theTitle);coutendl“书名:
3、“title“总页数:“num_pages;Book()deletetitle;)bool isClosed()constreturn cur_page=0; /书合上时返回 true,否则返回 falsebool isOpen()constreturn! isClosed();)/书打开时返回 true,否则返回 falseint numOfPages()const return num_pages;) /返回书的页数int currentPage()const return cur_page;) /返回打开页面的页码/ERROR *found*void openAtPage (int pa
4、ge_no) const( /把书翻到指定页coutendl;if(page_no1 |page_nonum_pages)cout“无法翻到第“cur_page“页。“;close();else cur_page=page_no;cout“已把“title“”翻到第“cur_page“页“;void openAtPrevPage()openAtPage(cur_page-1);/把书翻到上一页void openAtNextPage()openAtPage(cur_page+1);/把书翻到下一页void close() /把书合上coutendl;if(isClosed()cout“书是合上的
5、。“;else/ERROR*found*num_pages=0;cout“已把书合上。“;coutendl;int main()Book book(“C+语言程序设计“,299);book.openAtPage(50);book.openAtNextPage();book.openAtNextPage();book.openAtPrevPage();book.close();cout“当前页:“book.current-Page()endl;return 0;(分数:30.00)_2.请使用 VC6 或使用答题菜单打开考生文件夹 proj2 下的工程 proj2。此工程中包含一个源程序文件ma
6、in.cpp,其中有“房间”类 Room 及其派生出的“办公室”类 Office 的定义,还有主函数 main 的定义。请在程序中“/*found*”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:办公室房间号:308办公室长度:5.6办公室宽度:4.8办公室面积:26.88办公室所属部门:会计科注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。#includeiostreamusing namespace std;class Room /“房间”类int room no; /房间号double length; /
7、房间长度(m)double width; /房间宽度(m)public:Room(int the room_no.double the_length,double the_width):room_no(the_room_no),length(the_length),width(the width)int theRoomNo()constreturn room_no;/返回房间号double theLength()const returnlength;) /返回房间长度double theWidth()const returnwidth; /返回房间宽度/*found*double theAre
8、a()const_/返回房间面积(矩形面积);class Office:public Room /“办公室”类 char*depart; /所属部门public:Office (int the_room_no.double the_length,double the_width,constchar*the_depart)/*found*:_depart=new charstrlen(the depart)+1;/*found*strcpy(_);Office()deletedepart;const char*theDepartment()constreturn depart;)/返回所属部门;
9、int main()/*found*Office_;cout“办公室房间号:“an office.theRoomNo()endl“办公室长度:“an office.the-Length()endl“办公室宽度:“an office.the-Width()endl“办公室面积:“an_office.theArea()endl“办公室所属部门:“an office.theDepartment()endl;return 0;(分数:30.00)_3.请使用 VC6 或使用答题菜单打开考生文件夹 proj3 下的工程文件 proj3。本题创建一个小型字符串类,字符串长度不超过 100。程序文件包括 p
10、roj3.h、proj3.cpp、writeToFile.obj。补充完成重载赋值运算符函数,完成深复制功能。屏幕上输出的正确结果应该是:Hello!Happy new year!要求:补充编制的内容写在“/*333*”与“/*666*”两行之间。不得修改程序的其他部分。注意:程序最后调用 writeToFile 函数,使用另一组不同的测试数据,将不同的运行结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件。/proj3.h#includeiostream#includeiomanipusing namespace std;class MiniStr
11、ingpublic:friend ostream return output;friend istream /用于输入的临时数组temp0=/0; /初始为空字符串inputsetw(100)temp;int inLen=strlen(temp);/输入字符长度if(inLen!=0)s.length=inLen; /赋长度if(s.sPtr!=0)deletes.sPtr;/避免内存泄漏s.sPtr=new chars.length+1;strcpy(s.sPtr,temp);/如果 s 不是空指针,则复制内容else s.sPtr0=/0;/如果 s 是空指针,则为空字符串return i
12、nput;void modString const char *string2)/更改字符串内容if(string2 !=0)/如果 string2 不是空指针,则复制内容if (strlen(string2)!=length)length=strlen(string2);deletesPtr;sPtr=new char length+1;/分配内存strcpy(sPtr,string2);else sPtr0=/0;/如果 string2 是空指针,则为空字符串MiniStringMiniString(const char*s=“ “):length(s!=0)?strlen(s):0)/构
13、造函数sPtr=0;if(length!=0)setString(s);MiniString()/析构函数deletesPtr;private:int length; /字符串长度char*sPtr; /指向字符串起始位置void setString ( const char *string2)/辅助函数sPtr=new charstrlen(string2)+1; /分配内存if(string2 !=0)strcpy(sPtr,string2);/如果 string2 不是空指针,则复制内容else sPtr0=/0;/如果 string2 是空指针,则为空字符串;/proj3.cpp#in
14、cludeiostream#includeiomanipusing namespace std;#include “proj 3.h“MiniStringvoid writeToFile(const char*);str2=str1;/使用重载的赋值运算符str2.modString(“Happy new year!“);coutstr1/n;coutstr2/n;writeToFile(“ “);return 0;(分数:40.00)_C+语言笔试-12 答案解析(总分:100.00,做题时间:90 分钟)一、操作题(总题数:3,分数:100.00)1.请使用 VC6 或使用答题菜单打开考生
15、文件夹 proj1 下的工程 proj1,此工程中包含一个源程序文件main.cpp,其中有类 Book(“书”)和主函数 main 的定义。程序中位于每个“/ERROR*found*”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:书名:C+语句程序设计总页数:299已把“C+语言程序设计”翻到第 50 页已把“C+语言程序设计”翻到第 51 页已把“C+语言程序设计”翻到第 52 页已把“C+语言程序设计”翻到第 51 页已把书合上。当前页:0注意:只修改每个“/ERROR *found*”下的那一行,不要改动程序中的其他内容。#includeiostreamusing name
16、space std;class Bookchar*title;int num_pages;/页数int cur_page;/当前打开页面的页码,0 表示书未打开public:/ERROR *found*Book(const char*theTitle,int pages)num_pages(pages)title=new charstrlen(theTitle)+1;strcpy(title,theTitle);coutendl“书名:“title“总页数:“num_pages;Book()deletetitle;)bool isClosed()constreturn cur_page=0;
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
5000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 职业资格 语言 笔试 12 答案 解析 DOC
