面向对象的程序设计综合题

①定义 date 类对象 d1、d2,分别用于表示 2020 年 5 月 8 日、默
认日期;
②对 d1、d2 进行比较,并输出较小者的日期;
③动态建立 datetime 类对象以表示 2020 年 4 月 14 日 8 时 0 分 0
秒,并定义指针变量 p 使其指向该对象;
④为 p 所指向的对象增加 100 秒,并输出运算后的对象信息;
⑤撤销 p 所指向的对象。

#include<iostream>
using namespace std;
class date
{
    protected:
    int year,month,day;
    public:
    date()
    {
        year=1970;
        month=1;
        day=1;
    }
    date(int year,int month,int day)
    {
        this->year=year;
        this->month=month;
        this->day=day;
    }
    void show()
    {
        cout<<year<<"年-"<<month<<"月-"<<day<<"日";
    }
    bool operator<(date &d)
    {
        if(year<d.year) return true;
        if(year==d.year&&month<d.month) return true;
        if(year==d.year&&month==d.month&&day<d.day) return true;
        return false;
    }
};
class datetime:public date
{
    private:
    int hour,min,sec;
    public:
    datetime()
    {
        date();
        hour=12;
        min=0;
        sec=0;
    }
    datetime(int year,int month,int day,int hour,int min,int sec):date(year,month,day)
    {
        this->hour=hour;
        this->min=min;
        this->sec=sec;
    }
    void showT()
    {
        show();
        cout<<" "<<hour<<":"<<min<<":"<<sec<<endl;
    }
    friend datetime operator+(datetime &t,int k);
};
datetime operator+(datetime &t,int k)
{
    datetime tt;
    tt.year=t.year;
    tt.month=t.month;
    tt.day=t.day;
    int h=k/3600;
    int m=(k%3600)/60;
    int s=k%60;
    tt.hour=t.hour+h;
    tt.min=t.min+m;
    tt.sec=t.sec+s;
    return tt;
}



int main()
{
    //①定义 date 类对象 d1、d2,分别用于表示 2020 年 5 月 8 日、默认日期;
    date d1(2020,5,8);
    date d2;
    //②对 d1、d2 进行比较,并输出较小者的日期;
    if(d1<d2)
    {
        d1.show();
    }else{
        d2.show();
    }
    cout<<endl;
    //③动态建立 datetime 类对象以表示 2020 年 4 月 14 日 8 时 0 分 0秒,并定义指针变量 p 使其指向该对象;
    datetime *p;
    p=new datetime(2020,4,14,8,0,0);
    //④为 p 所指向的对象增加 100 秒,并输出运算后的对象信息;
    (*p)=(*p)+100;
    p->showT();
    //⑤撤销 p 所指向的对象。
    delete p;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值