全球某工商C++上机题(创键一个Cdate类....)

题目要求

创键一个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");
}
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值