};
其中的四个函数定义中正确的是(B)。
A.①B.②C.③D.④
3.13有如下类定义:
classTest{
public:
Test(){a=0;c=0;}//①常成员必须在初始化列表中初始化
intf(inta)const{this->a=a;}//②常函数不能修改成员
voidh(intb){Test:
:
b=b;};//③
staticintg(){returna;}//④静态函数要通过对象访问成员
private:
inta;
staticintb;
constintc;
};
intTest:
:
b=0;
在标注号码的行中,能被正确编译的是(C)。
A.①B.②C.③D.④
4写出程序程序执行结果。
4.1#include
usingnamespacestd;
classTest{
public:
Test(){n+=2;}
~Test(){n-=3;}
staticintgetNum(){returnn;}
private:
staticintn;
};
intTest:
:
n=1;
intmain(){
Test*p=newTest;
deletep;
cout<<”n=”<:
getNum()<return0;
}
n=0
4.2#include
usingnamespacestd;
classAA{
intn;
public:
AA(intk):
n(k){}
intget(){returnn;}
intget()const{returnn+1;}
};
intmain(){
AAa(5);
constAAb(6);
cout<return0;
}
57
4.3#include
usingnamespacestd;
classMyClass{
public:
intnumber;
voidset(inti);
};
intnumber=3;
voidMyClass:
:
set(inti){
number=i;
}
voidmain(){
MyClassmy1;
intnumber=10;
my1.set(5);
cout<my1.set(number);
cout<my1.set(:
:
number);
cout<}
5103
4.4#include
usingnamespacestd;
classTest{
private:
staticintval;
inta;
public:
staticintfunc();
voidsfunc(Test&r);
};
intTest:
:
val=200;
intTest:
:
func(){
returnval++;
}
voidTest:
:
sfunc(Test&r){
r.a=125;
cout<<"Result3="<}
voidmain(){
cout<<"Result1="<:
func()<Testa;
cout<<"Result2="<a.sfunc(a);
}
Result1=200
Result2=201
Result1==125
5.编程题
5.1构造一个日期时间类(Timedate),数据成员包括年、月、日和时、分、秒,函数成员包括设置日期时间和输出时间,其中年、月用枚举类型,并完成测试(包括用成员函数和用普通函数)。
#include
#include
#include
enumYR{Y2000,Y2001,Y2002,Y2003,Y2004,Y2005};//枚举名必须是标识符
enumMT{Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
classTimedate{
private:
YRyear;
MTmonth;
intdate;
inthh;
intmm;
intss;
public:
Timedate(){year=Y2000;month=Jan;date=1;hh=0;mm=0;ss=0;}
Timedate(YRa,MTb,intc){
year=a;
month=b;
date=c;
hh=12;mm=30;ss=0;
}
voidgetdate(YR&,MT&,int&);//体会用引用的优点,一次可返回3个值
voidgettime(int&,int&,int&);
voidputdate(YR,MT,int);
voidputtime(int,int,int);
voidlist();
};
voidTimedate:
:
getdate(YR&y,MT&m,int&d){
y=year;
m=month;
d=date;
}
voidTimedate:
:
gettime(int&a,int&b,int&c){
a=hh;
b=mm;
c=ss;
}
voidTimedate:
:
putdate(YRa,MTb,intc){
year=a;
month=b;
date=c;
}
voidTimedate:
:
puttime(inta,intb,intc){
hh=a;
mm=b;
ss=c;
}
voidTimedate:
:
list(){//成员函数
cout<<"year/month/date:
";
switch(year){
caseY2000:
cout<<"2000";break;
caseY2001:
cout<<"2001";break;
caseY2002:
cout<<"2002";break;
caseY2003:
cout<<"2003";break;
caseY2004:
cout<<"2004";break;
caseY2005:
cout<<"2005";break;
}
switch(month){//,,,,,,,,
caseJan:
cout<<'/'<<"Jan";break;
caseFeb:
cout<<'/'<<"Feb";break;
caseMar:
cout<<'/'<<"Mar";break;
caseApr:
cout<<'/'<<"Apr";break;
caseMay:
cout<<'/'<<"May";break;
caseJun:
cout<<'/'<<"Jun";break;
caseJul:
cout<<'/'<<"Jul";break;
caseAug:
cout<<'/'<<"Aug";break;
caseSep:
cout<<'/'<<"Sep";break;
caseOct:
cout<<'/'<<"Oct";break;
caseNov:
cout<<'/'<<"Nov";break;
caseDec:
cout<<'/'<<"Dec";break;
}
cout<<'/'<cout<<"hour:
minite:
second:
";
cout<'<'<}
voidshow(Timedate&td){//普通函数,体会两者的不同
YRyear;
MTmonth;
intdate,hh,mm,ss;
td.getdate(year,month,date);
td.gettime(hh,mm,ss);
cout<<"year/month/date:
";
switch(year){
caseY2000:
cout<<"2000";break;
caseY2001:
cout<<"2001";break;
caseY2002:
cout<<"2002";break;
caseY2003:
cout<<"2003";break;
caseY2004:
cout<<"2004";break;
caseY2005:
cout<<"2005";break;
}
switch(month){//,,,,,,,,
caseJan:
cout<<'/'<<"Jan";break;
caseFeb:
cout<<'/'<<"Feb";break;
caseMar:
cout<<'/'<<"Mar";break;
caseApr:
cout<<'/'<<"Apr";break;
caseMay:
cout<<'/'<<"May";break;
caseJun:
cout<<'/'<<"Jun";break;
caseJul:
cout<<'/'<<"Jul";break;
caseAug:
cout<<'/'<<"Aug";break;
caseSep:
cout<<'/'<<"Sep";break;
caseOct:
cout<<'/'<<"Oct";break;
caseNov:
cout<<'/'<<"Nov";break;
caseDec:
cout<<'/'<<"Dec";break;
}
cout<<'/'<cout<<"hour:
minite:
second:
";
cout<'<'<}
intmain(intargc,char*argv[]){
TimedateA(Y2004,Mar,3),B;
A.list();
B.list();
B.putdate(Y2005,Oct,18);
B.puttime(17,30,00);
B.list();
show(B);
return0;
}
5.2定义一个日期类Date,具有年月日等数据成员,显示日期、加减天数等函数成员。
注意需要考虑闰年。
#include
usingnamespacestd;
classDate{
intyear,month,day;
staticconstintdays[];
boolLeapYear();
boolEndofMonth();
public:
Date(int=1900,int=1,int=1);
voidIncrement(int);
voidDecrement(int);
voidSetDate(int,int,int);
voidPrint();
};
constintDate:
:
days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
Date:
:
Date(inty,intm,intd){
SetDate(y,m,d);
}
voidDate:
:
SetDate(inty,intm,intd){
year=(y>=1900&&y<=9999)?
y:
1900;
month=(m>=1&&m<=12)?
m:
1;
if(month==2&&LeapYear())
day=(d>=1&&d<=29)?
d:
1;
else
day=(d>=1&&d<=days[month])?
d:
1;
}
boolDate:
:
LeapYear(){
return((year%400==0)||(year%4==0&&year%100!
=0))?
true:
false;
}
boolDate:
:
EndofMonth(){
if(month==2&&LeapYear())
returnday==29;
else
returnday==days[month];
}
voidDate:
:
Increment(intn){
inti;
for(i=1;i<=n;i++)
if(EndofMonth()&&month==12){
year++;month=day=1;
}
elseif(EndofMonth()){
month++;day=1;
}
else
day++;
}
voidDate:
:
Decrement(intn){
inti;
for(i=1;i<=n;i++)
if(day==1){
if(month==1){
year--;month=12;day=31;
}
elseif(month==3){
day=LeapYear()?
29:
28;month=2;
}
else
day=days[--month];
}
else
day--;
}
voidDate:
:
Print(){
cout<}
voidmain(){
Dated1(2005,1,4),d2(2004,12,27);
d1.Print();
d1.Decrement(4);
d1.Print();
d2.Print();
d2.Increment(70);
d2.Print();
}
5.3定义一个圆类(Circle),属性为半径(radius)、圆周长和面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。
要求定义构造函数(以半径为参数,默认值为0,周长和面积在构造函数中生成)和复制构造函数。
#include
constdoublePI=3.1415;
usingnamespacestd;
classCircle{
doubleradius,length,area;
public:
Circle(doubler=0){
setr(r);
length=2*PI*radius;
area=PI*radius*radius;
}
Circle(Circle&c){
radius=c.radius;
length=c.length;
area=c.area;
}
voidprint(){
cout<<"radius="<cout<<"length="<cout<<"area="<
}
voidsetr(doubler){
radius=r;
}
};
intmain(){
Circlec1(10),c2(c1);
c1.print();
c2.print();
return0;
}
5.4设计一个学校在册人员类(Person)。
数据成员包括身份证号(IdPerson)、姓名(Name)、性别(Sex)、生日(Birth-day)和家庭住址(HomeAddress)。
成员函数包括人员信息的录入和显示,还包括构造函数与复制构造函数。
设计一个合适的初始值。
#include
#include
constdoublePI=3.1415;
usingnamespacestd;
structdate{
intyear,month,day;
};
classPerson{
charID[12];
charName[10];
charSex[3];
dateBirth;
charHomeAdd[20];
public:
Person(){
ID[0]=Name[0]=Sex[0]=HomeAdd[0]=0;
Birth.year=Birth.month=Birth.day=0;
}
Person(charid[],charname[],charsex[],datebirth,charhomeadd[]){
strcpy(ID,id);
strcpy(Name,name);
strcpy(Sex,sex);
Birth=birth;
strcpy(HomeAdd,homeadd);
}
Person(Person&p){
strcpy(ID,p.ID);
strcpy(Name,p.Name);
strcpy(Sex,p.Sex);
Birth=p.Birth;
strcpy(HomeAdd,p.HomeAdd);
}
voidsetID(charid[]){
strcpy(ID,id);
}
vo