7.5 应用示例

 

7.5 应用实例—日期和时间

下面是一个有关日期(年、月、日)和时间(时、分、秒)的程序。该程序建立了三个类,分别是日期类Date、时间类Time和基于前两个类派生的日期和时间类TimeDate

程序代码

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

typedef char string80[80];

class Date {
public:
    Date() {}
    Date(int y, int m, int d) {
        SetDate(y, m, d);
    }
    void SetDate(int y, int m, int d) {
        Year = y;
        Month = m;
        Day = d;
    }
    void GetStringDate(string80 &Date) {
        sprintf(Date, "%d/%d/%d", Year, Month, Day);
    }
protected:
    int Year, Month, Day;
};

class Time {
public:
    Time() {}
    Time(int h, int m, int s) {
        SetTime(h, m, s);
    }
    void SetTime(int h, int m, int s) {
        Hours = h;
        Minutes = m;
        Seconds = s;
    }
    void GetStringTime(string80 &Time) {
        sprintf(Time, "%d:%d:%d", Hours, Minutes, Seconds);
    }
protected:
    int Hours, Minutes, Seconds;
};

class TimeDate : public Date, public Time {
public:
    TimeDate() : Date(), Time() {}
    TimeDate(int y, int mo, int d, int h, int mi, int s) : Date(y, mo, d), Time(h, mi, s) {}
    void GetStringDT(string80 &DTstr) {
        sprintf(DTstr, "%d/%d/%d %d:%d:%d", Year, Month, Day, Hours, Minutes, Seconds);
    }
};

int main() {
    TimeDate datel, date2(1998, 8, 12, 12, 45, 10);
    string80 DemoStr;
    
    datel.SetDate(1998, 8, 7);
    datel.SetTime(10, 30, 45);
    
    datel.GetStringDT(DemoStr);
    cout << "The datel date and time is " << DemoStr << endl;
    
    datel.GetStringDate(DemoStr);
    cout << "The datel date is " << DemoStr << endl;
    
    datel.GetStringTime(DemoStr);
    cout << "The datel time is " << DemoStr << endl;
    
    date2.GetStringDT(DemoStr);
    cout << "The date2 date and time is " << DemoStr << endl;
    
    return 0;
}

程序说明

输出结果

The datel date and time is 1998/8/7 10:30:45
The datel date is 1998/8/7
The datel time is 10:30:45
The date2 date and time is 1998/8/12 12:45:10

说明

  1. 程序包含的头文件

    • #include <iostream>:标准输入输出流库。
    • #include <string.h>:字符串操作函数库。
    • #include <stdio.h>:标准输入输出函数库,包括sprintf函数。
  2. 类型定义

    typedef char string80[80];
    

    该语句定义了一个新的类型string80,它是char型的一维数组,有80个元素。

  3. 类关系: 程序中定义了三个类:DateTimeTimeDateTimeDate类通过多继承继承了Date类和Time类。

    • Date类:用于表示日期,包含年、月、日三个成员。
    • Time类:用于表示时间,包含时、分、秒三个成员。
    • TimeDate类:继承自DateTime,用于同时表示日期和时间。

    关系图如下:

    Date
    Time
    TimeDate
    

  4. 构造函数

    • Date类和Time类分别有默认构造函数和带参数的构造函数,用于初始化日期和时间。
    • TimeDate类的构造函数调用了其两个基类的构造函数来初始化日期和时间。
  5. 成员函数

    • GetStringDate:将日期转换为字符串格式。
    • GetStringTime:将时间转换为字符串格式。
    • GetStringDT:将日期和时间转换为字符串格式。

小结

本实例通过多继承实现了日期和时间的组合,展示了继承和成员函数的使用方法。通过使用typedef定义新类型和sprintf格式化字符串,使得代码更加简洁和易读。通过这种方式,可以方便地操作日期和时间数据,进行格式化输出。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏驰和徐策

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值