---
----
----
----
----
----
----
----
----
----
----
----
----
----
----
----
----
----
---/*
**功能:實現日期的簡單操作
****
**基本的成員函式:
**建構函式,拷貝建構函式,析構函式,賦值運算子過載,操作符過載(兩個日期間比較大小)
****日期類功能函式:
**1:計算乙個日期加上多少天數後的日期
**2:把該日期改為加上指定數目的天數後的日期
**3:乙個日期減上多少天數後的日期
**4:把該日期改為減去指定數目的天數後的日期
**5:該日期加1(前置++
)(後置++
)**6:該日期減1(前置--
)(後置--
)**7:計算某日期到未來某日期間隔的天數
****
**by
:niuxiaoke
***///-
----
----
----
----
----
----
----
----
----
----
----
----
----
----
----
----
----
----
--
#include
using
namespace
std;
class date
}date(const date& d)//拷貝構造
}~date()//析構函式
{}int getmonthday(int year,int month)
;if((year%4 == 0 && year%100!=0) || (year%400 == 0))
return days[month];
}bool isinvaliddate(int year,int month,int day)
void display()
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)
date& operator=(const date& d)
return *this;
}//d1 + 100
date operator+(int day)
date tmp = *this;
if(isinvaliddate(tmp._year,tmp._month,tmp._day+day))
int sumday = tmp._day + day;
while(sumday > getmonthday(tmp._year,tmp._month))
else
}return tmp;
}date& operator+=(int day)
date operator-(int day)
date tmp = *this;
while(day >= tmp._day)
else
tmp._day = getmonthday(tmp._year,tmp._month);
}tmp._day = tmp._day - day;
return tmp;
}date& operator-=(int day)
date& operator++() //前置加加
}return *this;
}date operator++(int) //後置加加
date& operator--() //前置減減
else
else
}return *this;
}date operator--(int)
intoperator-( date& d) //計算兩個日期之間的天數
else
if(_year == d._year && _month < d._month)
else
if(_year == d._year && _month == d._month && _day < d._day)
date tmp1(*this);
date tmp2(d);
int ret = 0;
while(tmp1 != tmp2)
return ret;
}private:
int _year;
int _month;
int _day;
};void test1()
C 實現日期類
date 日期 類 練習實現類的成員函式。運算子的過載 date.h include class date date int year,int month,int day 全預設建構函式 date const date d 拷貝構造 date operator const date d 賦值運算子的...
C 實現日期類
預設成員函式的應用場景 date date1 當我們例項化乙個物件date1時,就會呼叫 建構函式,所以我們可以使用建構函式對物件進行初始化。date date2 date1 當我們例項化物件date2的同時將乙個已存在的物件的內容拷貝過來,就會呼叫拷貝建構函式。date date3 date3 d...
借助c 實現日期類
本篇部落格借用c 簡單實現乙個日期類 其中包括日期計算器,日期比較大小。接下來我將注釋寫到 中方便理解。date.h pragma once class date 拷貝建構函式 date const date d return this 析構函式 date 列印日期 void show 判斷是否為閏...