1.題目
實現日期類的運算符 +, -, +=, -=, ==, !=, >=, <= ,>, <, >> ,<<, 日期-日期等。
2.代碼展示
Date.h
#include<iostream>
using namespace std;
class Date
{
public:
//獲取某月的天數
friend int Getmonthday(int year, int month);
//構造函數全缺省
Date(int year, int month, int day);
//析構函數
~Date();
//重載>
friend bool operator>(const Date& d1, const Date& d2);
//運算符==重載
friend bool operator==(const Date& d1, const Date& d2);
//運算符!=重載
friend bool operator!=(const Date& d1, const Date& d2);
//運算符>=重載
friend bool operator>=(const Date& d1, const Date& d2);
//運算<重載
friend bool operator<(const Date& d1, const Date& d2);
//運算<=重載
friend bool operator<=(const Date& d1, const Date& d2);
//賦值運算符號 =
//friend Date& operator=(Date& d1, const Date& d2);
//日期-=天數
friend Date& operator-=(Date& d1, int day);
//日期-天數
friend Date& operator-(Date& d1, int day);
//日期+天數
friend Date& operator+(Date& d1, int day);
//日期+=天數
friend Date& operator+=(Date& d1, int day);
//前置++
friend Date& operator++(Date& d1);
//後置++
friend Date operator++(Date& d1, int);
//前置--
friend Date& operator--(Date& d1);
//後置--
friend Date operator--(Date& d1, int);
//日期-日期
friend int operator-(const Date& d1, const Date& d2);
//<<
friend ostream& operator<<(ostream& _out, const Date& d);
//>>
friend istream& operator>>(istream& _in, Date& d);
private:
int _year;
int _month;
int _day;
};
Date.cpp
#include"Date.h"
//獲取某月的天數
int Getmonthday(int year, int month){
int day[13] = { 0, 31,28,31,30,31,30,31,30,31,30,31,30 };
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
day[2] = 29;
}
return day[month];
}
//構造函數全缺省
Date::Date(int year, int month, int day)
:_year(year)
, _month(month)
,_day(day)
{
if (year >= 0 && month > 0 && month < 13 && day>0 && day <= Getmonthday(year, month))
{
_year = year;
_month = month;
_day = day;
}else{
cout << "非法日期" << endl;
}
}
//析構函數
Date::~Date(){}
//運算符>重載
bool operator>(const Date& d1,const Date& d2){
if (d1._year > d2._year){
return true;
}
else if (d1._year == d2._year&&d1._month > d2._month){
return true;
}
else if (d1._year == d2._year&&d1._month == d2._month&&d1._day > d2._day){
return true;
}
return false;
}
//運算符==重載
bool operator==(const Date& d1, const Date& d2) {
if (d1._year == d2._year && d1._month == d2._month&&d1._day == d2._day){
return true;
}
return false;
}
//運算符!=重載
bool operator!=(const Date& d1, const Date& d2) {
return !(d1 == d2);
}
//運算符>=重載
bool operator>=(const Date& d1, const Date& d2){
if (d1 > d2){
return true;
}
else if (d1 == d2){
return true;
}
return false;
}
//運算<重載
bool operator<(const Date& d1, const Date& d2) {
if (d1>= d2){
return false;
}
return true;
}
//運算<=重載
bool operator<=(const Date& d1, const Date& d2) {
if (d1 > d2){
return false;
}
return true;
}
////賦值運算符號 =
/* Date& operator=( Date& d1, const Date& d2) {
if (&d1 != &d2){
d1._year = d2._year;
d1._month = d2._month;
d1._day = d2._day;
}
return d1;
}*/
//日期-天數
Date& operator-=(Date& d1,int day){
if (day < 0){
return d1 += -day;
}
d1._day -= day;
while (d1._day <= 0){
--d1._month;
if (d1._month == 0){
d1._month = 12;
--d1._year;
}
d1._day += Getmonthday(d1._year, d1._month);
}
return d1;
}
Date& operator-( Date& d1,int day){
d1 -= day;
return d1;
}
//日期+=天數
Date& operator+=(Date& d1, int day) {
if (day < 0) {
return d1 -= -day;
}
d1._day += day;
while (d1._day > Getmonthday(d1._year, d1._month)) {
++d1._month;
if (d1._month > 12) {
++d1._year;
d1._month = 1;
}
d1._day -= Getmonthday(d1._year, d1._month);
}
return d1;
}
//日期+天數
Date& operator+(Date& d1, int day) {
d1 += day;
return d1;
}
//前置++
Date& operator++(Date& d1){
d1 += 1;
return d1;
}
//後置++
Date operator++(Date& d1,int) {
Date d(d1);
d1 += 1;
return d;
}
//前置--
Date& operator--(Date& d1) {
d1 -= 1;
return d1;
}
//後置--
Date operator--(Date& d1, int){
Date d(d1);
d1 -= 1;
return d;
}
//日期-日期
int operator-(const Date& d1,const Date& d2){
Date max = d1;
Date min = d2;
int flag = 1;
int num = 0;
if (d1 < d2){
max = d2;
min = d1;
flag = -1;
}
while (min != max){
++num;
++min;
}
return flag == 1 ? num : -num;
}
//重載<<
ostream& operator<<(ostream& _out, const Date& d) {
_out << d._year << "-" << d._month << "-" << d._day << endl;
return _out;
}
//重載>>
istream& operator>>(istream& _in, Date& d) {
_in >> d._year >> d._month >> d._day;
return _in;
}
void test()
{
Date d1(1900,1,1);
Date d2(2020,1,1);
//d1 = d2;
//cout << (d1 == d2) << endl;
//cout << (d1 != d2) << endl;
//cout << (d1 >= d2) << endl;
//cout << (d1 <= d2) << endl;
//cout << (d1 > d2) << endl;
//cout << (d1 < d2) << endl;
//d1 + 1;
//d1 + -1;
//d1 - 1;
//d1 += -1;
//d1 += 1;
//d1 -1;
//d1 -= 1;
//d1 -= -1;
//cout<<d1++;
//cout <<++d1;
//cout<<d1--;
//cout <<--d1;
//cin >> d1;
//cout << d1;
//cout << (d2 - d1) << endl;
//cout << (d1 - d2) << endl;
}
test.cpp
#include"Date.h"
void test();
int main(){
test();
system("pause");
return 0;
}
3.心得體會
通過日期類的實現,我熟悉了運算符重載,友元類,構造函數等知識點,對類的封裝性也有了深刻的理解。