[计算机类试卷]国家二级C++机试(操作题)模拟试卷304及答案与解析.doc

上传人:花仙子 文档编号:497382 上传时间:2018-11-28 格式:DOC 页数:8 大小:38KB
下载 相关 举报
[计算机类试卷]国家二级C++机试(操作题)模拟试卷304及答案与解析.doc_第1页
第1页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷304及答案与解析.doc_第2页
第2页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷304及答案与解析.doc_第3页
第3页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷304及答案与解析.doc_第4页
第4页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷304及答案与解析.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、国家二级 C+机试(操作题)模拟试卷 304及答案与解析 一、基本操作题 1 使用 VC6打开考生文件夹 proj1下的工程 proj1,其中定义了一个 CD类。程序中位于每个 ERROR *found*下的语句行有错误,请加以更正,不得修改程序的其他部分。更正后程序的输出应该是: 歌唱祖国 30 义勇军进行曲 95 注意:只能修改每个 ERROR *found*下的那一行,不要改动程序中的其他内容。 #include iostream #include cstring using namespace std; C1ass CD char name20; int number; public:

2、 void init(char * aa, intbb) ERROR *found* name aa; number bb; char水 getName() ERROR *found* return * name; int getNumber()return number; void output() ERROR *found* cout name20 number end1; ; void main() CD dx, dy; dx init(“歌唱祖国 “, 30); dy init(“义勇军进行曲 “, 3*dx getNumber() 5); dx output(); dy output

3、(); 二、简单应用题 2 使用 VC6打开考生文件夹 proj2下的工程 proj2,其中有两个类:一是销售类(sale),用于表示按照一件商品的基本价格进行销售;另一个是打折销售类(DiscountSale),用于表示在基本价格基础上按一个折扣比例进行销售。DiscountSale类继承了 sale类。类的主要数据成员的含义和成员函数的功能要求在程序注释中有说明。请在程序中的横线处填写适当的代码,然后删除横线,完成程序的功能。此程序的正确输出结果应为: Discount item is cheaper Saving is 0 1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。

4、 #include iostream 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)const;返回参数 other所引用的对象比当前对象便宜的差价 protected: double price;商品的基本价格 (不打折的价格 ) ; Sale Sale

5、(): price C0) Sale Sale(double the_price): price(the_price) double Sale bill()const return price; double Sale savings(const Sale other)const ERROR *found* _返回当前对象价格比 other贵多少的差价 class DiscountSale: public Sale打折销售类继承销售类 public: DiscountSale();默认构造函数,将 discount初始化为 0 DiScountSale(double the_price, do

6、uble the_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_

7、price), discount (the_discount) double DiscOuntSale bill () const double fraction discount 100; *found* _;返回本对象打折以后的实际售价 bool operator (const Sale first, const Sale&Second) *found* _;判断是否 first价格低于 second价格 int main() Sale Simple(10 00); DiscountSale diScount(11 00, 90); if(discount simple) cout “Di

8、Scount item ischeaper n“; *found* 这里输出购买 discount比购买 simple节省多少钱 cout “Saving is“ _ end1; else cout “Discount item isnot cheaper n“; return 0; 三、综合应用题 3 使用 VC6打开考生文件夹 proj3下的工程 proj3,其中定义了一个字符串变量类StringVar。类成员的说明在程序注释中。请在 *333*和*666*之间填写 StringVar成员函 数和友元函数的实现代码。在main函数中给出了一组测试数据,运行时输入: Hello Kitty

9、此情况下程序的输出应该是: Hello Kitty Borg Borg 注意:只需在 *333*和 *666*之间填入所编写的若干语句,不要改动程序中的其他内容。 StringVar h #include iostream #include cstdlib #include cstddef #include cstring using namespace std; void writeToFile(const char * path); class StringVar public: StringVar(int size);构造函数, size为字符串长度 (字符个数 )初始值;字符串内容初始

10、化为空串 StringVar (const char a);构造函数,用参数数组 a的内容初始化当前对象 StringVar(const StringVar&strobj);复制构造函数 StringVar()deletevalue; ;析构函数 int length() constreturn strlen(value); 从输入流 ins输入一个字符串,其中可以包括空格 void input_line(istream&ins); 返回字符串首地址 char * getValue()constreturn value; private: char * value;字符串首地址 int max

11、_length; 字符串最大长度 (字符个数最大值 ) ; 将 the_string通过输出流 outs输出 ostream&operator (ostream&outs, const StringVar the_string); main cpp #include iostream #include string #include“StringVar h“ *333* *666* int main() StringVar namel(30), name2(“Borg“); name1 input line(cin); StringVar name3(name2); cout name1 en

12、d1; cout name2 end1; cout name3 end1; writeToFile(“、 “); return 0; writeToFile cpp #include iostream #include fstream #include sstream #include string using namespace std; #include“StringVar h“ void writeToFile(const char * path) char filename30; strcpy(filename, path); strcat(filename, “out dat“);

13、ofstream fout(filename); istringstream is(string(“Jenny Zheng“); StringVar namel(40), name2(“John“); name1 input_line(is); StringVar name3(name2); fout name1 name2 name3; fout close(); 国家二级 C+机试(操作题)模拟试卷 304答案与解析 一、基本操作题 1 【正确答案】 (1)strcpy(name, aa); (2)return name; (3)cout name number end1; 【试题解析】

14、程序定义 CD类,它包含两个数据成员:字符数组 name和整型变量 number;还包含四个公有成员函数, init()函数接收两个参数,用参数对数据成员进行赋值; getName()函数返回数据成员 name; getNumber()函数返回数据成员number; output()函数将数据成员 name和 number输出。 CD类的定义体中,有三个错误: (1)init()函数将形参 aa赋给 name,由于 name是字符数组,所以不能通过简单的赋值运算符进行赋值,应该使用 strcpy()函数将形 参 aa指向的字符串拷贝到name中。 (2)getName()函数的返回值为 cha

15、r * 类型,所以函数体的 return语句应该返回name,而不是 name指向的字符串。 (3)output()函数需要输出两个数据成员,输出字符数组时,只需要给出数组名name即可。 二、简单应用题 2 【正确答案】 (1)return this bill() other bill() (2)return fraction * price (3)return first bill() second bill() bill()是返回商品的实际价格 (4)simple savings(discount) 三、综合应用题 3 【正确答案】 StringVar StringVar(int siz

16、e): max_length(size)使用成员初始化列表初始化 max_length value new charsize; value0 0; StringVar StringVar(constchar a) max_length strlen(a) 1; value new charmax_length; strcpy(value, a); StringVar StringVar(const StringVar&strobj) max_length strobj max_length; value new charstrlen(strobj value) 1; strcpy(value, strobj value); void StringVar input_line(istream&ins) int i 0; char ch; while(i max length 1 (ch ins get()! n) valuei ch; valuei O; ostream&operator (ostream&outs, const StringVar the_string) outs the string getValue() end1; return outs;

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

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

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