设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期 相差的天数等。要求:使用运算符重载。

#include <iostream>

#include <cmath>

using namespace std;

class date

{

public:

    date(){a = 0, b = 0, c = 0;}

    date(int aa, int bb, int cc){a = aa; b = bb; c = cc;}

    friend date operator +(date &d1, int &i);

    friend date operator -(date &d1, int &i);

    friend date operator *(date &d1, date &d2);

    void display()

    {

      cout<<a<<"/"<<b<<"/"<<c<<endl;

    };

    void display1()

    {

       cout<<"他们之间相差了";

        cout<<c<<endl;

    }

    //~date(){cout<<"!"<<endl;}

private:

    int a, b, c;

};

date operator + (date &d1, int &i)

{

    date d;

    int sum;

    sum = d1.a*360+d1.b*30+d1.c+i;

    d.a = sum/360;

    sum = sum-(360*d.a);

    d.b = sum/30;

    d.c = sum%30;

    return d;

}

date operator -(date &d1, int &i)

{

       date d;

    int sum;

    sum = d1.a*360+d1.b*30+d1.c-i;

    d.a = sum/360;

    sum = sum-(360*d.a);

    d.b = sum/30;

    d.c = sum%30;

    return d;

}

date operator *(date &d1, date &d2)

{

    date d;

    int sum1, sum2;

    sum1 = d1.a*360+d1.b*30+d1.c;

    sum2 = d2.a*360+d2.b*30+d2.c;

    d.c = abs(sum1-sum2);

    return d;

}

int main()

{

    date d1(2019,4,22), d2(2019,4,30);

    int i = 10;

    date dd;

    dd = d1+i;

    dd.display();

    dd = d2-i;

    dd.display();

    dd = d1*d2;

    dd.display1();

    return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值