/******************************************************************************************
Date.hpp:
Copyright (c) Bit Software, Inc.(2013), All rights reserved.
Purpose:
声明并实现一个万年历类【腾讯面试题】
Author:
xxx
Created Time:
2015-4-26
******************************************************************************************/
class Time
{
public:
Time(int hour = 1)
{
_hour = hour;
}
Time(const Time& t)
{
_hour = t._hour;
}
Time& operator=(const Time& t)
{
this->_hour = t._hour;
return *this;
}
private:
int _hour;
};
class Date
{
public:
// 初始化列表进行初始化。
Date (int year = 1900, int month = 1, int day = 1, const Time& t = 1)
:_year(year)
,_month(month)
,_day(day)
,_t(t)
{}
Date (const Date& d)
:_year(d._year)
,_month(d._month)
,_day(d._day)
,_t(d._t)
{}
Date& operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
_t = d._t;
return *this;
}
void Display ()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
public:
//
// 比较日期的运算符重载
//
bool operator == (const Date& d)
{
return _year == d._year &
【C++】万年历的实现
最新推荐文章于 2024-07-18 18:39:15 发布
本文档展示了如何使用C++实现一个万年历类,包括日期的比较、加减天数以及日期运算符的重载。类`Date`包含了年、月、日和时间成员,并提供了显示日期、日期比较和日期加减的功能。
摘要由CSDN通过智能技术生成