收藏 分销(赏)

面向对象程序设计作业.doc

上传人:s4****5z 文档编号:8796547 上传时间:2025-03-02 格式:DOC 页数:9 大小:41.50KB
下载 相关 举报
面向对象程序设计作业.doc_第1页
第1页 / 共9页
面向对象程序设计作业.doc_第2页
第2页 / 共9页
点击查看更多>>
资源描述
郑州大学现代远程教育《 面向对象程序设计》课程考核要求 说明:本课程考核形式为提交作业,完成后请保存为WORD 2003版本格式的文档,登陆学习平台提交,并检查和确认提交成功(能够下载,并且内容无误即为提交成功)。 一. 作业要求 1.请独立自主完成作业内容。 二. 作业内容 一)、简答题:(每题 5 分,共30 分) 1.什么是抽象类?它有什么特点? 答: 抽象类与接口紧密相关,它们不能示例化,并且常常部分实现或根本不实现。 特点: 1、抽象类不能直接实例化 2、允许(但不要求)抽象类包含抽象成员。 3、抽象类不能被密封 2. C++中使用多态的前提条件是什么? 答:动态多态的前提条件: 1.通过调用虚函数实现多态 2.通过基类的指针或引用调用虚函数 3. 什么是类?类与结构有和不同? 答:类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存 不同:类有构造和析构函数,类可以继承和被继承 结构没有构造函数,但可以添加。结构没有析构函数,结构不可以继承自另一个结构或被继承,但和类一样可以继承自接口。 4. 面向对象的特征有哪些? 答:封装,继承,多态 5. 简述this指针的含义是什么? 答:this指针是一个隐含于每一个成员函数中的特殊指针。它是一个指向正在被该成员函数操作的对象,也就是要操作该成员函数的对象。 6. 友元关系具有什么特点? 答:友元函数的特点是能够访问类中的私有成员的非成员函数。友元函数从语法上看,它与普通函数一样,即在定义上和调用上与普通函数一样。 二)、分析下面的代码,回答问题(每题 10 分,共 40 分) 1. 分析下面的代码,指出其中的错误,说明理由并改正 class C { public: int fn1( int x ) { dm = x ; } private: int dm; }; void main ( ) { C c; c.fn1(16 ); cout << c.dm << ‘\n’; } 答://dm为私有变量,是不能通过对象直接访问的,正确的做法是添加一个public类型的方法,如public void Show(){cout<<dm<<endl;}然后利用对象c调用Show(), 2.下面代码的执行结果是什么?为什么? class A{ int n; public: A() {}; A(int i){n=i;} ~A() { cout<<"n="<<n<<endl;} }; class B { A a; int m; public: B(int i,int j):a(i){m=j;} ~B(){cout<<"m="<<m<<endl;} }; void main() { B b(1,2); } 答:执行结果: n=1 m=2 原因:b对象里包含一个A类的对象,b对象在析构时先调用A的析构函数,再调用B自身的析构函数 3.下面代码的执行结果是什么?为什么? class Sample{ int n; public: Sample(){}; Sample(int i){n=i;}; Sample& operator=(Sample); void disp(){cout<<"n="<<n<<endl;} }; Sample & Sample::operator=(Sample s) { Sample::n=s.n; return *this; } void main() {Sample s1(10),s2; s2=s1; s2.disp(); } 答:结果: n=10 Sample重载了赋值操作符,可以用Sample的一个对象去初使化另一个对象,s2对象被已经初使化的s1所初使化 4. 下面代码的执行结果是什么?为什么? class A{ public: A(char *s) {cout<<s<<endl;} ~A(){}; }; class B:virtual public A { public: B(char *s1,char*s2):A(s1) { cout<<s2<<endl; } }; class C:virtual public A { public: C(char* s1,char* s2) : A(s1) { cout<<s2<<endl; } }; class D: public B, public C { public: D(char *s1,char *s2,char *s3,char *s4):B(s1,s2),C(s1,s2),A(s1) { cout<<s4<<endl; } }; 创建对象的语句: D d(“class A”,”class B”,”class C”,”class D”);执行结果是什么?为什么? 答:执行结果: class A class B class C class D 三)、根据要求编写完整程序:(每题15分,共 30 分) 1.设计复数类的加法运算操作,使之能够执行下列运算:( 15 分) Complex x(3,6), y(4,5), z(0,0); z = x + y; z=2.4 + x; z=y + 3.5 答:class complex{ public: complex(int r=0,int i=0);//构造 complex operator+(const complex& other); complex Complex::operator+(const int &a); void complex::input() protected: int real,image; }; void complex::input() { int real,image; cout << "please enter a complex: " << endl; cin>>r>>m; real = r; image = m; } complex::complex(int r,int i) { real=r; image=i; return; } complex complex::operator+(const complex& other) { complex temp; temp.real=real+other.real; temp.image=image+other.image; return temp; } complex complex::operator+(const int &a) { complex temp; temp.real= real +a; temp.image = image; return temp; } int main(int argc,char* argv[]) { complex a,b,c; a.input(); b.input(); c = a +b; //输出 c = 5 +a; //输出 c = b +5; //输出 } } 2. 设计High类,其数据成员为高h,定义虚函数disp()。由High派生出长方体类Cuboid与圆柱体类Cylinder。在主函数中用基类指针变量p调用虚函数disp()显示长方体与圆柱体的体积。(15分) 答:class High{ public:   unsigned int h;      virtual void disp(){}; }; class Cuboid:public High{ Public:   unsigned int length;   unsigned int width;   virtual void disp(){cout<<length*width*h<<endl;}; }; Class Cylinder:public High{ Public:   unsigned int r;//半径   virtual void disp(){cout<<3.14159*r*r*h<<endl;}; }; void main() {   High *p;   Cuboid objA;   Cylinder objB;   objA.h=20;   objA.length=3;   objA.width=4;   p=&objA;   p->disp();      objB.r=5;   objB.h=10;   p=&objB;   p->disp(); }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服