C++复习题 3.docx
- 文档编号:28847984
- 上传时间:2023-07-20
- 格式:DOCX
- 页数:19
- 大小:17.88KB
C++复习题 3.docx
《C++复习题 3.docx》由会员分享,可在线阅读,更多相关《C++复习题 3.docx(19页珍藏版)》请在冰豆网上搜索。
C++复习题3
一)选择题
1对定义重载函数的要求中,错误的是D
A要求参数的个数不同
B要求参数中至少有一个不同
C要求参数个数相同时,参数类型不同
D要求函数的返回值不同
3以下那个函数函数表示虚函数c
Avirtualvoidvf(int)
Bvirtualvoidvf(int)=0
Cvirtualvoidvf()=0
Dvirtualvoidvf(int){}
4拷贝构造函数的形参是C
A某个对象名
B某个对象的成员名
C某个对象的引用名
D某个对象的指针名
5有关虚函数和抽象类的描述中,B是错的
A纯虚函数是一种特殊的虚函数,它没有具体的定义
B一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类
C抽象类是指具有纯虚函数的类
D抽象类只能作为基类来使用,其纯虚函数
6已定义的floatx,则以下对指针变量p进行定义且赋初值的语句中正确的是D
Afloat*p=1024;
Bint*p=x;
Cfloatp=&x;
Dfloat*p=&x;
7假定要对类型AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句正确的是B
AABoperator+(AB&a,AB&b)
BABoperator+(AB&a)
Coperator(AB&a)
DAB&operator+()
8从下对范本的说明,正确的是C
Atemplate
Btemplate
Ctemplate
Dtemplate
9派生类对象对它的基类成员中(公有继承的公有成员)是可以放回的
A公有继承的保护成员
B公有继承的私有成员
C公有继承的公有成员
D私有继承的私有成员
10派生类的构造函数的成员初始化列表中不能包含
A基类的构造函数
B基类的子对象的初始化
C派生类的子对象的初始化
D派生类中一般数据成员的初始化
11假定AB为一个类,则执行“ABa(4),b[3],*p”语句时,自动调用该类构造函数的次数为4
(二)填空为题
1编译时多态性使用通过(重载)获得。
2对一个类的数据成员初始化语句通过构造函数中的(赋值语句),也可以通过(初始化列表)
实现。
3对象的三大基本特征:
多态性,继承性和(封装性)。
4用(virtual)声明函数为虚函数。
5一个const对象只能访问(常)成员函数。
6字符串“hello,world”在内存中释放时,占用(13)个字节。
7语句int(*p)[n]的含义是定义了一个(数组指针)。
8假定一个类AB执行语句ABX,则将自动调用该类的(默认构造函数)。
9c++提供了一个测试某种数据所占内存的运算符(sizeof)。
10所有函数模板定义都是以关键词template用以关键的(类型参数表)。
(三)读程序题
1-----------------------------------------
#include
voidmain()
{intk=1,j=2,n=3;
ints=(j+=k,n+=j);
cout<<"s="<
cout<<(s*=n+=j)< } 答案: s=6j=3 54 2--------------------------------------- #include longf(intx) { inty=2; staticintz=1; x+=y++,z++; returnx; } voidmain() {intx=2,k; for(inti=0;i<2;i++) k=f(x); cout<<"k="< } 答案: k=4 3---------------------------------------- #include classtest { private: intnum; public: test(); intTEST() {returnnum;} ~test(); }; test: : test() { num=0; cout<<"testconstructor"< } test: : ~test() { cout<<"testdestructor"< } voidmain() { testx[3]; } 答案: testconstructor testconstructor testconstructor testdestructor testdestructor testdestructor 4----------------------------------------- #include classbase { public: base() {cout<<"构造函数"< base(inti) {cout<<"构造函数(int)"< ~base() {cout<<"析构函数"< private: inti; }; classderived: publicbase { public: derived() {cout<<"derived构造函数"< } derived(intm): base(m) {cout<<"derived构造函数(int)"< } ~derived() {cout<<"derived析构函数"< private: intk; }; voidmain() { derivedobj1; derivedobj2(10); } 构造函数 derived构造函数 构造函数(int) derived构造函数(int) derived析构函数 析构函数 derived析构函数 析构函数 5------------------------------------------------- #include usingnamespacestd; classcon { charid; public: chargetid()const{returnid;} con(): id('A'){cout<<1;} con(charid): id(id){cout<<3;} con(con&c): id(c.getid()){cout<<5;} }; voidshow(conc) {cout< voidmain() { conc1; show(c1); conc2('B'); show(c2); } 答案: 15A35B 6--------------------------------------------- #include classTest { staticintval; inta; public: staticintfunc() {returnval++;} voidsfunc(Test&r) {r.a=25;cout< } }; intTest: : val=20; voidmain() { cout< : func()<<""; Testa; cout< a.sfunc(a); } 答案: 202125 7------------------------------------------- #include voidmain() { intn1,n2; n2=1298; while(n2! =0) { n1=n2%10; n2=n2/10; cout< } } 答案: 8921 8------------------------------------- #include classsample { charc1,c2; public: sample(chara) {c2=(c1=a)-32;} voiddisp() { cout< } }; voidmain() { samplea('a'),b('b'); a.disp(); b.disp(); } 答案: a转换为A b转换为B 9------------------------------------- #include intcount=0; classpoint { intx,y; public: point() {x=1;y=1;count++;} ~point() { count--; } friendvoiddisp(); }; voiddisp() { cout<<"Thereare"< } voidmain() { pointa; disp(); {pointb[5];disp();} disp(); } 答案: Thereare1points Thereare6points Thereare1points (四)写程序题 1设计一个类string要求string类包括私有成员函数,char*str,在构造函数中初始化这个数据。 写出它的两个构造函数(有参和拷贝),析构函数,并能显示的声明和是调用构造函数(使用数组作为数据成员不给分) #include usingnamespacestd; classString { private: char*str; intlen; public: voidshowstr() { cout<<"String: "< "< } String() { len=0; str=NULL; } String(char*p) { len=strlen(p); if(len! =0) {str=newchar[len+1]; strcpy(str,p); } cout<<"构造函数"< } ~String() { if(str! =NULL) {delete[]str; str=NULL; len=0;} cout<<"析构函数"< } String(constString&r) { len=r.len; if(len! =0) { str=newchar[len+1]; strcpy(str,r.str); } cout<<"拷贝构造函数"< } }; voidmain() { Strings1("nihao"); s1.showstr(); Strings2=s1; s2.showstr(); } 2编写一个求两个正整数m和n最大公约数的程序并测试程序。 #include voidmain() { intm,n,t,r; cin>>m>>n; while(n! =0) {r=m%n; m=n; n=r; } cout<<"最大公约数是: "< } 3将fibnacial序列1,1,2,3,5,……的前20项存放在a[n]中,并显示出相应的元素 #include #include voidmain() { inti,a[20]={1,1}; for(i=2;i<20;i++) a[i]=a[i-1]+a[i-2]; for(i=0;i<20;i++) {if(i%5==0) cout< cout< } cout< } 4设计一个类person要求person类包括私有成员函数,char*Name,在构造函数中初始化这个数据。 写出它的两个构造函数(一般参数和对象的引用),析构函数,并能显示的声明和是调用构造函数(使用数组作为数据成员不给分) #include usingnamespacestd; classPerson { private: char*Name; intlen; public: voidshowName() { cout<<"Person: "< "< } Person() { len=0; Name=NULL; } Person(char*x) { len=strlen(x); if(len! =0) {Name=newchar[len+1]; strcpy(Name,x); } cout<<"构造函数"< } ~Person() { if(Name! =NULL) {delete[]Name; Name=NULL; len=0; } cout<<"析构函数"< } Person(Person&xm) { len=xm.len; if(len! =0) { Name=newchar[len+1]; strcpy(Name,xm.Name); } cout<<"拷贝构造函数"< } }; voidmain() { Persons1("Liming"); s1.showName(); Persons2=s1; s2.showName(); } 5输出100~200之间能被3整除,但不能被7整除的数及其个数。 #include #include voidmain() { inti,j,count;inta[100]; count=0; for(i=100,j=0;i<=200&&j<100;i++) if(i%3==0&&i%7! =0) { a[j]=i; j++; count++; } for(i=0;i { if(i%6==0)cout< cout< } cout< cout<<”个数为: ”< } 6完成下列函数,实现将a[n]中的元素按逆序重新存放。 1---------------------------- #include #include #defineN5 voidswap(inta[],intn) { inti,t; for(i=0;i {t=a[i];a[i]=a[n-i-1];a[n-i-1]=t;} for(i=0;i cout<<""< } voidmain() { inta[N],i; for(i=0;i cin>>a[i]; swap(a,N); cout< } 2-------------------------- #include #include #definen5 voidmain() { inta[n],i,t; for(i=0;i cin>>a[i]; for(i=0;i {t=a[i];a[i]=a[n-i-1];a[n-i-1]=t;} for(i=0;i cout<<""< cout< }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+复习题 C+ 复习题