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

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

1、国家二级 C+机试(操作题)模拟试卷 457及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 progl下的工程 progl,该工程中包含程序文件 main cpp,其中有 Salary(“工资 ”)类和主函数 main的定义。程序中位于每个 “ ERROR*found*”之后的一行语句行有错误,请加以改正。改正后程序的输出结果应为: 应发合计: 3500应扣合计: 67 5 实发工资: 3432 5 注意:只修改每个 “ ERROR*found*”下的那一行,不要改动程序中的其他内容。 #include using namespace std; class S

2、alary public: Salary(const char * id, double the_base, double the_bonus, double the_tax) ERROR *found* : the base(base), the bonus(bonus), the tax(tax) staff id=new charstrlen(id)+1; strcpy(staff id, id); ERROR*found* Salary()delete*staff_id; double getGrosSPay()const return base+bonus; 返回应发项合计 doub

3、le getNetPay()constreturn getGrossPay()-tax; )返回实发工资额 private: char*staff id; 职工号 double base; 基本工资 double bonus; 奖金 double tax; 代扣个人所得税 ; int main() Salary pay(“888888“, 3000 0, 500 0, 67 50); tout class vehicle private: int MaxSpeed; int Weight; public: *found* vehicle(int maxspeed, int weight): _

4、 vehicle(); int getMaxSpeed() return MaxSpeed; ) int getWeight() return Weight; ) , *found* class bicycle: _public vehicle private: int Height; public: bicycle(int maxspeed, int weight, int height): vehicle (maxspeed, weight), Height (height)f) int getHeight()return Height; ); ; *found* class motorc

5、ar: _public vehicle private: int SeatNum; public: motorcar(int maxspeed, int weight, int seatnum): vehicle (maxspeed, weight), SeatNum (seatnum) int getSeatNum()f return Seat-Num; ); ; *found* class motorcycle : _ public: motorcycle(int maxspeed, int weight, int height): vehicle(maxspeed, weight),bi

6、cycle(maxspeed, weight, height), motorcar(maxspeed, weight, 1) ; void main() motorcycle a(80, 150, 100); cout using namespace std; #include“prj3 h“ int FriFun(FriFunClass x) *333* 由于函数 FriFun()是类 FriFunClass的友元函数,所以它可以直接访问 a和 b *666* int main() FriFunClass n(10, 20); if(FriFun(n) cout“Common denomin

7、ator is“ FriFun(n) “ n“; else cout“No common denominator n“; writeToFile(“); return 0 ; 国家二级 C+机试(操作题)模拟试卷 457答案与解析 一、基本操作题 1 【正确 答案】 (1): base(the_base), bonus(the_bonus), tax(the_tax) (2) Salary()delete staff_id; (3)cout“实发工资: “pay getNetPay()endl: 【试题解析】 (1)主要考查考生对构造函数的掌握情况,构造函数的成员初始列表要把形参放在括号内。

8、(2)主要考查考生对析构函数的掌握情况,析构函数使用 delete释放指针, delete后要跟标识符 “”。 (3)主要考查考生对 成员函数调用的掌握情况,调用类的成员函数使用标识符“ ”,而不是作用域符 “: ”。 二、简单应用题 2 【正确答案】 (1)MaxSpeed(maxspeed), Weight(weight); (2)virtual (3)virtual (4)public bicycle, public motorcar 【试题解析】 (1)主要考查考生对构造函数的掌握,构造函数使用初始化列表来对私有成员 MaxSpeed和 Weight初始化。 (2)主要考查考生对派生类

9、的掌握,题目要求将 vehicle作为虚基类,避免二义性问题。因此在这里添加 virtual使 vehicle成为虚基类。 (3)主要考查考生对派生类的掌握,题目要求以 motorcar和 bicycle作为基类,再派生出 motorcycle类。在主函数中可以看到 motorcycle类的实例 a调用getHeight函数和 getSeatNum函数,由此可知这两个基类都是公有继承,因此得出语句: public bicycle, public motorcar。 三、综合应用题 3 【正确答案】 int min=x ax b?x a: x b; 此处为取出 x a与 x b中的最小值 for(int i =2; i =min; i+) 从 i到 min遍历数组 if(x a i=0&x b i=0) 如 i能同时整除 x a来的 x b,则仅回 i return i; return-1 ; 【试题解析】 主要考查考生对友元函数的掌握情况,友元函数可以访问类的私有数据成员,题目要求函数求出两个数据成员的大于 1的最小公因子,从 2开始往上算,因此要同时可以被两个私有成员整除,这里用取余符号完成,取余为 0即为整除。

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

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

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