C++程序设计试题Word格式.docx
- 文档编号:18971873
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:16
- 大小:16.70KB
C++程序设计试题Word格式.docx
《C++程序设计试题Word格式.docx》由会员分享,可在线阅读,更多相关《C++程序设计试题Word格式.docx(16页珍藏版)》请在冰豆网上搜索。
2、
double&
fn(constdouble&
pd)
staticdoublead=32;
ad+=pd;
cout<
”fnbeingcalled…thevalueis:
”<
pd<
returnad;
doublea=345.6;
double&
pa=fn(a);
pa<
a=55.5;
fn(a);
cout<
3、
sample();
sample(int);
~sample();
voiddisplay();
protected:
intx;
sample:
:
sample()
x=0;
”constructingnormally\n”;
sample(intm)
x=m;
”constructingwithanumber:
x<
~sample()
”destructing\n”;
voidsample:
display()
”displayanumber:
sampleobj1;
sampleobj2(20);
obj1.display();
obj2.display();
4、
classcomplex
complex(double,double);
complex(complex&
);
complex();
complexoperator+(complex&
complex&
operator=(complex&
doublereal;
doubleimage;
complex:
complex(doublex,doubley)
{real=x,image=y;
complex(complex&
x)
”Runingcopyconstructor\n”
real=x.real;
image=x.image;
complex()
{real=image=0.0;
complexcomplex:
operator+(complex&
returncomplex(real+x.real,image+x.image);
complex&
complex:
operator=(complex&
x)
”Runingassignoperator\n”;
real+=x.real;
image+=x.image;
return*this;
viodcomplex:
real<
”+”<
image<
”i”<
complexc1(2,5);
complexc2(c1);
complexc3;
c3=c1+c2;
c3.display();
5、
template<
classT>
sample(){};
sample(Tx){n=x;
sample<
T>
&
operator+(constsample<
voiddisp(){cout<
”n=”<
n<
Tn;
sample<
&
operator+(constsample<
s)
staticsample<
temp;
temp.n=n+s.n;
returntemp;
int>
s1(10),s2(20),s3;
s3=s1+s2;
s3.disp();
6、
classbase
intn;
base(inta)
”constructingbaseclasswithanumber=”<
a<
n=a;
~base()
{cout<
”destructingbaseclass”<
classderive:
publicbase
intm;
derive(inta,intb):
base(a)
cout<
”constructingderiveclasswithanumber=”<
b<
~derive()
”destructingderiveclass”<
derives(1,2);
7、
#include<
classvehicle
intwheels;
floatweight;
virtualvoidmessage()
{cout<
”vehiclemessage\n”;
classcar:
publicvehicle
intpassengers;
voidmessage(){
”carmessage\n”;
classtruck:
intgoods;
”truckmessage\n”;
vehicleobj,*ptr;
carobj1;
truckobj2;
ptr=&
obj;
prt->
message();
obj1;
ptr->
obj2;
message()
8、
voidfc()
try
{throw“sos”;
catch(int)
”sosint”<
try{
throw1;
catch(constchar*p){cout<
”sosstring”<
voidfb()
int*q=newint[100];
try{fc();
catch(…){
delete[]q;
throw;
voidfa()
int*p=newint[100];
try{fb();
delete[]p;
try{fa();
catch(…)
”anerroroccurredwhilerunning”<
9、
string.h>
classstudentid
studentid(intid=0)
value=id;
”Assigningstudentid”<
value<
~studentid()
”destructingid”<
intvalue;
classstudent
student(char*pname=”noname”,intssid=0):
id(ssid)
”constructingstudent”<
pname<
strcpy(name,pname);
name[sizeof(name)-1]=’\0’;
~student()
”destructingstudent”<
charname[20];
studentidid;
students(“randy”,9818);
10、
mem.h>
sample(){cout<
”constructingobject\n”;
~sample(){cout<
”destructingobject\n”;
sample*p1,*p2;
p1=newsample;
p2=(sample*)malloc(sizeof(sample));
deletep1;
free((void*)p2);
二、排除下列程序的错误(每处错误3分,共30分)
1、请指出以下程序的3处错误的位置
voidf1();
voidf2(intx);
voidf2(intx,inty=0);
voidf2(constint&
f1();
f2(3,4);
f2(3);
voidf1(intx)
voidf2(intx)
voidf2(intx,inty)
”,”<
y<
voidf2(constint&
x++<
2、请指出以下程序的4处错误的位置
sample(intxx,intzz)
x=xx;
z=zz;
z<
staticvoidf1()
void~sample()
{cout<
”destructingobject”<
staticinty=100;
constz;
constsamplea2(3,5);
a2.disp();
3、请指出以下程序的3处错误的位置
int*p;
base(intxx)
x=xx;
p=newint[x];
{delete[]p;
inty;
int*p1;
derive(intxx,intyy)
p=newint[x=xx];
y=yy;
p1=newint[yy];
~derive()
delete[]p1;
baseb1[10];
base*p1;
derive*p2;
p2=newderive(10,10);
p1=p2;
freep1;
三、程序设计题(20分)
下面是一个日期类的实现程序,试对以下程序增加对运算符号“<
”的重载函数,使语句行1处能够正确输出“2003年7月5日2002年6月5日”字样。
(要求不能修改类定义及类的实现部分。
)
classDATE
intyear;
intmonth;
intday;
DATE(intx=2003,inty=6,intz=5)
year=x;
month=y;
day=z;
voiddisp(ostream&
oo)
{oo<
year<
”年”<
month<
”月”<
day<
”日”;
DATEd1(2003,7,5),d2(2002,6,5);
d1<
d2<
//语句行1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 程序设计 试题
![提示](https://static.bdocx.com/images/bang_tan.gif)