最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++類和對象實戰(zhàn)之Date類的實現(xiàn)方法

 更新時間:2021年12月12日 12:41:10   作者:可口也可樂、  
C++ 標準庫沒有提供所謂的日期類型,C++ 繼承了C語言用于日期和時間操作的結構和函數(shù),這篇文章主要給大家介紹了C++類和對象實戰(zhàn)之Date類的實現(xiàn)方法,需要的朋友可以參考下

零、前言

在學了C++類和對象基本知識以及六個默認成員函數(shù)后,我們可以上手實現(xiàn)一個Date類出來,檢驗學習的效果。

一、Date類相關接口

接口展示:

class Date
{ 
	//輸出操作符重載
	friend ostream& operator<<(ostream& _cout, const Date& d);
	//輸出操作符重載
	friend istream& operator>>(istream& _cin, Date& d);

public:
    // 獲取某年某月的天數(shù)
    int GetMonthDay(int year, int month);

    // 全缺省的構造函數(shù)
    Date(int year=1988, int month=1, int day=1);

    // 拷貝構造函數(shù)
    Date(const Date& d);

    // 賦值運算符重載
    Date& operator=(const Date& d);

    // 日期+=天數(shù)
    Date& operator+=(int day);

    // 日期+天數(shù)
    Date operator+(int day);

    // 日期-天數(shù)
    Date operator-(int day);

    // 日期-=天數(shù)
    Date& operator-=(int day);

    // 前置++
    Date& operator++();

    // 后置++
    Date& operator++(int);

    // 后置--
    Date& operator--(int);

    // 前置--
    Date& operator--();

    // >運算符重載
    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);

    // 日期-日期 返回兩個日期之間相隔的具體天數(shù)
    int operator-(const Date& d);

    //日期展示
    void print()
    {
        cout << _year << " " << _month << " " << _day << endl;  
    }
private:
    int _year;
    int _month;
    int _day;
};

二、具體接口函數(shù)實現(xiàn)

注意:

因為對于定義在類里面的函數(shù)會自動設成內聯(lián)函數(shù),而只有一些簡單的函數(shù)才建議設成內聯(lián)函數(shù),所以實現(xiàn)函數(shù)時我們是聲明和定義分離(在類里面聲明,類外定義)

在類外實現(xiàn)函數(shù)接口需要加上類域名稱

1、獲取月份天數(shù)

注意:

閏年二月與平年二月的天數(shù)不同

實現(xiàn)代碼:

//獲取月份天數(shù)
int Date::GetMonthDay(int year, int month)
{
	//設置平年月天數(shù)數(shù)組
	static int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//設置成靜態(tài)避免重復創(chuàng)建
	int day = monthdays[month];
	//對閏年二月的處理
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
	{
		day = 29;
	}
	return day;
}

2、Date打印

注:打印函數(shù)比較簡單,設成內聯(lián)函數(shù)很適合,可以直接在類里定義

實現(xiàn)代碼:

void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

3、Date構造函數(shù)

注意:

對于構造函數(shù)建議寫成全缺省函數(shù)(便于無參數(shù)初始化),但是只能定義和聲明其中一個寫缺省

考慮初始化的日期是否合理

實現(xiàn)代碼:

//構造函數(shù)
//類里聲明
Date(int year = 0, int month = 1, int day = 1);
//定義
Date::Date(int year, int month, int day)
{
	// 檢查日期的合法性
	if (year >= 0
		&& month > 0 && month < 13
		&& day > 0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		// 嚴格來說拋異常更好
		cout << "非法日期" << endl;
		cout << year << "年" << month << "月" << day << "日" << endl;
		exit(-1);
	}
}

4、Date析構函數(shù)

注:對于像Date一樣的類來說,析構函數(shù)(沒有需要清理的空間資源),拷貝函數(shù)和賦值重載函數(shù)(能夠完成成員變量淺拷貝)都不用自己寫,編譯器默認生成的已經足夠使用

實現(xiàn)代碼:

//析構函數(shù)
Date::~Date()
{
	_year = 1;
	_month = 0;
	_day = 0;
}

5、Date拷貝構造函數(shù)

實現(xiàn)代碼:

//拷貝構造
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day= d._day;
}

6、Date賦值重載函數(shù)

注意:

對于賦值操作符來說,是需要能支持連續(xù)賦值的操作,這里我們返回Date本身來進行接下來的繼續(xù)賦值

實現(xiàn)代碼:

//賦值運算符重載
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}

效果圖:

7、Date+=天數(shù)

注意:

  1. +=表示會修改Date本身的數(shù)據
  2. 處理傳入負數(shù)天數(shù)
  3. 處理好天數(shù)進位,月份進位

實現(xiàn)代碼:

//日期+=天數(shù)
Date& Date::operator+=(int day)
{
	if (day < 0)//處理特殊情況
	{
		*this -= -day;//復用Date-=天數(shù)
	}
	else
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))//處理數(shù)據合理性
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
   			_year++;
				_month = 1;
			}
		}
	}
	return *this;//返回引用,即對象本身
}

8、Date+天數(shù)

注意:

+天數(shù)表示不會修改Date本身的數(shù)據(使用const修飾,避免修改)

邏輯與Date+=天數(shù)基本一致,可以進行復用

實現(xiàn)代碼:

Date Date::operator+(int day) const
{
	Date tmp = *this;//賦值重載
	tmp += day;//復用+=重載
	return tmp;//返回值(拷貝構造)
}

9、Date-=天數(shù)

注意:

  1. +=表示會修改Date本身的數(shù)據
  2. 處理傳入負數(shù)天數(shù)
  3. 考慮日期的借位,月份的借位

實現(xiàn)代碼:

//日期-=天數(shù)
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;//復用Date+=天數(shù)
	}
	else
	{
		_day -= day;
		while (_day <= 0)//處理數(shù)據合理性
		{
			_month--;
			if (_month <= 0)
			{
				_year--;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
	}
	return *this;
}

10、Date-天數(shù)

注意:

  1. -天數(shù)不會修改Date本身的數(shù)據(使用const修飾,避免修改)
  2. 邏輯與Date-=天數(shù)基本一致,可以進行復用

實現(xiàn)代碼:

Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

11、++Date

注意:

前置++表示,Date先增后使用

實現(xiàn)代碼:

//++Date
Date& Date::operator++()
{
	*this += 1;//復用Date+=天數(shù)
	return *this;
}

12、Date++

注意:

語法規(guī)定,因為與前置命名相同的緣故,這里的后置函數(shù)多一個參數(shù)來與前置函數(shù)形成重載

后置++表示先使用后自增

實現(xiàn)代碼:

//Date++
Date Date::operator++(int)
{
	Date tmp = *this;//保存一份日期
	*this += 1;//自增當前日期
	return tmp;//返回自增前的日期
}

13、–Date

實現(xiàn)代碼:

//--Date
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

14、Date–

實現(xiàn)代碼:

//Date--
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

15、日期比較

注:可以多次復用

實現(xiàn)代碼:

//日期比較
bool Date::operator>(const Date& d) const
{
	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;
}

bool Date::operator==(const Date& d) const
{
	return _year == d._year && _month == d._month && _day == d._day;
}

bool Date::operator<(const Date& d) const
{
	return !(*this >= d);
}

bool Date::operator>=(const Date& d) const
{
	return *this > d || *this == d;
}

bool Date::operator<=(const Date& d) const
{
	return !(*this > d);
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

16、Date相減

實現(xiàn)代碼:

 //日期減日期
 int Date::operator-(const Date& d) const
 {
 	//確定日期的大小
 	Date max = *this;
 	Date min = d;
 	if (*this < d)//復用日期比較
 	{
 		max = d;
 		min = *this;
 	}
 	int day = 0;
 	while (max != min)
 	{
 		++min;
 		++day;
 	}
 	return day;
 }

17、日期輸入\日期輸出

注意:

  1. 對于輸入操作符,我們習慣是cin>>date,而這樣的用法表示做操作數(shù)是cin,右操作數(shù)為日期對象,但是對于類成員函數(shù)來說,存在著隱含參數(shù)this指針(占據和第一個參數(shù)位置,即日期對象是左操作數(shù))
  2. 雖然定義成類外函數(shù)能修改參數(shù)位置,但是無法訪問類里的私有成員變量,這里我們使用友元函數(shù)來解決,即在類里聲明函數(shù)前加上friend,便可以訪問成員

實現(xiàn)代碼:

//輸出操作符重載
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "年" << d._month << "月" << d._day << "日" ;
	return _cout;
}
//輸出操作符重載
istream& operator>>(istream& _cin, Date& d)
> {
	_cin >> d._year >> d._month >> d._day;
	return _cin;
}

效果圖:

  1. date,而這樣的用法表示做操作數(shù)是cin,右操作數(shù)為日期對象,但是對于類成員函數(shù)來說,存在著隱含參數(shù)this指針(占據和第一個參數(shù)位置,即日期對象是左操作數(shù))
  2. 雖然定義成類外函數(shù)能修改參數(shù)位置,但是無法訪問類里的私有成員變量,這里我們使用友元函數(shù)來解決,即在類里聲明函數(shù)前加上friend,便可以訪問成員

實現(xiàn)代碼:

//輸出操作符重載
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "年" << d._month << "月" << d._day << "日" ;
	return _cout;
}
//輸出操作符重載
istream& operator>>(istream& _cin, Date& d)
{
	_cin >> d._year >> d._month >> d._day;
	return _cin;
}

效果圖:

總結

到此這篇關于C++類和對象實戰(zhàn)之Date類實現(xiàn)的文章就介紹到這了,更多相關C++?Date類的實現(xiàn)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C++設計模式之備忘錄模式

    C++設計模式之備忘錄模式

    這篇文章主要介紹了C++設計模式之備忘錄模式,本文講解了什么是備忘錄模式、備忘錄模式的UML類圖、備忘錄模式的使用場合等內容,需要的朋友可以參考下
    2014-10-10
  • 淺談C++11中的幾種鎖

    淺談C++11中的幾種鎖

    本文主要介紹了C++11中的幾種鎖,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 利用Matlab一鍵生成工地海報特效

    利用Matlab一鍵生成工地海報特效

    這篇文章主要介紹了如何利用Matlab制作出工地海報的特效,文中的示例代碼講解詳細,對我們學習Matlab有一定幫助,需要的可以參考一下
    2022-03-03
  • C++類與對象深入之引用與內聯(lián)函數(shù)與auto關鍵字及for循環(huán)詳解

    C++類與對象深入之引用與內聯(lián)函數(shù)與auto關鍵字及for循環(huán)詳解

    朋友們好,這篇播客我們繼續(xù)C++的初階學習,現(xiàn)在對一些C++的入門知識做了些總結,整理出來一篇博客供我們一起復習和學習,如果文章中有理解不當?shù)牡胤?還希望朋友們在評論區(qū)指出,我們相互學習,共同進步
    2022-06-06
  • C中實現(xiàn)矩陣乘法的一種高效的方法

    C中實現(xiàn)矩陣乘法的一種高效的方法

    本篇文章介紹了,在C中實現(xiàn)矩陣乘法的一種高效的方法。需要的朋友參考下
    2013-05-05
  • 詳解C++編程中的vector類容器用法

    詳解C++編程中的vector類容器用法

    vector是一個標準庫中的容器,使用時需要包含#include <vector>頭文件,也可以說vector是一個類模板而不是一種數(shù)據類型,對它的定義,需要指定類型,需要的朋友可以參考下
    2016-05-05
  • 淺談C++11新引入的lambda表達式

    淺談C++11新引入的lambda表達式

    Lambda表達式(又稱Lambda函數(shù),英文原文是Lambda Expression),是C++11的新特性中非常實用的一個。
    2017-07-07
  • C++使用htslib庫讀入和寫出bam文件的實例

    C++使用htslib庫讀入和寫出bam文件的實例

    下面小編就為大家分享一篇C++使用htslib庫讀入和寫出bam文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • 詳細談談C語言中動態(tài)內存

    詳細談談C語言中動態(tài)內存

    在C語言中,編寫程序的時候不能確定內存的大小,希望程序在運行的過程中根據數(shù)據量的大小動態(tài)的分配內存,這篇文章主要給大家介紹了關于C語言中動態(tài)內存的相關資料,需要的朋友可以參考下
    2022-03-03
  • C++實現(xiàn)迷宮算法實例解析

    C++實現(xiàn)迷宮算法實例解析

    這篇文章主要介紹了C++實現(xiàn)迷宮算法實例解析,是一個比較經典的C++算法,有一定的學習與借鑒價值,需要的朋友可以參考下
    2014-07-07

最新評論

望江县| 会泽县| 两当县| 锡林浩特市| 长寿区| 宁国市| 英超| 柯坪县| 滕州市| 红河县| 宝山区| 清新县| 东乌珠穆沁旗| 潮州市| 新建县| 东至县| 邵阳市| 即墨市| 汾阳市| 玛多县| 巴东县| 阜宁县| 年辖:市辖区| 土默特右旗| 商都县| 长武县| 治县。| 营山县| 丹东市| 常宁市| 通渭县| 治多县| 隆林| 洪泽县| 杭锦旗| 凌源市| 长阳| 惠来县| 华容县| 海口市| 大竹县|