【计算机类职业资格】二级C++真题1及答案解析.doc

上传人:registerpick115 文档编号:1324431 上传时间:2019-10-17 格式:DOC 页数:11 大小:50.50KB
下载 相关 举报
【计算机类职业资格】二级C++真题1及答案解析.doc_第1页
第1页 / 共11页
【计算机类职业资格】二级C++真题1及答案解析.doc_第2页
第2页 / 共11页
【计算机类职业资格】二级C++真题1及答案解析.doc_第3页
第3页 / 共11页
【计算机类职业资格】二级C++真题1及答案解析.doc_第4页
第4页 / 共11页
【计算机类职业资格】二级C++真题1及答案解析.doc_第5页
第5页 / 共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

20、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),

21、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

22、scount 比购买 simple 节省多少钱 cout“Saving is“_endl; else cout“Discount item is not cheaper./n“; return 0; (分数:30.00)_正确答案:()解析:(1)return this-bill()-other.bill() (2)return fraction*price (3)return first.bill()second.bill() /bill()是返回商品的实际价格 (4)simple.savings(discount) 考点 主要考查类的数据成员、类的成员、虚函数与多态性以及重载关系运算符。

23、解析 Sale 类有一个数据成员:保护成员 price,表示商品的价格;两个构造函数:默认构造函数 Sale()将类成员 price 初始化为 0,构造函数 Sale(double the_price)将成员 price 初始化为 the_price;两个成员函数:bill()函数是一个虚函数,返回 price 的值;savings()函数返回参数 other 所引用的对象比当前对象便宜的差价 DiscountSale 类继承 Sale 类,它有两个数据成员:保护成员 price 继承于 Sale 类,表示商品价格,保护成员 discount 表示商品折扣百分比;两个成员函数:savings(

24、)函数继承于 Sale 类;bill()函数继承Sale 类并被改写。 main()函数中,首先构造两个类对象:Sale 类对象 simple 和 DiscountSale 类对象 discount,其中simple 对象的 price 被赋值为 10.00;discount 对象的 price 被赋值为 11.00,成员 discount 被赋值为90,即折扣为 90% (1)if 语句的条件中,将 discount 与 simple 进行比较,如果 main()主数的 discount 小于 simple,那么说明 discount 的实际价格比 simple 价格便宜;所以需要重载“”运

25、算符,题意中重载的“”运算符函数接收两个 Sale 类对象的引用,通过对象的引用,比较两个对象的实际价格,所以重载“”运算符函数体中,若 first 对象引用的商品实际价格小于 second 对象引用的商品实际价格,那么函数值返回true,可知函数体中需要补充的语句如下: return first.bill()second.bill();/bill()是返回商品的实际价格 (2)由于 bill()是虚函数,且重载“”运算符函数的参数是引用,所以调用 bill()函数时,使用了多态机制,引用的对象是 Sale 类对象,调用 Sale 类中的 bill()函数,引用的对象是 DiscountSal

26、e 类对象,那么将调用 DiscountSale 类的 bill()函数,bill()函数体中,需要返回的是商品的实际价格,即折扣完的价格,所以需要将返回打过折的价格:DiscountSale 类中,bill()函数体中需要补充的语句如下: return fraction*price (3)savings()函数需要计算当前对象比 other 引用的对象在价格上贵多少,所以 savings()函数体中需要补充的语句如下: return this-bill()-other.bill() 同样,this 指针引用当前对象,other 是引用对象,所以可以与 bill()虚函数一起使用多态机制,获得

27、当前对象和 other 对象的实际价格,再进行差值计算 (4)main()函数中,程序判断 discount 小于 simple 后,输出提示信息,再调用 savings()函数输出discount 和 simple 的差值,这里需要计算 discount 比 simple 节省多少钱,所以需要将 simple 当做当前对象,discount 当做参数传入 savings()函数,所以补充语句如下: simple.savings(discount)三、综合应用题(总题数:1,分数:40.00)3.使用 VC6 打开 proj3 下的工程 proj3,其中定义了一个字符串变量类 StringVa

28、r。类成员的说明在程序注释中。请在/*333*和/*666*之间填写 StringVar 成员函数和友元函数的实现代码。在 main 函数中给出了一组测试数据,运行时输入: Hello Kitty 此情况下程序的输出应该是: Hello Kitty Borg Borg 注意:只需在/*333*和/*666*之间填入所编写的若干语句,不要改动程序中的其他内容。 /StringVar.h #includeiostream #includecstdlib #includecstddef #includecstring using namespace std; void writeToFile(con

29、st char*path); class StringVar public: StringVar(int size);/构造函数,size 为字符串长度(字符个数)初始值;字符串内容初始化为空串 StringVar(const char a);/构造函数,用参数数组 a 的内容初始化当前对象 StringVar(const StringVar/复制构造函数 StringVar()deletevalue;);/析构函数 int length()constreturn strlen(value); /从输入流 ins 输入一个字符串,其中可以包括空格 void input_line(istream

30、 /返回字符串首地址 char*getValue()constreturn value; private: char*value;/字符串首地址 int max_length;/字符串最大长度(字符个数最大值) ; /将 the_string 通过输出流 outs 输出 ostream /main.cpp #includeiostream #includestring #include“StringVar.h“ /*333* /*666* int main() StringVar name1(30),name2(“Borg“); name1.input_line(cin); StringVar

31、 name3(name2); coutname1endl; coutname2endl; coutname3endl; writeToFile(“./“); return 0; /writeToFile.cpp #includeiostream #includefstream #includesstream #includestring using namespace std; #include“StringVar.h“ void writeToFile(const char*path) char filename30; strcpy(filename,path); strcat(filena

32、me,“out.dat“); ofstream fout(filename); istringstream is(string(“Jenny Zheng“); StringVar name1(40),name2(“John“); name1.input_line(is); StringVar name3(name2); foutname1name2name3; fout.close(); (分数:40.00)_正确答案:()解析:StringVar:StringVar(int size):max_length(size)/使用成员初始化列表初始化 max_length value=new ch

33、arsize; value0=“/0“; StringVar:StringVar(const char a) max_length=strlen(a)+1; value=new charmax_length; strcpy(value,a); StringVar:StringVar(const StringVar value=new charstrlen(strobj.value)+1; strcpy(value,strobj.value); void StringVar:input_line(istream char ch; while(imax_length-1 valuei=“/0“;

34、ostream return outs; 考点 本题考查构造函数和析构函数的定义、类的成员函数、重载流运算符和插入运算符和预定义流对象。 解析 由 StringVar.h 这个头文件可知:头文件中定义了 StringVar 类,它包含的成员信息如下: 两个数据成员:char*类型的 value,指向字符串的首地址;int 类型的 max_length,表示字符串的最大长度。两个构造函数:接收 int 类型的构造函数和接收 const char a的构造函数。一个复制构造函数:用参数 strobj 对象的成员值给当前对象的成员赋值;析构函数:使用 delete释放 value 指向的地址空间;l

35、ength()成员函数:返回成员 value 指向的字符串长度;input_line()成员函数:使用输入流参数输入一个字符串,保存到 value 指向的内存空间;getValue()成员函数:常成员函数,返回 value 的值。 另外类定义体外部还声明一个重载“”运算符函数,将 StringVar 对象中 value 指向的字符串输出,并返回输出流 outs 的引用 StringVar.h 中,已经给出的定义有:析构函数、length()函数和 getValue()函数,所以需要我们完成的函数定义有:构造函数、复制构造函数、input_line()函数和重载的“”运算符函数 StringVa

36、r(int size); 题意指出,size 为字符串长度(字符个数)初始值,字符串内容初始化为空串,所以需要使用 size 设置成员 max_length,为 value 申请 size 个字符的存储空间,并把 value 的第一个字符设置为“/0“表示空串,这样保证析构函数的 delete也是正确的。 StringVar(const char a); 题意指出,使用参数数组 a 的内容初始化对象的成员,即将数组 a 中的内容,存放到 value 指向的内存空间,由于 value 是使用 new 分配的(保证析构函数的 delete是正确的),所以不能简单的把数组 a 的首地址赋给 valu

37、e,需要将数组 a 的内容复制到 value 指向的内存空间中,另外成员 max_length,初值最小必须是 value 指向的内存空间长度,即 value 指向的字符串长度+1(题意中 max_length 表示字符个数的最大值,所以需要包含字符串结束符“/0“)(此构造函数的形参必须传入的是字符串,如果是字符数组,必须传入数组的长度,以防数组 a 的最后一个字符不是结束符“/0“,此时 strlen()函数获取的长度,就不是字符数组 a 的字符个数) StringVar(const StringVar 复制构造函数需要将形参 strobj 的成员赋给当前调用该复制构造函数的对象成员,在赋

38、值 value 时,同样需要为 value 分配足够的内存空间。 input_line(istream input_line()函数需要使用形参 ins 输入流,输入一个字符串存放到 value 中,题意要求可以输入空格,所以我们不能直接用输入流 ins 和输入运算符,因为输入运算符会丢弃空白符、空格符和制表符,可以使用输入流 ins 的成员函数:get()和 while 循环,逐个输入字符存放到 value 下标 i 的字符中,直到输入的字符为换行符或 value 存满(i=max_length)为止,当这两个条件满足其一,那么就应该停止输入,另外需要注意:value 指向的字符串结尾,必须

39、包含空字符“/0“,所以下标 max_length-1 必须是空字符,循环输入时,i 取值最大为 max_length-2。 ostream 输出运算符重载函数,题意要求将 the_string 通过输出流 outs 输出,由于 the_string 是 StringVar 对象,所以输出的应该是对象的成员 value 指向的字符串,根据返回值可知,函数必须返回输入流 ostream的引用;另外由于第一形参是输入流 outs,所以无法通过 StringVar 对象来调用,该函数不是类的成员函数,函数定义时,不能限定 StringVar 作用域,由于该函数不是类的成员函数,所以使用输出流 outs输出 value 指向的字符串时,不能直接使用私有成员 value,而是应该使用 getValue()公有成员函数,返回 value 的值,再输出。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1