【计算机类职业资格】二级C++真题1及答案解析.doc
《【计算机类职业资格】二级C++真题1及答案解析.doc》由会员分享,可在线阅读,更多相关《【计算机类职业资格】二级C++真题1及答案解析.doc(11页珍藏版)》请在麦多课文档分享上搜索。
1、二级 C+真题 1 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.使用 VC6 打开 proj1 下的工程 proj1,其中定义了一个 CD 类。程序中位于每个/ERROR*found*下的语句行有错误,请加以更正,不得修改程序的其他部分。更正后程序的输出应该是: 歌唱祖国 30 义勇军进行曲 95 注意:只能修改每个/ERROR*found*下的那一行,不要改动程序中的其他内容。 #includeiostream #includecstring using namespace std; class CD char name20; in
2、t number; public: void init(char*aa,int bb) /ERROR*found* name=aa; number=bb; char*getName() /ERROR*found* return*name; int getNumber()return number;) void output() /ERROR*found* coutname20“numberendl; ; void main() CD dx,dy; dx.init(“歌唱祖国“,30); dy.init(“义勇军进行曲“,3*dx.getNumber()+5); dx.output(); dy.
3、output(); (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.使用 VC6 打开 proj2 下的工程 proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale),用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale 类继承了 sale 类。类的主要数据成员的含义和成员函数的功能要求在程序注释中有说明。请在程序中的横线处填写适当的代码,然后删除横线,完成程序的功能。此程序的正确输出结果应为: Discount item is cheaper. Saving is 0.
4、1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。 #includeiostream using namespace std; class Sale public: Sale();/默认构造函数,将 price 初始化为 0 Sale(double the_price);/构造函数,用 the price 初始化 price virtual double bill()const;/返回当前商品的价格(基本价) double savings(const Sale/返回参数 other 所引用的对象比当前对象便宜的差价protected: double price;/商品的基本价格(
5、不打折的价格) ; Sale:Sale():price(0) Sale:Sale(double the_price): price(the_price) double Sale:bill()const return price; double Sale:savings(const Sale/返回当前对象价格比 other 贵多少的差价 class DiscountSale: public Sale/打折销售类继承销售类 public: DiscountSale();/默认构造函数,将 discount 初始化为 0 DiscountSale(double the_price,double th
6、e_discount);/构造函数,the_price 是基本价格;the_discount 是折扣百分比 virtual double bill()const;/返回本商品销售价格(即打折以后的实际售价,覆盖了基类的 bill 函数) protected: double discount;/折扣百分比。例如降价至原价的 70%,此成员值应为 70 ; DiscountSale:DiscountSale(): discount(0) DiscountSale:DiscountSale(double the_price,double the_discount) :Sale(the_price),
7、discount(the_discount) double DiscountSale:bill()const double fraction=discount/100; /*found* _;/返回本对象打折以后的实际售价 bool operator(const Sale/判断是否 first 价格低于 second 价格 int main() Sale simple(10.00); DiscountSale discount(11.00,90); if(discountsimple) cout“Discount item is cheaper./n“; /*found* /这里输出购买 di
8、scount 比购买 simple 节省多少钱 cout“Saving is“_endl; else cout“Discount item is not cheaper./n“; return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.使用 VC6 打开 proj3 下的工程 proj3,其中定义了一个字符串变量类 StringVar。类成员的说明在程序注释中。请在/*333*和/*666*之间填写 StringVar 成员函数和友元函数的实现代码。在 main 函数中给出了一组测试数据,运行时输入: Hello Kitty 此情况下程序的输出应该是: He
9、llo Kitty Borg Borg 注意:只需在/*333*和/*666*之间填入所编写的若干语句,不要改动程序中的其他内容。 /StringVar.h #includeiostream #includecstdlib #includecstddef #includecstring using namespace std; void writeToFile(const char*path); class StringVar public: StringVar(int size);/构造函数,size 为字符串长度(字符个数)初始值;字符串内容初始化为空串 StringVar(const c
10、har a);/构造函数,用参数数组 a 的内容初始化当前对象 StringVar(const StringVar/复制构造函数 StringVar()deletevalue;);/析构函数 int length()constreturn strlen(value); /从输入流 ins 输入一个字符串,其中可以包括空格 void input_line(istream /返回字符串首地址 char*getValue()constreturn value; private: char*value;/字符串首地址 int max_length;/字符串最大长度(字符个数最大值) ; /将 the_
11、string 通过输出流 outs 输出 ostream /main.cpp #includeiostream #includestring #include“StringVar.h“ /*333* /*666* int main() StringVar name1(30),name2(“Borg“); name1.input_line(cin); StringVar name3(name2); coutname1endl; coutname2endl; coutname3endl; writeToFile(“./“); return 0; /writeToFile.cpp #includei
12、ostream #includefstream #includesstream #includestring using namespace std; #include“StringVar.h“ void writeToFile(const char*path) char filename30; strcpy(filename,path); strcat(filename,“out.dat“); ofstream fout(filename); istringstream is(string(“Jenny Zheng“); StringVar name1(40),name2(“John“);
13、name1.input_line(is); StringVar name3(name2); foutname1name2name3; fout.close(); (分数:40.00)_二级 C+真题 1 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.使用 VC6 打开 proj1 下的工程 proj1,其中定义了一个 CD 类。程序中位于每个/ERROR*found*下的语句行有错误,请加以更正,不得修改程序的其他部分。更正后程序的输出应该是: 歌唱祖国 30 义勇军进行曲 95 注意:只能修改每个/ERROR*found*下的那一行,不
14、要改动程序中的其他内容。 #includeiostream #includecstring using namespace std; class CD char name20; int number; public: void init(char*aa,int bb) /ERROR*found* name=aa; number=bb; char*getName() /ERROR*found* return*name; int getNumber()return number;) void output() /ERROR*found* coutname20“numberendl; ; void
15、main() CD dx,dy; dx.init(“歌唱祖国“,30); dy.init(“义勇军进行曲“,3*dx.getNumber()+5); dx.output(); dy.output(); (分数:30.00)_正确答案:()解析:(1)strcpy(name,aa); (2)return name; (3)coutname“numberendl; 考点 主要考查字符数组的赋值、函数返回类型、提取运算符和插入运算符。 解析 程序定义 CD 类,它包含两个数据成员:字符数组 name 和整型变量 number;还包含四个公有成员函数,init()函数接收两个参数,用参数对数据成员进行
16、赋值;getName()函数返回数据成员 name;getNumber()函数返回数据成员 number;output()函数将数据成员 name 和 number 输出。CD 类的定义体中,有三个错误:(1)init()函数将形参 aa 赋给 name,由于 name 是字符数组,所以不能通过简单的赋值运算符进行赋值,应该使用 strcpy()函数将形参 aa 指向的字符串拷贝到 name 中。 (2)getName()函数的返回值为 char*类型,所以函数体的 return 语句应该返回 name,而不是 name 指向的字符串。 (3)output()函数需要输出两个数据成员,输出字符
17、数组时,只需要给出数组名 name 即可。二、简单应用题(总题数:1,分数:30.00)2.使用 VC6 打开 proj2 下的工程 proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale),用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale 类继承了 sale 类。类的主要数据成员的含义和成员函数的功能要求在程序注释中有说明。请在程序中的横线处填写适当的代码,然后删除横线,完成程序的功能。此程序的正确输出结果应为: Discount item is cheaper. Saving is 0.
18、1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。 #includeiostream using namespace std; class Sale public: Sale();/默认构造函数,将 price 初始化为 0 Sale(double the_price);/构造函数,用 the price 初始化 price virtual double bill()const;/返回当前商品的价格(基本价) double savings(const Sale/返回参数 other 所引用的对象比当前对象便宜的差价protected: double price;/商品的基本价格(
19、不打折的价格) ; Sale:Sale():price(0) Sale:Sale(double the_price): price(the_price) double Sale:bill()const return price; double Sale:savings(const Sale/返回当前对象价格比 other 贵多少的差价 class DiscountSale: public Sale/打折销售类继承销售类 public: DiscountSale();/默认构造函数,将 discount 初始化为 0 DiscountSale(double the_price,double th
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
5000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 职业资格 二级 答案 解析 DOC
