【计算机类职业资格】二级C++分类模拟163及答案解析.doc

上传人:inwarn120 文档编号:1324240 上传时间:2019-10-17 格式:DOC 页数:9 大小:45KB
下载 相关 举报
【计算机类职业资格】二级C++分类模拟163及答案解析.doc_第1页
第1页 / 共9页
【计算机类职业资格】二级C++分类模拟163及答案解析.doc_第2页
第2页 / 共9页
【计算机类职业资格】二级C++分类模拟163及答案解析.doc_第3页
第3页 / 共9页
【计算机类职业资格】二级C++分类模拟163及答案解析.doc_第4页
第4页 / 共9页
【计算机类职业资格】二级C++分类模拟163及答案解析.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、二级 C+分类模拟 163 及答案解析(总分:100.00,做题时间:90 分钟)一、程序改错题(总题数:1,分数:30.00)1.使用 VC6 打开 下的源程序文件 modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: Number=7 Number=12 注意:错误的语句在/*error*的下面,修改该语句即可。 #includeiostream.h class CMyClass public: /*error* CMyClass(int i): Number=i /*error* return Number; void set(int i) Number=i;

2、 void display() cout “Number=“ Number endl; private: int Number; ; void main() /*error* CMyClass* p=new CMyClass; p-display(); p-set(12); p-display(); return; (分数:30.00)_二、程序填空题(总题数:1,分数:40.00)2.使用 VC6 打开 下的源程序文件 modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数 sum(int n)计算在 n 范围内,能被 7 和 11 整除的所有整数的和(包括 n 在内)。 注意:不

3、能修改程序的其他部分,只能补充 sum ()函数。 #include iostream.h double sum(int n) void main() cout sum(80) endl; cout sum(200) endl; cout sum(300) endl; return; (分数:40.00)_三、程序设计题(总题数:1,分数:30.00)3.使用 VC6 打开 下的源程序文件 modi3.cpp,要求编写一个 CMyShape 类,含有求面积求周长等纯虚函数。然后编写一个 CMyRectangle 类和 CMyCircle 类继承 CMyShape,并实现求面积、求周长的两个函数

4、。在main()函数中测试得到下面的结果: 在 CMyShape 类构函数造内 在 CMyCircle 类构造函数内 在 CMyShape 类构造函数内 在 CMyRectangle 类构造函数内 myCircle:Area=314.159 Girth=62.8319 myRectangle:Area=900 Girth=120 具体要求如下: (1)定义求面积纯虚函数,请在注释/*1*之处添加适当的语句。 (2)定义求周长纯虚函数,请在注释/*2*之处添加适当的语句。 (3)请在注释/*3*和/*4*之处添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 #inc

5、lude iostream.h #include math.h #define PI 3.1415926 class CMyPoint public: int x, y; CMyPoint (int tx, int ty): x(tx),y(ty) ; class CMyShape public: CMyShape()cout “在 CMyShape 类构造函数内“ endl; /*1* /*2* protected: ; class CMyCircle: public CmyShape public: CMyCircle(CMyPoint i,double j):CMyShape(),arc

6、Center(i), radius(j) cout “在 CMyCircle 类构造函数内“ endl; double GetArea() return PI*radius*radius; double GetGirth() return 2*PI*radius; private: CMyPoint arcCenter; double radius; ; class CMyRectangle: public CmyShape public: CMyRectangle(CMyPoint lt,CMyPoint rb):leftTop(lt), rightBottom(rb),CMyShape()

7、 cout “在 CMyRectangle 类构造函数内“ endl; Double GetArea() int width=abs (rightBottom.x-leftTop.x); int height=abs (rightBottom.y-leftTop.y); Return width*height; double GetGirth() int width=abs (rightBottom.x-leftTop.x); int height=abs (rightBottom.y-leftTop.y); return 2*(width+height); private: CMyPoint

8、 leftTop, rightBottom; ; void main() CMyShape *myShape=NULL; CMyCircle *myCircle=new CMyCircle(CMyPoint(5,5),10); CMyRectangle *myRectangle=new CMyRectangle(CMyPoint(0,0), CMyPoint(30,30); /*3* cout “myCircle: “ “Area=“ myShape-GetArea () “/t“ “Girth=“ myShape- GetGirth() endl; /*4* cout “myRect ang

9、le: “ “Are a=“ myShape-GetArea () “/t“ “Girth=“ myShape- GetGirth() endl; (分数:30.00)_二级 C+分类模拟 163 答案解析(总分:100.00,做题时间:90 分钟)一、程序改错题(总题数:1,分数:30.00)1.使用 VC6 打开 下的源程序文件 modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: Number=7 Number=12 注意:错误的语句在/*error*的下面,修改该语句即可。 #includeiostream.h class CMyClass public:

10、/*error* CMyClass(int i): Number=i /*error* return Number; void set(int i) Number=i; void display() cout “Number=“ Number endl; private: int Number; ; void main() /*error* CMyClass* p=new CMyClass; p-display(); p-set(12); p-display(); return; (分数:30.00)_正确答案:()解析:(1)CMyClass(int i):Number(i) (2)应删除:

11、return Number; (3)CMyClass* p=new CMyClass(7); 答案考生文件夹 解析 CMyClass 类含有成员变量 Number 和多个成员函数,set(int i)函数可改变成员变量的值,display()函数在屏幕上打印成员变量的值。 (1)第 1 个标识下是声明构造函数,并使用初始化列表完成成员变量的初始化,可知第 1 标识下的初始化列表错误,正确的应该是“CMyClass(int i):Number(i)”。 (2)构造函数不能有返回值,不能用 return 来返回值,故第 2 个标识下应将“return Number;”删除。 (3)类实例在不指定构

12、造函数的情况下,调用的是默认无参数的构造函数,此时成员变量 Number 是不确定的,在定义对象时应使用已定义的构造函数,根据输出结果可知 p 指向的对象的 Number 初始化值为 7,故第 3 个标识下应改为“CMyClass* p=new CMyClass(7);”。二、程序填空题(总题数:1,分数:40.00)2.使用 VC6 打开 下的源程序文件 modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数 sum(int n)计算在 n 范围内,能被 7 和 11 整除的所有整数的和(包括 n 在内)。 注意:不能修改程序的其他部分,只能补充 sum ()函数。 #includ

13、e iostream.h double sum(int n) void main() cout sum(80) endl; cout sum(200) endl; cout sum(300) endl; return; (分数:40.00)_正确答案:()解析:int result=0; for(int i=11; i=n;i+) if(i%7=0) return result; 答案考生文件夹 解析 函数 sum(int n)的功能是实现计算在 n 范围内,能被 7 和 11 整除的所有整数的和(包括 n 在内),可采用循环的方式从 11 开始到 n 逐个寻找能被 7 和 11 整除的数,如

14、果能同时被 7 和11 整除则加到累加变量中。 (1)利用循环查找能所有小于等于 n 的能被 7 和 11 整除的数,循环变量 i 从最小值 11 开始,到 n 结束,即 11=i=n。 (2)在函数中定义累加变量 result 用来保存求和结果,在循环体内,逐个判断每个 i 是否能被 7 和 11 整除,即(i%7=0)和(i%11=0)都满足的话,说明 i 能同时被 7 和 11 整除,将该值加到 result 上,循环结束返回 result。三、程序设计题(总题数:1,分数:30.00)3.使用 VC6 打开 下的源程序文件 modi3.cpp,要求编写一个 CMyShape 类,含有求

15、面积求周长等纯虚函数。然后编写一个 CMyRectangle 类和 CMyCircle 类继承 CMyShape,并实现求面积、求周长的两个函数。在main()函数中测试得到下面的结果: 在 CMyShape 类构函数造内 在 CMyCircle 类构造函数内 在 CMyShape 类构造函数内 在 CMyRectangle 类构造函数内 myCircle:Area=314.159 Girth=62.8319 myRectangle:Area=900 Girth=120 具体要求如下: (1)定义求面积纯虚函数,请在注释/*1*之处添加适当的语句。 (2)定义求周长纯虚函数,请在注释/*2*之

16、处添加适当的语句。 (3)请在注释/*3*和/*4*之处添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 #include iostream.h #include math.h #define PI 3.1415926 class CMyPoint public: int x, y; CMyPoint (int tx, int ty): x(tx),y(ty) ; class CMyShape public: CMyShape()cout “在 CMyShape 类构造函数内“ endl; /*1* /*2* protected: ; class CMyCircl

17、e: public CmyShape public: CMyCircle(CMyPoint i,double j):CMyShape(),arcCenter(i), radius(j) cout “在 CMyCircle 类构造函数内“ endl; double GetArea() return PI*radius*radius; double GetGirth() return 2*PI*radius; private: CMyPoint arcCenter; double radius; ; class CMyRectangle: public CmyShape public: CMyRe

18、ctangle(CMyPoint lt,CMyPoint rb):leftTop(lt), rightBottom(rb),CMyShape() cout “在 CMyRectangle 类构造函数内“ endl; Double GetArea() int width=abs (rightBottom.x-leftTop.x); int height=abs (rightBottom.y-leftTop.y); Return width*height; double GetGirth() int width=abs (rightBottom.x-leftTop.x); int height=a

19、bs (rightBottom.y-leftTop.y); return 2*(width+height); private: CMyPoint leftTop, rightBottom; ; void main() CMyShape *myShape=NULL; CMyCircle *myCircle=new CMyCircle(CMyPoint(5,5),10); CMyRectangle *myRectangle=new CMyRectangle(CMyPoint(0,0), CMyPoint(30,30); /*3* cout “myCircle: “ “Area=“ myShape-

20、GetArea () “/t“ “Girth=“ myShape- GetGirth() endl; /*4* cout “myRect angle: “ “Are a=“ myShape-GetArea () “/t“ “Girth=“ myShape- GetGirth() endl; (分数:30.00)_正确答案:()解析:(1)添加语句:virtual double GetArea()=0; (2)添加语句:virtual double GetGirth()=0; (3)添加语句:mvShape=myCircle; (4)添加语句:mvShape=myRectangle; 答案考生文

21、件夹 解析 类 CMyPoint 含有成员变量 x 和 y,抽象基类 CMyShape 含有两个虚函数 GetArea()和GetGirth(),分别用来求图形的面积和周长。类 CMyRectangle 和类 CMyCircle 派生于类 CMyShape,并都实现了抽象父类的纯虚函数,通过这两个函数可分别求得矩形的面积和周长以及圆的面积和周长。 (1)第 1 个标识下声明纯虚函数,纯虚函数的定义格式为:virtual函数类型函数名(参数表)=0,由程序后面的函数体实现可知求面积纯虚函数的函数名为 GetArea(),因此第 1 个标识下应添加“virtual double GetArea()

22、=0;”。 (2)声明格式和(1)相同,由求周长的函数具体实现可知该函数的名为 GetGirth(),因此第 2 个标识下应添加“virtual double GetGirth()=0;” (3)主函数中定义了 CMyShape 类对象指针 myShape,CMyCircle 类对象指针 myCircle, CMyRectangle 类对象指针 myRectangle,由类继承关系知,在调用子类的构造函数的时候,编译器会自动调用父类的默认构造函数来初始化父类,因此在实例化类 CMyRectangle 和类 CMyCircle 的实例的时候,抽象父类 CMyShape的默认构造函数会先被调用,因

23、此在屏幕上先输出“在 CMyShape 类构函数造内”,然后输出“在CMyCircle 类构造函数内”,实例化 CMyRectangle 对象的过程也是如此。同时由纯虚函数的性质知,抽象基类不需要实现纯虚函数,它的实现是在子类中完成的,但是通过用子类实例隐式转换成基类实例,可以使得基类实例具有子类实例的特性,因此可以访问子类的成员函数,myShape 是基类指针,其虚函数GetArea()和 GetGirth()没有实现,要求程序输出面积值和周长值,只可能是 myShape 指向子类对象,调用子类的函数 GetArea()和 GetGirth()打印输出,显然第 3 个标识下是 myShape 指向子类 CMyCircle 对象,即 myShape=myCircle;。 (4)同理第 4 个标识下 myShape 指向子类 CMyRectangle 对象,即第 4 个标识下应添加“myShape=myRectangle;”。

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

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

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