2010 面向对象复习

上传人:M****1 文档编号:513101645 上传时间:2022-12-11 格式:DOCX 页数:9 大小:16.48KB
返回 下载 相关 举报
2010 面向对象复习_第1页
第1页 / 共9页
2010 面向对象复习_第2页
第2页 / 共9页
2010 面向对象复习_第3页
第3页 / 共9页
2010 面向对象复习_第4页
第4页 / 共9页
2010 面向对象复习_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《2010 面向对象复习》由会员分享,可在线阅读,更多相关《2010 面向对象复习(9页珍藏版)》请在金锄头文库上搜索。

1、一. 填空。1. C+源程序文件的扩展名为()2. class有3中访问权限,分别为(),(),()3. 已知下面的类层次结构,每个类 都定义了自己的缺省构造函数: /说明:.处定义了每个类的构造函数 /方便起见一律省略class A.;class B:public A.;class C:public B.;class X.;class Y.;classZ:public X,public Y.;class MI:public C,public Z.; 下面的定义引起的构造函数调用顺序是什么? MI mi;在5. string str; cin str;输入abed def(换行)str 内容是

2、什么?6. 面向对象程序设计语言的三大特性是:封装性、继承性和()7. C+中关键字inline用于定义()函数。8. C+中关键字friend用于定义()函数。9. 联编是指一个计算机程序自身彼此关联的过程,根据联编所进行的阶段不同 可分为两种不同的联编方法:动态联编和静态联编, 前者是指联编工作出现在( ),后者是在编译时进行联编工作。10. 为了实现类间的代码重用,可以使用组合机制和( 继承 )机制, 组合机制表示的是 has-a 关系,而后者则表示的是( is-a )关系。二. 选择。2. 已知下列类层次结构,它们都定义了缺省的构造函数:class X;class A;class B:

3、public A;class C:private B;class D:public X,public C; 则下列哪些转换是不允许的? D* pd = new D;a) X* px=pd; b)B* pb=pd;c)A* pa=pd; d)C* pc=pd;3. 下面模板类的定义中,哪些是错误的? a)templateclass Container1;b)template class Container2;c)templateclass Container3;a, template class Container1;b. 其中 mT 重复了三. 阅读程序。1. 以下程序是否有错误?请改正。 改

4、正后输出是什么?#include using namespace std;class Apublic:A()cout A endl;class Bpublic:B(int x)cout B endl;class CA a;B b;public:C(int y);int main()C c(3);改成 public:C(int y):b(y)2. 下面程序中的输出是什么, 说明原因.#include using namespace std;class Apublic:void doit()coutA:doitendl;class B:public Apublic:void doit()coutB

5、:doitdoit();3. 下面程序的输出是什么, 说明原因.#include using namespace std;class Apublic:virtual void doit()coutA:doitendl; ;class B:public Apublic:void doit()coutB:doitendl;void f(A a)a.doit();int main()B b;f(b);4. 下面程序的输出是什么,说明原因.#include using namespace std;class Apublic:A()doit1();doit2();virtual void doit1()

6、=0;virtual void doit2()=0;class B:public Apublic:void doit1()coutB:doit1endl;void doit2()coutB:doit2endl;int main()B b;5. 以下函数试图完成什么功能?为什么有时候成功有时候失败? 请修改该函数,使得该函数的调用总是成功的。(假设头文件 都已写好)void func(vect& vec)vect:iterator it = vec.begin();vec.push_back(vec.front();reverse(it,vec.end();/ 试图将首元素插入到容器的尾部,然后

7、逆向重排列容器中的元素。 若 vec 的 capacity 不够大而重新申请一块空间,则原先 vec 的所有迭代器都失效 void func(vect& vec)vec.push_back(vec.front();vect:iterator it = vec.begin();reverse(it,vec.end();6. 以下程序存在什么问题?不修改 main 函数, 仅修改Test类使该程序正确运行且不会泄漏内存。 #include using namespace std;class Testint *p;public:Test(int n) p = new int(n); Test() d

8、elete p; ;int main()Test a(1);Test b=a;b=a;b=b;/class Testint *p;public:Test(int n) p = new int(n); Test() delete p; Test(Test& tmp)p=new int(*tmp.p);void operator=(Test & tmp)if(p)delete p;p=new int(*tmp.p);7. 下面程序的输出是什么? #include using namespace std;class A;class B:public A;void test()throw B();co

9、uttestendl;void f()trytest();coutfendl;catch(int)coutcatch(int)endl;catch(double) coutcatch(double)endl;int main()tryf();coutmain()endl;catch(A)coutcatch(A)endl;catch(B)coutcatch(B)endl;catch(.) coutcatch(.)endl;coutEndendl;/ catch(A)end四. 编程:1. 定义一个抽象类Animal,该类有一个纯虚函数void shout(); 用以表示动物“吼叫”的动作。从该类

10、派生出Cat,Dog,Cow 3个类,分别实现各自的shout函数,函数只要输出各种动物 的叫声就可以。编写一个函数test,输入参数为Animal*类型 的数组,在该函数中对每个指针依次调用shout()函数。编写一个main函数,向test函数传进一个指向各种不同动物 对象的数组来测试你的程序。2. 个文本文件data.txt中存储了 一些三国时期武将的资料, 格式为:每个武将资料占一行,该行内容依次为:武将名, 性别,武力,智力,体力。文件里存储了许多武将的资料( 具体数目未知)吕布男10030100貂蝉女698570诸葛亮男6010070黄月英女559565刘备男807875现在要求你

11、将所有的武将按照综合实力从高到底排序,并将排序 后的武将资料输出到文件output.txt 输出内容包括:武将名,性别,武力,智力,体力,综合实力。 综合实力计算公式如下:(1) 若为男武将,且武力,智力有一样95(若都大于,取较大者), 综合实力=該值(2) 若为女武将,且智力80,综合实力=(100+該值)/2(3) 其它情况,综合实力=武力,智力,体力的平均值。/#include#include#include#include #include #include #include #include using namespace std;class Generalpublic:strin

12、g name;string gender;int w;int z;int t;ostream& operator(ostream& out,General& g)return outg.name g.gender g.w g.z (istream& in, General& g) ing.nameg.genderg.wg.zg.t; return in;void Print( const vector& v, ostream& out ) copy(v.begin(), v.end(),ostream_iterator(out,n);bool sortByIntelligent(General& g1,General& g2)return g1.zg2.z;int main() vector

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号