日期類的封裝

2021-09-29 07:00:19 字數 1939 閱讀 2526

#include

using

namespace std;

class

date

}//拷貝建構函式

date

(const date& d)

//析構函式

~date()

//判斷是不是閏年,四年一閏,百年不閏,四百年一閏

bool

isleapyear

(int year)

else

}//判斷是不是合法日期,小於當前年月的日期或大於當前年月的日期

bool

isinvaliddate

(int year,

int month,

int day)

else

}//判斷當前月份有多少天,陣列小標從零開始,但月份沒有為零的,

//則定義13個月,下標為零的值為0

intyearsofmonth

(int year,

int month)

; day = days[month];if

(month ==2&&

isleapyear

(year)

)return day;

}//修正日期

date tocorrect

(date& d)

else

}else

else}}

return d;

}//過載賦值運算子

date&

operator=(

const date& d)

return

*this;}

//兩個物件相差多少天

intoperator

-( date& d)

int n =0;

while

(min < max)

return n*flag;

}//給乙個日期加上days是什麼日期

date operator+(

int days)

//給乙個日期減去days

date operator-(

int days)

//前置++

date&

operator++(

)//後置++

date operator++(

int)

//前置與後置的區別通過返回值和傳參來區別

//前置--

date&

operator--(

)//後置--

date operator--(

int)

//區別與++一樣,在返回值和引數

//日期比大小,大於

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)

//日期比大小,小於等於

bool

operator

<=

(const date& d)

void

display()

private

:int _year;

int _month;

int _day;};

intmain()

類對日期date的封裝

include stdafx.h include using namespace std include date.h using namespace space int tmain int argc,tchar argv pragma once 與 ifndef date h define dat...

時間與日期類封裝

在ios開發時,會經常用到一些處理時間的函式和方法,比如求當前時間,計算兩個日期差幾天,格式化現在時間與給定的秒數的時間差等。所以就整理並歸納了一些比較實用的方法。主要用到的類有 nsdate,nsdateformatter,nscalendar,nsdatecomponents,nstimeint...

3 2封裝的日期類

public class mydate 共有類,與原始檔同名 public mydate int year,int month,int day 建構函式,指定日期 public mydate 無參構造方法。預設指定日期 public mydate mydate d 過載函式 public void ...