首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int
使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int
admin
2013-11-15
20
问题
使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int。请在注释1后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值。请在注释2后添加适当的语句。
(3)完成重载符号“+=”的定义,请在注释3后添加适当的语句。
(4)完成print打印函数,输出到屏幕和文件的格式相同,请在注释4后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
程序正常运行,输出的结果为2008年11月8日。
试题程序:
#include
#include
#include
#include
using namespace std;
void WriteFile(int c)
{
ofstream out1;
out1.open("3.txt",ios_base::app);
out1<
out1.close();
}
void WriteFile(char*str)
{
ofstream out1;
out1.open("3.txt",ios_base::app);
out1<
out1.close();
}
void ClearFile()
{
ofstream out1;
out1.open("3.txt");
out1.close();
}
class Date
{
public:
Date(int y,int m,int d)
{
//* * * * * * * * 1 * * * * * * * *
}
void print();
//* * * * * * * * 2 * * * * * * * *
{
month+=m:
int i=month/12:
int j=month%12;
if(j==0)
{year+=(i-1);
month=12:
}
else
{
year+=i:
month=j;
}
return *this;
}
private:
//* * * * * * * * 3 * * * * * * * *
};
void Date::print()
{
//* * * * * * * * 4 * * * * * * * *
WriteFile(year);
WriteFile("年");
WriteFile(month);
WriteFile("月");
WriteFile(day);
WriteFile("日");
}
int main()
{
ClearFile();
Date Oly_day(2008,8,8);
Oly_day+=3:
Oly_day.print();
return 0;
}
选项
答案
(1)应添加“year=y;month=m;day=d;”。 (2)应添加“Date & operator+=(int m)”。 (3)应添加“int year,month,day;”。 (4)应添加“cout<
解析
本题中第3处要求“定义私有成员变量year、month、day”。在C++程序的private区域中添加变量year、month、day的定义,即在第3处添加“int year,month,day;”。
第1处要求“完成构造函数,分别给year、month、day赋值”。在程序中“Date(int y,int m,int d)”的构造函数中添加给year、month、day赋值的语句,即在第1处添加“year=y;month=m;day=d;”。第2处要求“完成重载符号‘+=’的定义”。在C++中,运算符重载的定义方法是定义一个重载运算符的函数,格式为函数operator+重载运算符号+,所以这里补全“Date&operator+=(int m)”。
第4处要求“完成函数print()打印函数”。在C++中的print函数中补全其打印功能,即在第四个标识下添加“cout<
转载请注明原文地址:https://jikaoti.com/ti/eKE0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
为了避免流程图在描述程序逻辑时的灵活性,提出了用方框图来代替传统的程序流程图,通常也把这种图称为()。
在类中定义和实现的函数称为【】。
在函数定义前加上关键字inline,表示该函数被定义为()。
下列字符常量的写法中,错误的是()。
带有基类的多层派生类构造函数的成员初始化列表中都要排出虚基类的构造函数,这样将对虚基类的子对象初始()。
以下关键字不能用来声明类的访问权限的是()。
下列叙述中错误的是()。
下面叙述错误的是()。
在深度为5的满二叉树中,叶子结点的个数为()。
多数运算符既能作为类的成员函数重载,也能作为类的非成员函数重载,但口运算符只能作为类的【】函数重载。
随机试题
对钢管表面的油污和可溶性盐经喷(抛)除锈后可以被去除干净。
根据《商业银行合规风险管理指引》,下列属于合规风险管理体系基本要素的有()。
甲公司拟自建一条生产线,与该生产线建造相关的情况如下:(1)2×21年1月2日,甲公司发行公司债券,专门筹集生产线建设资金。该公司债券为3年期分期付息、到期还本债券,面值为3000万元,票面年利率为5%,发行价格为3069.75万元,另在发
根据参与联合的经营者所处的产业链环节是相同还是相续,可分为横向垄断协议行为和纵向垄断协议行为。下列选项中,属于横向垄断协议行为的有()。
警察机关为了完成法律赋予自己的职责,保障强制力的权威性,配有一定的武器和械具,是一支重要的具有武装性质的行政力量。()
公安机关的人民警察是维护社会治安的排头兵,也是服务群众的重要力量。2017年5月,离开大陆几十年的华侨严老先生携子女回到家乡探亲,并准备在家乡投资,因寻弟未果无奈中到县政府政务服务大厅求助,希望有关部门帮助他,能在有生之年寻找到失散多年的胞弟,接刮政务平台
在这个国家的历史上,一直都缺乏对于个体的足够尊重。(A)自春秋至近代中国,尊严一词多与权力有关,但在现代,尊严一词更多是与权利有关,简言之,尊严就是独立个体的权利被尊重,但是古代中国的等级秩序森严、个人本位缺失、权利意识匮乏、礼法与宗法约束、私有产权不受保
实践是人的存在方式,是指
AllovertheworldmentionoftheBritisheducationsuggestsapictureofthe"publicschool",anditsuggestsinparticularthe
A、OntheNewYear’sDay.B、JustbeforetheSpringFestival.C、Atthemidnight.D、OntheeveofChristmas.B
最新回复
(
0
)