1、定义日期“Date”类,类中包含数据成员年、月、日,成员函数包括构造函数(只有带参的构造函数),设置日期,或取年、月、日,显示年、月、日等 2、定义时间“Time”类,数据成员包括时分秒。成员函数的要求同date类 3、定义日期时间型“Date_Time”类 ⑴公有继承Date类及Time类; ⑵不增加数据成员; ⑶增加日期时间处理成员函数,包括初始化日期时间、设置日期时间、显示日期时间等。在 4、每个类都要求重载>> 和<<运算符< span=""> 5.对Date_Time,重载〉运算符。 6. 编写main()函数进行测试。在主函数中,输入两个Date_Time类的数据,按照时间从小到大的顺序输出两个时间信息。用c++实现
时间: 2024-03-22 18:37:34 浏览: 194
用c++设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期相差的天数等。
5星 · 资源好评率100%
好的,下面是实现您所需的代码:
```cpp
#include <iostream>
#include <iomanip>
using namespace std;
class Date {
public:
Date(int year = 0, int month = 0, int day = 0) : year_(year), month_(month), day_(day) {}
friend istream& operator>>(istream& in, Date& date);
friend ostream& operator<<(ostream& out, const Date& date);
void SetDate(int year, int month, int day) {
year_ = year;
month_ = month;
day_ = day;
}
int GetYear() const {
return year_;
}
int GetMonth() const {
return month_;
}
int GetDay() const {
return day_;
}
private:
int year_;
int month_;
int day_;
};
istream& operator>>(istream& in, Date& date) {
in >> date.year_ >> date.month_ >> date.day_;
return in;
}
ostream& operator<<(ostream& out, const Date& date) {
out << setfill('0') << setw(4) << date.year_ << "-"
<< setw(2) << date.month_ << "-" << setw(2) << date.day_;
return out;
}
class Time {
public:
Time(int hour = 0, int minute = 0, int second = 0) : hour_(hour), minute_(minute), second_(second) {}
friend istream& operator>>(istream& in, Time& time);
friend ostream& operator<<(ostream& out, const Time& time);
void SetTime(int hour, int minute, int second) {
hour_ = hour;
minute_ = minute;
second_ = second;
}
int GetHour() const {
return hour_;
}
int GetMinute() const {
return minute_;
}
int GetSecond() const {
return second_;
}
private:
int hour_;
int minute_;
int second_;
};
istream& operator>>(istream& in, Time& time) {
in >> time.hour_ >> time.minute_ >> time.second_;
return in;
}
ostream& operator<<(ostream& out, const Time& time) {
out << setfill('0') << setw(2) << time.hour_ << ":"
<< setw(2) << time.minute_ << ":" << setw(2) << time.second_;
return out;
}
class Date_Time : public Date, public Time {
public:
Date_Time(int year = 0, int month = 0, int day = 0, int hour = 0, int minute = 0, int second = 0)
: Date(year, month, day), Time(hour, minute, second) {}
friend istream& operator>>(istream& in, Date_Time& datetime);
friend ostream& operator<<(ostream& out, const Date_Time& datetime);
friend bool operator>(const Date_Time& datetime1, const Date_Time& datetime2);
void SetDateTime(int year, int month, int day, int hour, int minute, int second) {
SetDate(year, month, day);
SetTime(hour, minute, second);
}
};
istream& operator>>(istream& in, Date_Time& datetime) {
in >> static_cast<Date&>(datetime) >> static_cast<Time&>(datetime);
return in;
}
ostream& operator<<(ostream& out, const Date_Time& datetime) {
out << static_cast<const Date&>(datetime) << " " << static_cast<const Time&>(datetime);
return out;
}
bool operator>(const Date_Time& datetime1, const Date_Time& datetime2) {
if (datetime1.GetYear() > datetime2.GetYear()) {
return true;
}
else if (datetime1.GetYear() < datetime2.GetYear()) {
return false;
}
else {
if (datetime1.GetMonth() > datetime2.GetMonth()) {
return true;
}
else if (datetime1.GetMonth() < datetime2.GetMonth()) {
return false;
}
else {
if (datetime1.GetDay() > datetime2.GetDay()) {
return true;
}
else if (datetime1.GetDay() < datetime2.GetDay()) {
return false;
}
else {
if (datetime1.GetHour() > datetime2.GetHour()) {
return true;
}
else if (datetime1.GetHour() < datetime2.GetHour()) {
return false;
}
else {
if (datetime1.GetMinute() > datetime2.GetMinute()) {
return true;
}
else if (datetime1.GetMinute() < datetime2.GetMinute()) {
return false;
}
else {
if (datetime1.GetSecond() > datetime2.GetSecond()) {
return true;
}
else {
return false;
}
}
}
}
}
}
}
int main() {
Date_Time datetime1, datetime2;
cout << "Please input the first datetime (yyyy-MM-dd hh:mm:ss): ";
cin >> datetime1;
cout << "Please input the second datetime (yyyy-MM-dd hh:mm:ss): ";
cin >> datetime2;
if (datetime1 > datetime2) {
cout << datetime2 << " " << datetime1 << endl;
}
else {
cout << datetime1 << " " << datetime2 << endl;
}
return 0;
}
```
上述代码实现了日期、时间、日期时间型三个类,并重载了输入输出运算符以及比较运算符。其中日期和时间的输入输出运算符实现很简单,直接使用了流输入输出。日期时间型的输入输出运算符则将输入分别交给日期和时间输入运算符处理,输出则将日期和时间输出运算符的结果拼接起来。
比较运算符的实现比较繁琐,需要逐个比较年、月、日、时、分、秒,直到找到不同之处或者比较完毕。在比较时,我们先比较年,如果年相同则比较月,以此类推。这种比较方式可以确保按照时间从小到大的顺序输出两个时间信息。
阅读全文