题目描述
定义一个Date类,该类有year、month和day三个私有成员,用于存储日期的年、月和日信息。
为该类定义默认构造函数、带参的构造函数、复制构造函数和读写数据成员的函数setDate和showDate。
其中,setDate用于设置年月日信息,showDate用于输出日期信息。
在主函数中用不同的构造函数创建Date对象并进行测试。
注意:程序的开始部分(必要的文件包含和using namespace std; )和main函数已经写好了,你只需要定义Date类并实现它的成员函数即可(只需要提交这部分代码)。
提示
main函数如下:
int main(){
Date d1;
d1.showDate();
Date d2(2021,3,13);
d2.showDate();
d2.setDate(2021,5,1);
d2.showDate();
Date d3(d1);
d3.showDate();
return 0;
}
样例输出
2021/4/16
2021/3/13
2021/5/1
2021/4/16
#include<iostream>
using namespace std;
class Date{
public:
void setDate(int y,int m,int d);
void showDate();
private:
int year;
int month;
int day;
};
void Date::setDate(int y,int m,int d){
year = y;
month = m;
day = d;
}
void Date::showDate(){
cout<<year<<"."<<month<<"."<<day<<endl;
}
int main(){
Date d1;
d1.showDate();
Date d2(2021,3,13);
d2.showDate();
d2.setDate(2021,5,1);
d2.showDate();
Date d3(d1);
d3.showDate();
return 0;
}