C++類與對(duì)象之日期類的實(shí)現(xiàn)
1. 實(shí)現(xiàn)前的鋪墊
在實(shí)現(xiàn)前,我們要先把類寫好,類中包含成員函數(shù)和成員變量。對(duì)于日期類來說,拷貝構(gòu)造和賦值運(yùn)算符可以不寫,但是我在類中寫了,也沒關(guān)系哦。
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
//構(gòu)造函數(shù)
Date(int year = 0, int month = 1, int day = 1)
{
if (year >= 0 && month > 0 && month<13 && day>0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "輸入日期不和法" << endl;
}
}
//拷貝構(gòu)造
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//賦值運(yùn)算符
Date& operator=(const Date&d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//判斷合法性
int GetMonthDay(int year, int month);
//輸出日期
void prit();
//日期+=
Date& operator+=(int day);
//日期+
Date operator+(int day);
//日期-=
Date& operator-=(int day);
//日期-
Date operator-(int day);
//日期==
bool operator==(const Date& d);
//日期>
bool operator>(const Date& d);
//日期!=
bool operator != (const Date& d);
//<
bool operator < (const Date& d);
//>=
inline bool operator >= (const Date& d);
//<=
bool operator <= (const Date& d);
//++d
Date& operator++();
//d++
Date& operator++(int);
//--d
Date& operator--();
//d--
Date& operator--(int);
//日期-日期
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
//給出當(dāng)年當(dāng)月的天數(shù)
inline int Date::GetMonthDay(int year, int month)
{
static int dayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
dayArray[2] = 28;
if (month == 2 && ((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0)
{
dayArray[2] = 29;
}
return dayArray[month];
}
2. 日期類的實(shí)現(xiàn)
在實(shí)現(xiàn)前我們先要利用復(fù)用,復(fù)用就是把已經(jīng)寫好的函數(shù)來完成要完成的函數(shù)。
2.1 日期+=天數(shù)
在這里,就復(fù)用了(日期-=天數(shù)的)函數(shù)
對(duì)于+=,當(dāng)加一個(gè)天數(shù)是為正的時(shí)候(如:100),
但當(dāng)加的天數(shù)為負(fù)數(shù)時(shí)(如:-100)
這里就要先判斷day是否為正負(fù)數(shù)
如果為正數(shù)就正常加,如果是負(fù)數(shù)就復(fù)用-=操作符重載函數(shù)
詳細(xì)請(qǐng)看下面:
Date& Date::operator+=(int day)
{
if (day > 0)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
}
else
{
*this -= (-day);
}
return *this;
}
2.2 日期-=天數(shù)
這里也和+=操作符重載函數(shù)一樣,都要判斷day是否為正負(fù)值
Date& Date::operator-=(int day)
{
if (day > 0)
{
_day -= day;
while (_day <= 0)
{
_month--;
_day += GetMonthDay(_year, _month);
if (_month == 0)
{
_year--;
_month = 12;
}
}
}
else
{
*this += (-day);
}
return *this;
}
2.3 日期-天數(shù)
減天數(shù),自己沒變,所以要?jiǎng)?chuàng)個(gè)臨時(shí)變量。
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return *ret;
}
2.4 日期+天數(shù)
與減天數(shù)同理
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
2.5 前置++
對(duì)于前置和后置,在函數(shù)命名的時(shí)候,后置的參數(shù)列表多一個(gè)int來占位。
Date& Date::operator++()
{
*this += 1;
return *this;
}
2.6 后置++
Date& Date::operator++(int)
{
Date tmp(*this);
tmp += 1;
return *this;
}
2.7 前置–
Date& Date::operator--()
{
*this -= 1;
return *this;
}
2.8 后置–
Date& Date::operator--(int)
{
Date tmp(*this);
tmp -= 1;
return *this;
2.9 >運(yùn)算符重載
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
2.10 ==運(yùn)算符重載
bool Date::operator==(const Date& d)
{
return _year == d._year
&&_month == d._month
&&_day == d._day;
}
2.11 >=運(yùn)算符重載
這里就開始復(fù)用>和==的操作符重載函數(shù)
bool Date::operator >= (const Date& d)
{
return (*this > d) || (*this == d);
}
2.12 <運(yùn)算符重載
bool Date::operator < (const Date& d)
{
return !(*this>d);
}
2.13 <=運(yùn)算符重載
bool Date::operator <= (const Date& d)
{
return (*this < d) || (*this == d);
}
2.14 !=運(yùn)算符重載
bool Date::operator != (const Date& d)
{
return !(*this == d );
}
2.15 日期-日期 返回天數(shù)
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (max < min)
{
max = d;
min = *this;
flag = -1;
}
int m = 0;
while (max!=min)
{
++min;
++m;
}
return m*flag;
}
2.16 輸出
void Date::prit()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
到此這篇關(guān)于C++類與對(duì)象之日期類的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C++日期類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)與算法之圖的遍歷(一)
這篇文章主要是介紹了利用深度優(yōu)先算法實(shí)現(xiàn)圖的遍歷,文中利用圖文詳細(xì)的介紹了實(shí)現(xiàn)步驟,對(duì)我們學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)與算法有一定的幫助,需要的朋友可以參考一下2021-12-12
使用root權(quán)限運(yùn)行自己所編譯程序的解決方法
本篇文章介紹了,使用root權(quán)限運(yùn)行自己所編譯程序的解決方法。需要的朋友參考下2013-05-05
掌握C++:揭秘寫時(shí)拷貝與淺深拷貝之間的關(guān)系
探索C++的奧秘,本指南將揭秘寫時(shí)拷貝與淺深拷貝之間的微妙關(guān)系,摸索這些復(fù)雜概念背后的邏輯,讓你的編程技能瞬間提升,來吧,讓我們一起進(jìn)入這個(gè)引人入勝的C++世界!2024-01-01
C語言實(shí)現(xiàn)將彩色bmp圖像轉(zhuǎn)化為灰圖、灰度圖像反色
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)將彩色bmp圖像轉(zhuǎn)化為灰圖、灰度圖像反色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
C語言將數(shù)組中元素的數(shù)排序輸出的相關(guān)問題解決
這篇文章主要介紹了C語言將數(shù)組中元素的數(shù)排序輸出的相關(guān)問題解決,文中的題目是將元素連接起來排成一個(gè)數(shù)并要求出這類結(jié)果中數(shù)最小的一個(gè),需要的朋友可以參考下2016-03-03
解析sizeof, strlen, 指針以及數(shù)組作為函數(shù)參數(shù)的應(yīng)用
本篇文章是對(duì)sizeof, strlen, 指針以及數(shù)組作為函數(shù)參數(shù)的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

