首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int
使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int
admin
2019-06-07
39
问题
使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int。请在注释//********1********后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值,请在注释//********2********后添加适当的语句。
(3)完成重载符号“+=”的定义,请在注释/********3********后添加适当的语句。
(4)完成print()打印函数,如2008年8月8日到屏幕和文件modi3.txt格式相同,请在注释//********4********后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
#include
#include
#include
#include
using namespace std;
void WriteFile(int c)
{
ofstream out1;
out1.open("modi3.txt",ios
base::app);
out1<
out1.close();
}
void WriteFile(char*str)
{
ofstream out1;
out1.open("modi3.txt",ios
base::app);
out1<
out1.Close();
}
void ClearFile()
{
ofstream out1;
out1.open("modi3.txt");
out1.Close();
}
class Date
{
public:
Date(int y,int m,int d)
{
//********2********
}
void print()const;
//********3********
{
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:
//********1********
};
void Date::print()const
{
//********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)添加语句:int year,month,day; (2)添加语句:year=y;month=m;day=d; (3)添加语句:Date&operator+=(int m) (4)添加语句:cout<
解析
(1)题目l要求“定义私有成员变量year、month、day”。在C++程序的private区域中添加变量year、month、day的定义,即在第1个标识下添加“int year,month,day;”。
(2)题目2要求“完成构造函数,分别给year、month、day赋值”。在程序中“Date(int y,int m,int d)”的构造函数中添加给year、month、day赋值的语句,即在第2个标识下添加“year=y;month=m;day=d;”。
(3)题目3要求“完成重载符号“+=”的定义”。在C++中,运算符重载的定义方法是定义一个重载运算符的函数,格式为函数operator+重载运算符号+,所以这里补全“Date&operator+=(int m)”。
(4)题目4要求“完成函数print()打印函数”。在C++中的print()函数中补全其打印功能,即在第四个标识下添加“cout<
转载请注明原文地址:https://jikaoti.com/ti/56A0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列字符串中可以用作C++语言标识符的是
纯虚函数的声明总是以__________结束。
执行以下程序,输出结果的最后一行语句是__________。#includeclassSample{intx,y;public:Sample0{x=y=0;}
在程序设计阶段应该采取__________和逐步求精的方法,把一个模块的功能逐步分解,细化为一系列具体的步骤,继而用某种程序设计语言写成程序。
若需要把一个函数“voidfun();”定义为一个类TestClass的友元函数,则应在类TestClass的定义中加入一条语句:___________。
在下面的程序的横线处填上适当的语句,使该程序的输出为12。#includeusingnamespacestd;classBase{public:
有如下类声明:classMyClass{inti;private:intj;protected:intk;public:intm,n;};其中,私有成员
下列选项中,作为是数据库系统的核心的是()。
任何关系数据库系统都应该支持()。I、实体完整性约束Ⅱ、参照完整性约束Ⅲ、用户自定义完整性约束
下列有关继承和派生的叙述中,正确的是()。
随机试题
盆腔手术后应采取的卧位是()。
AT1受体拮抗剂的作用特点是
(2017年真题)秦简《法律答问》记载:“甲小未盈六尺,有马一匹自牧之,今马为人败,食人稼一石,问当论不当?不当论及偿稼。”依照该解答,秦律判断责任能力的标准是()。
()能把用户线路、电信电路和其他要互连的功能单元根据单个用户的请求连接起来。
海运提单按照收货人可以分为()。
在确定组织跨度和层次时,如果人员受过良好训练,具有高度责任感和独立性,则可()。
教学策略的基本特征为()
=___________.
A、 B、 C、 C原文说“Henry经常在家里拉小提琴”,所以[C]是正确的。
FormostofAmericanhistory,businesseswereruntoprovidelivelihoodsand"reasonable"profit.Inthelastfewdecades,thou
最新回复
(
0
)