c++复习文档格式.docx
《c++复习文档格式.docx》由会员分享,可在线阅读,更多相关《c++复习文档格式.docx(28页珍藏版)》请在冰豆网上搜索。
![c++复习文档格式.docx](https://file1.bdocx.com/fileroot1/2023-1/4/722ea0e5-9653-427b-a67a-aac066d37ff3/722ea0e5-9653-427b-a67a-aac066d37ff31.gif)
endl;
return0;
abccde
3、有类定义如下:
#include"
iostream.h"
classType{
Type(inti=0);
Typeoperator-(int);
friendTypeoperator+(Type,Type);
private:
intval;
若有对象定义Typecl;
则下列语句序列中,错误的是c
A)Type(3)+c1;
B)c1+Type(3);
C)3-c1;
D)c1-3;
4、#include<
classBook{
Book(char*t="
"
){strcpy(title,t)};
----virtual_char*Category()const=0;
-----
chartitle[40];
classNovel:
publicBook{
Novel(char*t="
):
Book(t){}
char*Category()const
{return"
文学"
;
Book*pb;
pb=newNovel();
cout<
pb->
Category();
5.有如下程序:
#include<
usingnamespacestd;
classMedia{
public:
voidName();
voidShow();
protected:
intpage;
};
classBook:
privateMedia{
voidPrint()
{cout<
page<
voidShow()
{Media:
:
Show();
}//①
intmain(){
BookBible;
Bible.Name();
//②
Bible.Print();
//③
Bible.Show();
//④
}编译不正确的是:
(2)
6、有如下程序段
inti=0,j=1;
int&
r=i;
//①
r=j;
int*p=&
i;
*p=&
r;
//④//将*去掉或者将&
去掉
其中编译正确的语句是123
7、下面程序的语句1-4有误,找出不正确的语句并修改
有如下程序:
MyClass(intx):
val(x){}
voidSet(intx){val=x;
}
voidPrint()const
{cout<
val="
val<
'
\t'
;
Intmain(){
constMyClassobj1(10);
---
MyClassobj2(20);
obj1.Print();
//语句1
obj2.Print();
//语句2
obj1.Set(20);
//语句3//将此句删除obj1为常量
obj2.Set(30);
//语句4
8、有如下程序:
#include<
classToy{
Toy(char*_n)
{strcpy(name,_n);
count++;
~Toy(){count--;
char*GetName(){returnname;
staticintgetCount(){returncount;
}静态成员属于类
charname[10];
staticintcount;
intToy:
count=0;
Toyt1("
Snoopy"
),t2("
Mickey"
),t3("
Barbie"
t1.getCount()<
//====toy:
:
getcount();
}//输出3
9、如果不使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问从基类继承的成员,有如下程序,没有使用多态机制。
classBase{
inta,b;
Base(intx,inty){a=x;
b=y;
voidshow(){cout<
a<
'
b<
classDerived:
publicBase{
intc,d;
Derived(intx,inty,intz,intm):
Base(x,y){c=z;
d=m;
c<
d<
BaseB1(50,50),*pb;
DerivedD1(10,20,30,40);
pb=&
D1;
show();
//输出1020
10.若有如下类声明
classMyClass{
MyClass(){cout<
执行下列语句
MyClassa,b[2],*p[2];
以后,程序的输出结果111
11、下列语句中不正确的是(AC)。
A)constinta;
B)constinta=10;
C)constint*point=10;
D)constint*point=newint(10);
12、在C++中既可以用于文件输入又可以用于文件输出的流类是A
A)fstreamB)ifstreamC)ofstreamD)iostream
13、有如下程序:
classA{
A(){cout<
A'
~A(){cout<
C'
classB:
publicA{
B(){cout<
G'
~B(){cout<
T'
Bobj;
}运行这个程序的输出结果是AGTC
14、有如下程序:
classBoat;
classCar{
Car(inti):
weight(i){}
friendintTotal(constCar&
c,constBoat&
b);
//①
private:
intweight;
classBoat{
Boat(inti):
c,constBoat&
intTotal(constCar&
b){//②
returnc.weight+b.weight;
Carc(10);
Boatb(8);
Thetotalweightis"
Total(c,b)<
15.若需要为XV类重载乘法运算符,运算结果为XV类型,在将其声明为类的成员函数时,下列原型声明正确的是D
A)XVoperator*(XV,XV);
B)XV*(XV);
C)operator*(XV);
D)XVoperator*(XV);
16.有如下程序:
usingnamespacestd;
classInstrument{
public:
virtualvoidDisplay()=0;
};
classPiano:
publicInstrument{
voidDisplay(){/*函数体程序略*/}
intmain(){
Instruments;
Instrument*p=0;
//...;
return0;
}下列叙述中正确的是B
A)语句“Insturment*p=0;
”编译时出错
B)语句“Instruments;
C)类Piano中的Display函数不是虚函数
D)类Instrumen类是一个虚基类
17、已知类IMS中两个成员函数的声明为"
voidlisten()const;
与"
voidspeak();
另有两个对象的定义为"
IMSobj1;
constIMSobj2;
则下列语句中,产生编译错误的是B
A)obj1.listen();
B)obj1.speak();
C)obj2.listen();
D)obj2.speak();
17、若在表达式y/x中,“/”是作为成员函数重载的运算符,则该表达式还可以表示为C
A)x.Operator/(y) B)operator/(x,y)
C)y.operator/(x) D)operator/(y,x)
18、有函数模板声明和一些变量定义如下:
template<
classT1,classT2,classT3>
T1sum(T2,T3);
doubled1,d2;
则下列调用中,错误的是D
A)sum<
double,double,double>(d1,d2);
B)sum<
double;
double>
(d1,d2);
C)sum<
D)sum(d1,d2);
//没有参数指明T1的类型
19、有如下类定义
classA{
char*a;
A():
a(0){}
A(char*aa){//把aa所指字符串拷贝到a所指向的存储空间
a=_newchar[strlen(aa)+1];
__________________;
strcpy(a,aa);
~A(){delete[]a;
横线处应填写的表达式是
20、有如下程序:
classtest{
inta;
test(){cout<
constructor"
test(inta){cout<
test(consttest&
_test){
a=_test.a;
copyconstructor"
~test(){cout<
destructor"
testA(3);
}运行时输出的结果是3destructor
21、下列运算符重载函数中,属于类Value的成员函数的是(ACD)。
A)Valueoperator+(Value);
B)Valueoperator-(Value,Value);
C)Valueoperator*(int);
D)Valueoperator/(Value);
22、下列函数模板的定义中,不合法的是CD
A)template<
typenameT>
Tabs(Tx){returnx<
0?
-x:
x;
B)template<
classT>
Tabs(Tx){returnx<
-x:
x;
C)templateT<
classT>
abs(Tx){returnx<
D)templateTabs(Tx){returmx<
23、有如下程序:
classpoint{
staticintnumber;
point(){number++;
~point(){number--;
intpoint:
number=0;
voidmain(){
point*ptr;
pointA,B;
{
point*ptr_point=newpoint[3];
ptr=ptr_point;
pointC;
point:
number<
delete[]ptr;
}运行时输出的结果是6
24、下列函数原型声明中,正确的是ABD
A)intfnction(intm,intn);
B)intfunction(int,int);
C)intfunction(intm=3,intn);
D)intfunction(int&
m,int&
n);
25、若有函数模板mySwap和一些变量定义如下:
template<
voidmySwap(TX,TY);
doubled1,d2;
inti1,i2;
下列对mySwap的调用中,正确的是ABD
A)mySwap(i1,i2)B)mySwap(d1,d2)
C)mySwap(i1,d1)D)mySwap<
int>
(i2,d2)
26、下列程序的输出是3,请填充程序中的空缺,使该行形成一个运算符重载函数的定义。
classMyNumber{
intn;
MyNumber(intk):
n(k){}
----operator----int()const{returnn;
MyNumbernum1(3);
int(num1);
27、有如下程序:
//没有错误
cmath>
usingstd:
cout;
classPoint{
frienddoubledistance(constPoint&
p);
//p距原点的距离
Point(intxx=0,intyy=0):
x(xx),Y(yy){}//①改为point(intxx,intyy):
x(XX),y(YY)
intx,Y;
doubledistance(constPoint&
p){//②
returnsqrt(p.x*p.x+p.Y*p.Y);
Pointp1(3,4);
distance(p1);
28、若已定义了类Vehicle,则下列派生类定义中,正确的是acd
A)classCar:
Vehicle{/*类体略*/},
B)classCar:
publicCar{/*类体略*/};
C)classCar:
publicVehicle{/*类体略*/};
D)classCar:
virtualpublicVehicle{/*类体略*/};
29、使用VC6打开工程test4_1,此工程包含一个源程序文件test4_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Constructor2
Constructor1
i=0
i=10
Destructor
源程序文件test4_1.cpp清单如下:
#include<iostream.h>
classCSample{
inti;
public;
CSample(){cout<<"
Constructorl"
<<endl;
i=0;
}
CSample(intval){cout<<"
Constructor2"
i=val;
~CSample(){cout<<"
Destructor"
voiddisp();
/**********found
(1)********/
voiddisp(){//voidCSample:
disp()声明错了
cout<<"
i=”<<i<<endl;
voidmain(){
CSample*a,b(10);
/**********found
(2)*********/指向不明CSamplec;
a=&
c;
a->
disp();
a->disp();
/**********found(3)*********/
b->disp();
普通对象b.disp();
30、请将下列模板类Data补充完整。
template<
classData
voidput(Tv)
{val=v;
_T__get()//返回数据成员val的值,返回类型不加转换
{returnval;
Tval;
31、根据程序中的注释将下列缺失部分补充完整。
intx,y;
//点的x和y坐标
x(xx),y(yy){}
classCircle{
Pointcenter;
//圆心位置
Intradius;
//半径
//利用cx和cy分别初始化圆心的x和y坐标
----Circle(intcx,intcy,intr):
center(cx,cy)-----,radius(r){}
voidarea(){cout<
3.14159*radius*radius<
32、下列程序的输出结果是__`___042_____________。
classTest{
Test(){cnt++;
~Test(){cnt--;
staticintCount(){returncnt;
staticintcnt;
intTest:
cnt=0;
cout<
Test:
Count()<
'
Testtl,t2;
Test*pT3=newTest;
Test*pT4=newTest;
Test:
deletepT4;
deletepT3;
Count()<
endl;
33、使用VC6打开工程test4_2。
此工程包含一个源程序文件test4_2.cpp,其对一个学校的教师和考生情况进行了描述。
由于教师和考生之间有相同的信息,如姓名、年龄等,所以可抽象出一个基类person,考生类student和教师类teacher作为person类的派生类。
请按要求完成下列操作,将程序中的类定义补充完整。
(1)定义基类person的私有数据成员name和age,分别用于表示姓名和年龄,name为字符指针,age为血型的数据。
请在注释“//**1**”之后添加适当的语句。
(2分)
(2)完成基类person的重载构造函数person(char*n,inta)的定义,把数据成员name,age分别初始化为参数n,a的值,请在注释“//**2**”之后添加适当的语句。
(2分)
(3)根据主函数main中对s1的声明,定义派生类student的构造函数。
在注释“//**3**”之后添加适当的语句。
(3分)
(4)完成派生类teacher成员函数voidsetinf(ehar*n,inta,char*d,char*1)的定义,其功能为记录教师的姓名、年龄、院,系与职称信息。
请在注释“//**4**”之后添加适当的语句。
输出结果如下:
刘雷(45)destructor蒋军(23)destructor
string.h>
classperson{
//*********************1*******************