2018年3月27日课堂笔记

1、

#include <iostream>

using namespace std;
#define DEBUG
class Date
{
private:
    int y_,m_,d_;
public:
    Date():y_(0),m_(0),d_(0){}
    Date(int y,int m,int d):y_(y),m_(m),d_(d)
    {
        if(m_>12||m_<1)
            #ifdef DEBUG
            cout<<"error";
            #endif // DEBUG
    }

};
int main()
{
    Date d(2018,13,27);
    return 0;
}

2、

#include <iostream>

using namespace std;
//#define DEBUG
class Date
{
private:
    int y_,m_,d_;
public:
    Date():y_(0),m_(0),d_(0){}
    Date(int y,int m,int d):y_(y),m_(m),d_(d)
    {
        if(is_not_correct_month())m_=1;
        if(is_not_correct_day())d_=1;
    }
    bool is_not_correct_month(){return m_>12||m_<0;}
    //这一句的意思是如果m_>12||m_<0那么返回true,否则返回false;然后执行上面Data的语句,如果true则m_=1;
    bool is_not_correct_day(){return d_>30||d_<1;}//这里执行的道理是一样的;
    friend ostream& operator<<(ostream& os,Date& d);
    friend istream& operator>>(istream& is,Date& d);
    Date& operator++();
    Date& operator--();
public:
    const gety()const{return y_;}

};
Date& Date::operator++()
{
    if(++d_>30)//修改值,让其变得符合正常;
    {
        d_=1;
        if(++m_>12)
        {
            m_=1;
            ++y_;
        }
    }
}
Date& Date::operator--()
{
    if(--d_<1)
    {
        d_=30;
        if(--m_<1)
        {
            m_=12;
            --y_;
        }
    }
}
ostream& operator<<(ostream& os,Date& d)
{
    os<<d.y_<<"-"<<d.m_<<"-"<<d.d_<<endl;
    return os;
}
istream& operator>>(istream& is,Date& d)
{
    is>>d.y_>>d.m_>>d.d_;
    return is;
}
int main()
{
    Date d1,d2(2018,1,1);
    ++d1;
    --d2;
//    cin>>d1;
    cout<<d1<<d2;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值