简答题 C++
题号 #2254748

使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int。请在注释1后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值,请在注释2后添加适当的语句。
(3)完成重载符号“+=”的定义,请在注释3后添加适当的语句。
(4)完成函数print打印函数,如2005年1月5日到屏幕和文件out3.txt格式相同,请在注释4后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
#include<fstream>
#include<iomanip>
#include<cmath>
usingnamespacestd;
voidWriteFile(intc)
{
ofstreamout1;
out1.open("out3.txt",ios_base::app);out1<<C<<'';
out1.close();
}
voidWriteFile(char*str)
{
ofstreamout1;
out1.open("out3.txt",ios_base::app);out1<<str;
out1.close();
}
voidClearFile()
{
ofstreamout1;
out1.open("out3.txt"):
outl.close();
}
classDate
{
public:
Date(inty,intm,intd)
{
//********1*********}
voidprint();
//********2********
{
month+=m:
inti=month/12;
intj=month%12;
if(j==0)
{
year+=(i-1):
month=12;
}
else
{
year+=i:
month=j;
}
return*this:
}
private:
//********3********
};
voidDate::print()
{
//********4********
WriteFile(year);
WriteFile("年");
WriteFile(month);
WriteFile("月");
WriteFile(day);
WriteFile("日");
}
intmain()
{
ClearFile();
DateNationa1_day(2004,10,5);
National_day+=3;
National_day.print();
return0;
}

进入在线模考

参考答案

暂无

答案解析

(1)应添加“year=y;month=m;day=d;”。
(2)应添加“Date&operator+=(intm)”。
(3)应添加“intyear,month,day;”。
(4)应添加“cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;”。
【解析】本题要求私有成员变量year、month、day为整形表示方法,所以注释3应添加“intyear,month,day;”;注释1要求分别给year、month、day赋值,根据上下文提意,所以应添加“year=y;month=m;day=d;”;注释2是对“+=”运算符的重栽,Date&operator4+=(intm);第4处打印输出年、月、日,C++语言中,输出使用cout。

你可能感兴趣的试题

3 使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示个人基本信息的类PInfo,但类PInfo的定义并不完整。请按要求完成下列操作,将类Pin-fo的定义补充完成:
(1)定义私有数据成员bloodType用于表示血型,血型为char型的数据。请在注释1之后添加适当的语句。
(2)完成构造函数的定义,要求具有默认值,默认值为
身高175,体重70,血型A。请在注释2之后添加适当的语句。
(3)完成类PInfo外成员函数SetInfo的定义。请在注
释3之后添加适当的语句。
(4)在主函数main中调用成员函数Setlnfo,把对象d2的3个私有数据成员分别设定为身高170,体重64,血型B。
请在注释4之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
试题程序:
#include<iostream.h>
classPlnfo
{
private:
intheight;
intweight;
//********1********
public:
//********2********
:height(ht),weight(wt),bloodType(bt){};
Plnfo(Plnfo&h1):height(h1.height),
weight(h1.weight),bloodType(h1.bloodType){}
intGetHeight()
{
returnheight;
}
intGetWeight()
{
returnweight;
}
intGetBloodType()
{
returnbloodType;
}
voidSetlnfo(intht,intwt,charbt);
voidDisplay();
};
//********3********
{
height=ht;
weight=wt;
bloodType=bt;
}
voidPlnfo::Display()
{
cout<<"Humanlnf0:";
cout<<height<<"cm,"<<weight<<"Kg,Blood-
Type"<<bloodType<<endl;
}
voidmain()
{
PInfoh1(169,61,'A');
Plnfoh2;
Plnfoh3(h1);
Plnfoh4(h2);
//********4********
h1.Display();
h2.Display();
h3.Display():
h4.Display();
}
找试题 · 找答案 · 找资料 在线模考