收藏 分享(赏)

第六到八章 作业(带答案).doc

上传人:scg750829 文档编号:9259829 上传时间:2019-07-31 格式:DOC 页数:19 大小:139.50KB
下载 相关 举报
第六到八章 作业(带答案).doc_第1页
第1页 / 共19页
第六到八章 作业(带答案).doc_第2页
第2页 / 共19页
第六到八章 作业(带答案).doc_第3页
第3页 / 共19页
第六到八章 作业(带答案).doc_第4页
第4页 / 共19页
第六到八章 作业(带答案).doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

1、一、单选1-5DDBDC 6-10 DABCC11、C 12 、D一单项选择题1.下列有关派生类的叙述中,错误的是( D )A、一个派生类至少有一个基类; B、派生类的默认继承方式是 private;C.、一个派生类可以另一个派生类的基类;D、派生类只继承了基类的公有成员和保护成员;2.下列有关继承和派生类的叙述中,正确的是:( D )A、作为虚基类的类不能被实例化;B、派生类不能访问基类的保护成员;C、虚函数必须在派生类中重新实现;D、派生类应当向基类的构造函数传递参数;3.有如下类定义class XXint xdata;public:XX(int n=0):xdata(n);class Y

2、Y:public XXint ydata;public:YY(int m=0,int n=0):XX(m),ydata(n);YY 类的对象包含的数据成员的个数为( B )A、1 B、2 C、 3 D、44如果派生类以 protected 方式继承基类,则原基类的 protected 成员和 public 成员在派生类中的访问权限分别是( D )A、private 和 public B、private 和 protectedC、protected 和 public D、protected 和 protected5.有下列类的定义:class Baseint k;public:void set(

3、int n)k=n;int get() const return k;class Derived : protected Baseprotected:int j;public:void set (int m,int n)Base:set(m);j=n;int get() const return Base:get()+j;则类 Derived 中保护的数据成员和成员函数的个数是( C )A、1 B、2 C、 3 D、46.下列关于派生类构造函数和析构函数的叙述中,错误的是( D )A、派生类的构造函数会隐含调用基类的构造函数;B、如果基类中没有默认的构造函数,那么派生类必须定义构造函数;C、在

4、建立派生类对象时,先调用基类的构造函数,在调用派生类的构造函数;D、在销毁派生类的对象时,先调用基类的析构函数,在调用派生类的析构函数;7.设 class Y:public X,即类 Y 是类 X 的派生类,则创建一个 Y 类的对象和删除一个 Y 类对象时,调用构造函数和析构函数的次序分别为 ( A )A. X、Y;Y、X B. X、Y;X 、YC. Y、X;X、Y D. Y、X;Y 、X8.将类 A 说明是类 B 的虚基类,下列叙述正确的是 ( B )A. class virtual B:public A B. class B:virtual public AC. virtual class

5、 B:public A D. class B:public A virtual9.下列关于虚基类的叙述中,错误的是( C )A、使用虚基类可以消除由多继承产生的二义性;B、构造派生类的对象时,虚基类的构造函数只被调用一次;C、声明“class B:virtual public A”说明类 B 为虚基类;D、建立派生类对象时,首先调用虚基类的构造函数;10下列关于虚基类的叙述中,错误的是( C )A、设置虚基类的目的是消除二义性;B、虚基类子对象的初始化由最派生类完成;C、虚基类子对象的初始化次数与虚基类下面的派生类个数有关;D、带有虚基类的多层派生类构造函数的成员初始化列表中都要列出对虚基类构

6、造函数的调用;11有如下程序:/#include “stdafx.h“#include using namespace std;class Baseint x;public:Base(int n=0):x(n)coutusing namespace std;class Baseprivate:void fun1()constcoutusing namespace std;class Foopublic:Foo(int x)(*this).x=x;coutusing namespace std;class Apublic:A()coutusing namespace std;class myCl

7、asspublic:myClass(int i=0):n(i)coutusing namespace std;class Bpublic:B()coutusing namespace std;class Testprivate:static int val;int a;public:static int func();static void sfunc (Test ;int Test:val =20;int Test:func()val+=val;return val;void Test:sfunc (Test coutusing namespace std;class ClassOnepub

8、lic:ClassOne()i=0;coutusing namespace std;class Pointpublic:Point(int xx=0,int yy=0)X=xx;Y=yy;countP+;Point(Point Y=p.Y;countP+;int GetX()return X;int GetY()return Y;static void GetC()coutusing namespace std;class MyClasspublic:MyClass(int x=0):i(x)coutSetValue(pi-GetVal()*2);for(i=0;iusing namespac

9、e std;class PARENTpublic:PARENT()coutPARENTusing namespace std;class Apublic:A()coutusing namespace std;class Bpublic:B()coutusing namespace std;class Apublic:A()a=0;A(int i)a=i;void print()coutusing namespace std;class Datapublic:Data(int i):x(i)coutusing namespace std;class Basepublic:Base(char i)

10、coutusing namespace std;class Apublic:int n;class B:public A; /class B:virtual public A;class C:public A; /class C:virtual public A;class D:public B,public Cpublic:int getn()return B:n;int main()D d;d.B:n=10;d.C:n=20;coutusing namespace std;/定义状态变量,用不同整数表示不同状态const int CLOSED=1;const int OPENING=2;c

11、onst int OPEN=3;const int CLOSING=4;const int STAYOPEN=5;class Doorpublic:Door():state(CLOSED);void getState() /根据当前状态输出相应的字符串switch(state)case OPENING: coutstate=state;int main()Door aDoor;aDoor.getState(); /CLOSEDaDoor.click(); aDoor.getState(); /OPENINGaDplete(); aDoor.getState(); /OPENaDoor.clic

12、k(); aDoor.getState(); /STAYOPENaDoor.click(); aDoor.getState(); /CLOSINGreturn 0;2.下列程序中用静态数据成员 countP 记录 point 对象个数。程序运行结果为:Object count=0Object count=5Object count=2#include using namespace std;class Pointpublic:Point(int xx=0,int yy=0):X(xx),Y(yy)_countP+;_;/(1)Point(_Point _; /(3)_Point()_ coun

13、tP-; /(4)int GetX()return X;int GetY()return Y;static void GetC() coutusing namespace std;class Personpublic:Person(char* s)strcpy(name ,s);void display()cout#include using namespace std;class Personpublic:Person():name(“ “),gender(f),age (0)Person(string theName,char theGender,int theAge):name(theN

14、ame),gender(theGender),age(theAge)void who() const;protected:string name;char gender; /morfint age;class Employee:public Personpublic:Employee();Employee(string theName,char theGender,int theAge,long persNum):_Person(theName,theGender,theAge)_,personnelNumber(persNum) /(1)void who() const;protected:

15、long personnelNumber;class Executive:public Employeepublic:Executive();Executive(string theName,char theGender,int theAge,long persNum): _Employee(thName,theGender,theAge,thepersNum)_ /(2)void who() const;void Person:who () constcout“This is“name“ who is “age“ years old.“endl;void Employee:who ()con

16、st coutname“is a “(_gender=f?”female”:”male”_)“employee aged “age“.“endl;/(3)void Executive:who () constcoutname“is a“(_ gender=f?”female”:”male”_)“executive.“endl; /(4)int main()Employee employees2;employees0=Employee(“Guo jing “,m,21,34567);employees1=Employee(“Huang Rong“,f,19,34568);for(int i=0;isizeof employees/sizeof(Employee);i+)employeesi._Person:who()_; /(5)employeesi._who()_; /(6)Executive executives;executives=Executive(“Hong Qigong“,m,56,35591);executives._who()_; /(7)executives._Employee:who()_; /(8)return 0;

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

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报