上篇博客簡單的介紹了以下c++的幾個默認成員函數,現在我們爲了更好的理解這幾個成員函數,實現一個簡單的日期類。 日期類所用到的指知識點:
1、構造函數
2、拷貝構造函數
3、賦值運算符的重載
4、函數的複用 實現的功能:
1)、在當前基礎下加一天,減一天後的日期
2)、在當前基礎下加多天,減多天後的日期
3)、當前日期與要計算的日期相差多少天
代碼實現:
Date.h*
#pragma once
#include<stdlib.h>
#include<assert.h>
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (month<1||month>12||day<0||day>GetMonthDay(_year,_month))
{
assert(false);
}
}
Date& operator=(const Date& d);
bool IsInvalid();
bool isLeapYear(int year);
int GetMonthDay(int year, int month);
void Show();
bool operator==(const Date& d);//判斷相等
bool operator!=(const Date& d);//判斷不等
bool operator>=(const Date& d);//大於等於
bool operator<=(const Date& d);//小於等於
bool operator>(const Date& d);//大於
// d1 < d2
bool operator<(const Date& d);//小於
// d1 + 10
Date operator+(int day);
Date& operator+=(int day);
Date operator-(int day);
Date& operator-=(int day);
int operator-(const Date& d);
//++d1
Date& operator++();// 前置
//d1++
Date operator++(int); // 後置
Date& operator--();//前置
Date operator--(int);//後置
private:
int _year;
int _month;
int _day;
};
Date.c
#include"Date.h"
bool Date:: operator==(const Date& d)//判斷相等
{
if((_year == d._year)
&& (_month ==d. _month)
&& (_day == d._day))
{
return true;
}
return false;
}
bool Date:: operator<(const Date&d)//判斷小於
{
if ((d._year<_year)
||(d._year==_year&&d._month<_month)
|| (d._year==_year&&d._month==_month&&d._day<_day))
{
return false;
}
return true;
}
//d1!=d2
bool Date::operator!=(const Date& d)//不等調用等的複用
{
return !(*this == d);
}
bool Date:: operator>=(const Date& d)//大於等於調小於的複用
{
return !(*this<d);
}
bool Date::operator<=(const Date& d)//小於等於調用小於和等於的複用
{
return (*this<d || *this == d);
}
bool Date::operator>(const Date& d)//大於調用小於等於的複用
{
return !(*this <= d);
}
bool Date:: isLeapYear(int year)//判斷是否爲閏年
{
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
{
return true;
}
return false;
}
int Date::GetMonthDay(int year, int month)//獲得這一天的天數
{
int monthdays[13] = { 0, 31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31 };
if (month==2&&isLeapYear(year))
{
monthdays[2]= 29;
}
int day = monthdays[month];
return day;
}
void Date::Show()//輸出函數
{
cout<< _year << "-"<< _month << "-" << _day<<endl;
}
Date Date:: operator+(int day)//日期加
{
if (day<0)
{
return *this-(-day);
}
Date ret (*this);
ret._day += day;
while (ret._day>GetMonthDay(ret._year,ret._month))
{
ret._day -= GetMonthDay(ret._year, ret._month);
ret._month++;
if (ret._month==13)
{
ret._year++;
ret._month = 1;
}
}
return ret;
}
Date& Date:: operator+=(int day)//日期的加等
{
*this = *this + day;
return *this ;
}
Date Date::operator-(int day)//日期的減
{
if (day<0)
{
return *this + (-day);
}
Date ret(*this);
ret._day -= day;
while (ret._day<=0)
{
ret._month--;
if (ret._month==0)
{
ret._year--;
ret._month = 12;
}
ret._day += GetMonthDay(ret._year, ret._month);
}
return ret;
}
Date& Date:: operator-=(int day)//日期的減等
{
return (*this -= day);
}
Date& Date::operator=(const Date& d)//賦值運算符的重載
{
if ((*this)!= d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return (*this);
}
int Date::operator-(const Date& d)//日期相差多少天的計算
{
Date max = *this; Date min = d;
int flag = 1;
if ((*this)<d)
{
max = d;
min = *this;
flag = -1;
}
int day = 0;
while (min<=max)
{
min++;
day++;
}
return day*flag;
}
Date& Date:: operator++()//日期的自增(前置)
{
*this = *this + 1;
return *this;
}
Date Date:: operator++(int)//日期的自加(後置)
{
Date tmp(*this);
*this=*this+1;
return tmp;
}
Date& Date::operator--()//日期的自減(前置)
{
*this = *this-1;
return *this;
}
Date Date::operator--(int)//日期的自減(後置)
{
Date tmp(*this);
*this = *this - 1;
return(tmp);
}
test.cpp
#include"Date.h"
int main()
{
Date d1(2018,5,3);
//d1.Show();
/*Date d2(2018,4,5);
Date d3(2018 ,3,2);
d2.Show();
(d1 + 100).Show();
(d1 +(- 4)).Show();
(d1 - 100).Show();
(d1).Show();
--d1;
d1.Show();
(++d1).Show();
(d1++).Show();
d1=d2=d3;*/
Date d2(2018, 3, 5);
d2.Show();
cout << (d1 - d2) << endl;
system("pause");
return 0;
}
可以去這個在線日期計算器進行驗證;
http://beijing-time.org/riqi.htm