面向对象上一届期末试卷

上传人:xuey****n398 文档编号:137270404 上传时间:2022-08-18 格式:DOC 页数:11 大小:59KB
收藏 版权申诉 举报 下载
面向对象上一届期末试卷_第1页
第1页 / 共11页
面向对象上一届期末试卷_第2页
第2页 / 共11页
面向对象上一届期末试卷_第3页
第3页 / 共11页
资源描述:

《面向对象上一届期末试卷》由会员分享,可在线阅读,更多相关《面向对象上一届期末试卷(11页珍藏版)》请在装配图网上搜索。

1、专业、班级: 学号: 姓名:密 封 线浙江财经学院 学年第一学期面向对象程序设计课程期末考试试卷( 卷)(共六大题)一、判断题(/,每题1分,共10分)1、程序测试是为了发现程序中的错误而执行程序的过程。 ( )2、白盒测试是把程序看成一个封装的系统,测试只在程序接口处进行。 ( )3、类的测试不仅与当前应用程序有关,还关系到部件复用的可靠性。 ( )4、程序的效率是最重要的,为了提高效率可以牺牲程序的可读性。 ( )5、程序调试就是要找出程序中所有的错误。 ( )6、编译程序后出现的警告信息可以忽略,对程序的运行结果没有影响。 ( )7、程序编译后报告的错误数量和程序中存在的错误数量是一致的

2、。 ( )8、提高程序调试效率的有效方法是先进行静态跟踪,然后进行运行跟踪。( )9、进行程序调试时,断点设得越多越好。 ( )10、在VC+调试工具可以设置观察(watch)窗口,但在窗口中不能改变变量的当前值。 ( )二、单项选择题 (每题1分,共10分)1关于类和对象不正确的说法是( )。 (a) 类是一种类型,它封装了数据和操作 (b) 对象是类的实例 (c) 一个类的对象只有一个 (d) 一个对象必属于某个类 2在类定义的外部,可以被访问的成员有( )。 (a) 所有类成员 (b) private 或 protected 的类成员 (c) public 的类成员 (d) public

3、 或 private 的类成员 3关于 this 指针的说法错误的是( )。 (a) this 指针可以不显式说明 (b) 当创建一个对象后,this 指针就指向该对象 (c) 成员函数拥有 this 指针 (d) 静态成员函数拥有 this 指针 4下面对构造函数的不正确描述是( )。 (a) 系统可以提供默认的构造函数 (b) 构造函数可以有参数,所以也可以有返回值 (c) 构造函数可以重载 (d) 构造函数可以设置默认参数 5下面对友员的错误描述是( )。 (a) 关键字 friend 用于声明友员 (b) 一个类中的成员函数可以是另一个类的友员 (c) 友员函数访问对象的成员不受访问特

4、性影响 (d) 友员函数通过 this 指针访问对象成员 6下列关于运算符重载的描述中,( )是正确的。(a) 可以改变参与运算的操作数个数 (b) 可以改变运算符原来的优先级(c) 可以改变运算符原来的结合性 (d) 不能改变原运算符的语义 7当一个派生类公有继承一个基类时,基类中的所有公有成员成为派生类的( )。(a) public 成员 (b) private 成员 (c) protected 成员 (d) 友元 8不论派生类以何种方式继承基类,都不能使用基类的( )。 (a) public 成员 (b) private 成员 (c) protected 成员 (d) public 成员

5、和 protected 成员9在创建派生类对象时,构造函数的执行顺序是( )。 (a) 对象成员构造函数、基类构造函数、派生类本身的构造函数 (b) 派生类本身的构造函数、基类构造函数、对象成员构造函数 (c) 基类构造函数、派生类本身的构造函数、对象成员构造函数(d) 基类构造函数、对象成员构造函数、派生类本身的构造函数10若有定义“int x=17;”,则语句“coutoctx;”的输出结果是()(a) 11 (b) 0x11 (c) 21 (d) 021三、程序选择填空 (每空2分,共20分)1以下是一个采用类结构的方式求 n!的程序,请填空完成程序。 #include class Fa

6、ctorial int n; int fact; public: Factorial(int); void Calculate( ); void Display( ); ; Factorial:Factorial(int val) n=val; ; void Factorial:Calculate( ) int i=n; while(i1) ; void Factorial:Display( ) coutn!=factendl; void main( ) int n; coutn; ; A.Calculate( );/计算n的阶乘 A.Display( ); /显示n的阶乘 2、下列程序的运行

7、结果为:cl=(5,4)c2=(2,10)(0,0)c3=c1+c2=(7,14)#include class complex private: double real; double imag; public: complex(double r=0.0,double i=0.0) ; friend complex operator + (complex c1,complex c2); void display(); ; complex operator + (complex c1,complex c2) return ; void complex:display() cout endl; v

8、oid main() complex c1(5,4),c2(2,10),c3,c4; coutcl=; c1.display(); coutc2=; c2.display(); c3.display(); c3=c1+c2; coutc3=c1+c2=; c3.display(); 3、#include #include class Point private:double X,Y;_Line; /将Line声明为友元类public:Point(double x=0, double y=0) X=x; Y=y;Point(Point &p) X=p.X; Y=p.Y;class Linepri

9、vate: Point p1,p2;public: Line(Point &xp1, Point &xp2): _ double GetLength(); /获取两点之间的距离;double Line:GetLength() double dx=p2.X-p1.X; double dy=p2.Y-p1.Y;return _ ;void main() Point p1,p2(3,4); Line L1(p1,p2); cout_imag=i (b) this-real=r;this-imag=i (c) real=0;imag=0 (d) this-real=0;imag=i(a)complex

10、(c1.real+c2.real,c1.imag+c2.imag)(b)complex(real+c2.real,imag+c2.imag)(c)complex(c1.real+real,c1.imag+imag)(d)complex(real+this-real,imag+this-imag)(a)(real,imag) (b) (real,imag)(c)(real,imag) (d)(real,imag)3、(a)friend class (b) class friend (c) friend (d) class (a)p1,p2 (b)p1(xp1),p2(xp2) (c)p1=0,p

11、2=0 (d)p1(0),p2(0) (a) dx*dx + dy*dy (b) dx*dxdy*dy(c)sqrt(dx*dx + dy*dy) (d) sqrt(dx*dx -dy*dy) (a)p1-p2 (b) p2-p1 (c) p2.GetLength()-p1.GetLength() (d)L1.GetLength()四、写出下列程序的运行结果(本大题共30分) 1、#include class Sample int x,y; public: Sample( ) x=y=1; Sample(int a,int b) x=a;y=b; Sample( ) if(x=y) coutx

12、=yendl; else coutx!=yendl; void disp( ) coutx=x , y=yendl; ; void main( ) Sample s1(2,3),s2; s2.disp( ); s1.disp( ); coutendl; 2、#include class point private: int x, y; public: point(int xx = 0, int yy = 0) x = xx; y = yy; cout 构造函数被调用 endl; point(point &p); point() cout 析构函数被调用 endl; int get_x() re

13、turn x; int get_y() return y; ; point:point(point &p) x = p.x; y = p.y; cout 拷贝构造函数被调用 endl; void main () point a(15, 22); point b( a ); cout b.get_x() b.get_y() endl; b =a; cout b.get_x() b.get_y() endl; 3、#include #include using namespace std;int main() char *pt=China; /pt指向字符串China coutsetw(10)pt

14、endl; coutsetfill(*)setw(10)ptendl; double pi=22.0/7.0; coutsetiosflags(ios:scientific)setprecision(8); coutpi=piendl; /输出pi值 coutpi=setprecision(4)setiosflags(ios:fixed)piendl;/改为小数形式输出 return 0;4、#include using namespace std; class Rectangle int width; int height; public: Rectangle(int w, int h) w

15、idth = w; height = h; cout Constructing width by height rectangle.n; Rectangle() cout Destructing width by height rectangle.n; int area() return width * height; ; int main() Rectangle Rect(3,4),*p; p = new Rectangle(10, 8); cout Area is area() n; delete p; return 0; 5、#includeclass BASE public: virt

16、ual void getxy( int i,int j = 0 ) x = i; y = j; virtual void fun( ) = 0 ; protected: int x , y; ; class A: public BASE public: void fun( ) coutx=xty=x*x=x*xendl; ; class B:public BASE public: void fun( ) cout x= x t y= y endl; cout y=x/y= x/y getxy( 10 ); pb - fun( ); pb = &obj2; pb - getxy( 100, 20

17、 ); pb - fun( ); 五、程序改错:下列程序中划线的语句有错,请改正(本大题共10分,每错2分)1、#include class Point int X,Y; public: Point( )X=0;Y=0; Point(int x=0,int y=0)X=x;Y=y; int getX() const return X;int getY() const return Y; void display( )coutX,Yendl; ;void main( )Point p1;coutp1.X;2、#includeclass Location public: void InitL(in

18、t xx,int yy); void Move(int xOff,int yOff); int GetX() return X; int GetY() return Y;private: int X,Y;void Location:InitL(int xx,int yy) X=xx;Y=yy;void Location:Move(int xOff,int yOff) X+=xOff; Y+=yOff;class Rectangle:private Location public: void InitR(int x,int y,int w,int h); void Move(int xOff,i

19、nt yOff); int GetX() coutX=; return Location: GetX(); int GetY() coutY=; return Location:GetY(); int GetH() coutH=; return H; int GetW() coutW=; return W; private: int W,H;void Rectangle:InitR(int x,int y,int w,int h) X=x;Y=y;W=w;H=h;void Rectangle:Move(int xOff,int yOff) Move(xOff,yOff);int main()

20、Rectangle rect(2,3,20,10);rect.InitR(2,3,20,10);rect.Move(3,2);coutrect.GetX(), rect.GetY(),rect.GetH(),rect.GetW()endl;return 0;六、程序设计(本大题20分)1、下列程序,声明一个抽象基类Shape,由它派生出两个派生类: Circle类(圆形)、 Triangle类(三角形), 用一个函数 printArea 分别输出以上两者的面积, 两个个图形的数据在定义对象时给定。程序已给出Shape类和Circle类的定义和主函数,请给出Triangle类和函数printAr

21、ea的定义和实现。(8分)#include using namespace std;/定义抽象基类Shapeclass Shapepublic: virtual double area() const =0; /纯虚函数;/定义Circle类class Circle:public Shapepublic:Circle(double r):radius(r) /构造函数 virtual double area() const return 3.14159*radius*radius; /定义虚函数 protected: double radius; /半径;int main() Circle c

22、ircle(12.6); /建立Circle类对象circle coutarea of circle =; printArea(circle); /输出circle的面积 Triangle triangle(4.5,8.4); /建立Triangle类对象 coutarea of triangle =; printArea(triangle); /输出triangle的面积 return 0;2、下面给出的是三角形类Triangle的定义:#include #include class Triangleint x,y,z; /三条边double area; / 面积public: Triangle(int i,int j,int k);/构造函数 void disparea( ); /显示三角形面积;(1) 请实现其中的成员函数。(4分)(2) 重载运算符“”,以实现求两个三角形对象的面积之和。即对于对象t1和t2,能够计算t1+t2,得到面积之和。(3分)(3) 想一想,如何设计重载运算符“”,使之能计算任意三个以上三角形对象的面积之和?(即对于对象t1、t2、t3,能够计算t1+t2+t3,得到三个三角形对象的面积之和。)(3分)(4)设计主函数,对你的设计进行测试。(2分)

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!