题目要求
创键一个Cdate类,该类中有y(年)、m(月)、d(日)数据成员及初始化这些数据的构造函数
和设置该日期的外联成员函数和显示日期的友元函数。
完成在主程序中初始化日期、设置键入的日期和显示该日期的操作。
代码
#include<iostream>
using namespace std;
class Cdate
{
private:
int y,m,d;
public:
Cdate(){
y=2020;
m=5;
d=10;
};
void set(int yy,int mm,int dd);
~Cdate();
friend void display(Cdate);
};
void display(Cdate aa)
{
printf("%.4d:%.2d:%.2d\n",aa.y,aa.m,aa.d);
// cout<<aa.y<<":"<<aa.m<<":"<<aa.d<<endl;
}
void Cdate::set(int yy,int mm,int dd)
{
y=yy;
m=mm;
d=dd;
}
Cdate::~Cdate()
{
}
void menu()
{
cout<<"0------Exit"<<endl;
cout<<"1------Set Date"<<endl;
cout<<"2------Show Date"<<endl;
}
int main()
{
Cdate a;
while(1)
{
menu();
int key;
cin>>key;
if(key==0)
{
break;
}
if(key==1)
{
int y,m,d;
cout<<"You are now setting the date."<<endl;
cout<<"Please input year:"<<endl;
cin>>y;
cout<<"Please input month:"<<endl;
cin>>m;
while(m>12||m<0)
{
cout<<"You enter wrong month,input again"<<endl;
cin>>m;
}
cout<<"Please input day:"<<endl;
cin>>d;
while(d>31||d<0)
{
cout<<"You enter wrong date,input again"<<endl;
cin>>d;
}
a.set(y,m,d);
cout<<"Set OK."<<endl;
}
if(key==2)
{
display(a);
}
}
system("pause");
}