设计思路:
首先定义Date类,声明三个protected成员变量(方便TimeWithDate类访问),year(年),month(月),day(日)。定义成员函数equal(比较日期是否相同),display(打印日期),increment(增加一天)。再定义protected成员函数,isLeapYear(判断是否闰年),getMonthDay(返回当年当月的天数)。
定义TimeWithDate类公共的方式继承Date类和Time类,
①定义成员函数equal(判断时间是否相同),在该函数中分别利用作用域运算符,分别调用Time类和Date类的equal方法,并作与运算。
②定义函数Add,实现对时间增加一定的秒数。
③定义函数subtract,实现两个日期相减,返回秒数。
④定义函数display,分别调用Time类和Date类的display方法。
代码实现:
timedate.hpp
#pragma once
#include<iostream>
using namespace std;
class Time
{
public:
Time() :hour(0), minute(0), sec(0) {}
Time(int a, int b, int c) :hour(a), minute(b), sec(c) {}
void display();
void set(int,int,int);
void increment();
bool equal(Time& other_time);
bool less_than(Time& other_time);
protected:
int hour, minute, sec;
};
class Date
{
public:
Date(int y=1,int m=1,int d=1):year(y),month(m),day(d){}
void increment();
bool equal(Date& other);
void display();
protected:
int year, month, day;
int getMonthDay(int y, int m);
bool isLeapYear(int y) { return (y % 400 == 0) || (y % 100 && y % 4 == 0); }
};
class TimeWithDate:public Time,public Date
{
public:
TimeWithDate(int y = 1, int m = 1, int d = 1, int h = 0, int mi = 0, int s = 0)
: Date(y, m, d), Time(h, mi, s) {}
void display() { Date::display(); Time::display(); }
bool equal(TimeWithDate& other) {
return Date::equal(other) && Time::equal(other);
}
void Add(int s);
int subtract(TimeWithDate& other);
};
TimeWithDate.cpp
#include "timedate.hpp"
void Time::display(){
cout << this->hour << ":" << this->minute << ":" << this->sec << endl;
}
void Time::set(int h,int m,int s){
this->hour = h; this->minute = m; this->sec = s;
}
void Time::increment()
{
this->sec++;
if (sec >= 60)
{
this->sec = 0; this->minute++;
if (minute >= 60)
{
minute = 0; hour++;
if (hour >= 24)hour = 0;
}
}
}
bool Time::equal(Time& a)
{//注意同类之间可以访问私有成员
if (a.hour == this->hour)
if (a.minute == this->minute)
if (a.sec == this->sec)
return true;
return false;
}
bool Time::less_than(Time& a)
{
if (this->hour < a.hour)return true;
else if (this->hour == a.hour)
{
if (this->minute < a.hour)return true;
else if (this->minute == a.minute)
return this->sec < a.sec;
}
return false;
}
int Date::getMonthDay(int y,int m) {
switch (m)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
return isLeapYear(y) ? 29 : 28;
default:
return 0;
}
}
void Date::increment()
{
int d = getMonthDay(year, month);
if (day == d) {
day = 1;
if (month == 12) {
month = 1;
year++;
}
else month++;
}
else day++;
}
void Date::display(){
cout << this->year << "-" << this->month << "-" << this->day << endl;
}
bool Date::equal(Date& other){
return this->year == other.year && this->month == other.month && this->day == other.day;
}
void TimeWithDate::Add(int s)
{
int d = sec + s;
sec = d % 60;d /= 60;
d=minute+d;
minute = d % 60;d /= 60;
d = hour + d;
hour = d % 24;
if (d > 24){
d /= 24;
d = day + d;
int md = getMonthDay(year, month);
while (d > md){
d -= md;
month += 1;
if (month > 12) {
month = 1;
year += 1;
}
md = getMonthDay(year, month);
}
day = d;
}
}
int TimeWithDate::subtract(TimeWithDate& other)
{
int totalSeconds = 0;
// 计算日期差
for (int i = other.year; i < this->year; i++) {
totalSeconds += isLeapYear(i) ? 366 : 365;
}
for (int i = 1; i < this->month; i++){
totalSeconds += getMonthDay(year, i);
}
totalSeconds += day;
for (int i = 1; i < other.month; i++) {
totalSeconds -= getMonthDay(other.year, i);
}
totalSeconds -= other.day;
totalSeconds *= 86400;
// 计算时间差
totalSeconds += (hour - other.hour) * 3600 + (minute - other.minute) * 60 + (sec - other.sec);
return totalSeconds;
}