欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++無痛實(shí)現(xiàn)日期類的示例代碼

 更新時(shí)間:2022年10月18日 10:59:12   作者:梨+蘋  
凡是要寫類必須要提到六大默認(rèn)成員(六位大爺):構(gòu)造函數(shù)、析構(gòu)函數(shù)、拷貝構(gòu)造函數(shù)、賦值重載函數(shù)、取地址重載函數(shù)(包括const對(duì)象和普通對(duì)象);那么這次的日期類又需要伺候哪幾位大爺呢?本文就來詳細(xì)說說

日期類的實(shí)現(xiàn)

凡是要寫類必須要提到六大默認(rèn)成員(六位大爺):構(gòu)造函數(shù)、析構(gòu)函數(shù)、拷貝構(gòu)造函數(shù)、賦值重載函數(shù)、取地址重載函數(shù)(包括const對(duì)象和普通對(duì)象);那么這次的日期類又需要伺候哪幾位大爺呢?

日期類的實(shí)現(xiàn)中函數(shù)與函數(shù)之間有較強(qiáng)的耦合性,所以實(shí)現(xiàn)的邏輯順序一定要把握好,不然會(huì)暈頭轉(zhuǎn)向的?。?! 下面是我的實(shí)現(xiàn)順序:

構(gòu)造函數(shù)

Date(const Date& d)//拷貝構(gòu)造函數(shù)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        
    }

析構(gòu)函數(shù)

~Date()//析構(gòu)函數(shù)
    {
        _year = 1;
        _month = 1;
        _day = 1;
    }

拷貝構(gòu)造函數(shù)

    Date(const Date& d)//拷貝構(gòu)造函數(shù)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        
    }

打印函數(shù)

    void Print()//打印函數(shù)
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

這里我們還需要寫一個(gè)獲取月份對(duì)應(yīng)天數(shù)的函數(shù)

獲取天數(shù)函數(shù)

    int GetTrueDay(int year, int month)//得到正確月份天數(shù)
    {
        static int monthday[] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
        if (month == 2 && ((year % 4 == 0 && year % 100 != 0)
        || (year % 400 == 0)))
        {
            return 29;
        }
        else
        {
            return monthday[month];
        }
    }

這里就是大體框架了,接下來是各個(gè)細(xì)節(jié)部分

運(yùn)算符重載區(qū)

判斷兩個(gè)日期是否相等(*this==d)

bool operator==(const Date& d) const;//等于
//---相等為真(返回1);不相同為假(返回0)
bool Date:: operator==(const Date& d) const//等于
{//年相等才判斷到月,月相等才判斷到年
    return _year == d._year
        && _month == d._month
        && _day == d._day;
}

判斷前一個(gè)日期是否大于后一個(gè)日期(*this>d)

bool operator>(const Date& d) const;//大于
//---相等為真(返回1);不相同為假(返回0)
bool Date:: operator>(const Date& d) const//大于
{//這里一樣的判斷順序依次是年---月---日
    if (_year > d._year)
    {
        return true;
    }
    else if (_month > d._month)
    {
        return true;
    }
    else if (_day == d._day)
    {
        return true;
    }
    else
    {
        return false;
    }
//大于
}

判斷前一個(gè)日期是否大于等于后一個(gè)日期(*this>=d)

這里直接重載?。?!

bool operator>=(const Date& d) const//大于等于
    {
        return *this > d || *this == d;
    }

判斷前一個(gè)日期是否小于后一個(gè)日期(*this<d)

這里直接重載?。?!

    bool operator<(const Date& d)const //判斷小于
    {
        return !(*this >= d);
    }

判斷前一個(gè)日期是否小于等于后一個(gè)日期(*this<=d)

這里直接重載?。。?/p>

    bool operator<=(const Date& d)const//小于等于
        {
            return !(*this > d);
        }

賦值重載

前一個(gè)日期等于后一個(gè)日期(*this=d)—可以連續(xù)賦值

    Date& operator=(const Date& d)//賦值重載
        {
            if (this!=&d)
            {
                _year = d._year;
                _month = d._month;
                _day = d._day;
            }
            else
            {
                return*this;
            }
        }

對(duì)日期減天數(shù)-不影響自身-用拷貝構(gòu)造

    Date operator-(int day) const;//減天數(shù)
Date Date:: operator-(int day) const//減天數(shù)-不影響本身-不用引用-用拷貝構(gòu)造函數(shù)
{
    Date tmp(*this);
    tmp-= day;
    return tmp;
}

對(duì)日期加天數(shù)-不影響自身-用拷貝構(gòu)造

Date operator+(int day) const;//加天數(shù)
Date Date:: operator+(int day) const//加天數(shù)-不影響本身-不用引用-用拷貝構(gòu)造函數(shù)
{
    Date tmp(*this);
    tmp+= day;
    return tmp;
}

日期減等天數(shù)-影響自身-用引用

Date& operator-=(int day) ;//減等天數(shù)
Date& Date:: operator-=(int day) //減等天數(shù)- 影響本身-用引用-不加const
{
    if (day < 0)
    {
        return *this += abs(day);
    }
    _day -= day;
    while (_day<=0)
    {
        --_month;
        if (_month == 0)
        {
            --_year;
            _month = 12;
        }
        _day += GetTrueDay(_year, _month);
    }
    return *this;
    
}

日期加等天數(shù)-影響自身-用引用

Date& operator+=(int day);//加天數(shù)
Date& Date:: operator+=(int day) //加天數(shù)- 影響本身-用引用-不加const
{
    if (day < 0)
    {
        return *this -= abs(day);
    }
    _day += day;
    while (_day > GetTrueDay(_year, _month))
    {
        _day -= GetTrueDay(_year,_month);
        _month++;
        if (_month == 13)
        {
            _year++;
            _month = 1;
        }
    }
    return *this;
}

日期天數(shù)前置++【影響自身(自增)-用引用-不加const】

Date& operator++(); //天數(shù)前置++
Date& Date::operator++()//前置++-改變自身-用引用
{
    return *this += 1;
}

日期天數(shù)后置++【不影響自身-用拷貝構(gòu)造】

Date operator++(int);//后置++
Date Date::operator++(int)//后置++-不改變自身-用拷貝函數(shù)-括號(hào)里+int
{
    Date tmp(*this);
    *this += 1;
    return tmp;
}

日期天數(shù)前置–【影響自身(自減)-用引用-不加const】

Date& operator--();//前置--
Date& Date::operator--()//前置-- --需要改變自身-用引用
{
    return *this -= 1;
}

日期天數(shù)后置–【不影響自身-用拷貝構(gòu)造

Date operator--(int);//后置--
Date Date::operator--(int)//后置--,不需要改變自身-用構(gòu)造函數(shù)-括號(hào)里+int
{
    Date tmp(*this);
    *this -= 1;
    return tmp;
 }

日期減日期(前一個(gè)日期減后一個(gè)日期-算差距天數(shù))

int operator-(const Date& d)const;//日期減日期-算差距天數(shù)
int Date:: operator-(const Date& d) const//日期減日期-算差距天數(shù)-都不改變自身+const
{
	Date max = *this;
	Date min=d;
	int flag = 1;
	if (*this<d)
	{
		max = d;
		min = *this;
		flag = -1;//如果*this比d小則減出來是負(fù)數(shù),所以要預(yù)備flag=-1
	}

	int n = 0;
	while (min < max)//min++,max--,最后相等時(shí),n++得出的就是差距天數(shù)
	{
		n++;
		min++;
	}
	return flag * n;//*this比d小,得出來是負(fù)數(shù)-乘-1,*this比d大,得正數(shù)-乘1
}

流插入函數(shù)

friend ostream& operator<<(ostream& out, Date& d);//流插入友元聲明
ostream& operator<<(ostream& out, Date& d)//流插入
{
    cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    return cout;
}

流提取函數(shù)

    friend istream& operator>>(istream& in, Date& d);//流提取友元聲明
istream& operator>>(istream& in, Date& d)//流提取
{
    in>> d._year;
    in >> d._month;
    in >> d._day;
    return in;
    
 }

好啦,以上就是日期類實(shí)現(xiàn)各個(gè)模塊啦,下面是整體代碼!

整體代碼

Date.h

#pragma once
#include<iostream>
using namespace std;
class Date
{
public:

	Date(const Date& d)//拷貝構(gòu)造函數(shù)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		
	}

	~Date()//析構(gòu)函數(shù)
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}

	void Print()//打印函數(shù)
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	int GetTrueDay(int year, int month)//得到正確月份天數(shù)
	{
		static int monthday[] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			return 29;
		}
		else
		{
			return monthday[month];
		}
	}
	Date(int year = 1, int month = 1, int day = 1)//構(gòu)造函數(shù)
		: _year(year)
		, _month(month)
		, _day(day)
	{

	}

	bool operator==(const Date& d) const;//等于
	bool operator>(const Date& d) const;//大于
	bool operator>=(const Date& d) const//大于等于
	{
		return *this > d || *this == d;
	}
		bool operator<(const Date& d)const //判斷小于
	{
		return !(*this >= d);
	}
		bool operator<=(const Date& d)const//小于等于
		{
			return !(*this > d);
		}

		Date& operator=(const Date& d)//賦值重載
		{
			if (this!=&d)
			{
				_year = d._year;
				_month = d._month;
				_day = d._day;
			}
			else
			{
				return*this;
			}
		}
		Date operator-(int day) const;//減天數(shù)
		Date operator+(int day) const;//加天數(shù)
		Date& operator-=(int day) ;//減等天數(shù)
		Date& operator+=(int day);//加天數(shù)
		Date& operator++(); //天數(shù)前置++
		Date operator++(int);//后置++
		Date& operator--();//前置--
		Date operator--(int);//后置--
		int operator-(const Date& d)const;//日期減日期-算差距天數(shù)
		friend ostream& operator<<(ostream& out, Date& d);//流插入友元聲明
		friend istream& operator>>(istream& in, Date& d);//流提取友元聲明
private:
	int _year;
	int _month;
	int _day;
}; 

Date.cpp

#include"Date.h"




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

bool Date:: operator>(const Date& d) const//大于
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_month > d._month)
	{
		return true;
	}
	else if (_day == d._day)
	{
		return true;
	}
	else
	{
		return false;
	}

}

Date Date:: operator-(int day) const//減天數(shù)-不影響本身-不用引用-用拷貝函數(shù)
{
	Date tmp(*this);
	tmp-= day;
	return tmp;
}
Date Date:: operator+(int day) const//加天數(shù)-不影響本身-不用引用-用拷貝函數(shù)
{
	Date tmp(*this);
	tmp+= day;
	return tmp;
}

Date& Date:: operator-=(int day) //減等天數(shù)- 影響本身-用引用-不加const
{
	if (day < 0)
	{
		return *this += abs(day);
	}
	_day -= day;
	while (_day<=0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetTrueDay(_year, _month);
	}
	return *this;
	
}

Date& Date:: operator+=(int day) //加天數(shù)- 影響本身-用引用-不加const
{
	if (day < 0)
	{
		return *this -= abs(day);
	}
	_day += day;
	while (_day > GetTrueDay(_year, _month))
	{
		_day -= GetTrueDay(_year,_month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date& Date::operator++()//前置++-改變自身-用引用
{
	return *this += 1;
}

Date Date::operator++(int)//后置++-不改變自身-用拷貝函數(shù)-括號(hào)里+int
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator--()//前置-- --需要改變自身-用引用
{
	return *this -= 1;
}
Date Date::operator--(int)//后置--,不需要改變自身-用構(gòu)造函數(shù)-括號(hào)里+int
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
 }

int Date:: operator-(const Date& d) const//日期減日期-算差距天數(shù)-都不改變自身+const
{
	Date max = *this;
	Date min=d;
	int flag = 1;
	if (*this<d)
	{
		max = d;
		min = *this;
		flag = -1;//如果*this比d小則減出來是負(fù)數(shù),所以要預(yù)備flag=-1
	}

	int n = 0;
	while (min < max)
	{
		n++;
		min++;
	}
	return flag * n;//*this比d小,得出來是負(fù)數(shù)-乘-1,比大,乘1
}

ostream& operator<<(ostream& out, Date& d)//流插入
{
	cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return cout;
}

istream& operator>>(istream& in, Date& d)//流提取
{
	in>> d._year;
	in >> d._month;
	in >> d._day;
	return in;
	
 }

以上就是C++無痛實(shí)現(xiàn)日期類的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++日期類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C語言實(shí)現(xiàn)井字棋詳解

    C語言實(shí)現(xiàn)井字棋詳解

    這篇文章主要為大家介紹了C語言如何實(shí)現(xiàn)井字棋,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • OpenCV獲取圖像中直線上的數(shù)據(jù)具體流程

    OpenCV獲取圖像中直線上的數(shù)據(jù)具體流程

    對(duì)圖像進(jìn)行處理時(shí),經(jīng)常會(huì)有這類需求:客戶想要提取出圖像中某條直線或者ROI區(qū)域內(nèi)的感興趣數(shù)據(jù),進(jìn)行重點(diǎn)關(guān)注,怎么操作呢,下面小編通過實(shí)例代碼介紹下OpenCV獲取圖像中直線上的數(shù)據(jù),一起看看吧
    2021-11-11
  • C語言進(jìn)階:指針的進(jìn)階(2)

    C語言進(jìn)階:指針的進(jìn)階(2)

    這篇文章主要介紹了C語言指針詳解及用法示例,介紹了其相關(guān)概念,然后分享了幾種用法,具有一定參考價(jià)值。需要的朋友可以了解下
    2021-09-09
  • C語言實(shí)現(xiàn)車輛出租管理系統(tǒng)

    C語言實(shí)現(xiàn)車輛出租管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)車輛出租管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 淺析C++中結(jié)構(gòu)體的定義、初始化和引用

    淺析C++中結(jié)構(gòu)體的定義、初始化和引用

    以下是對(duì)C++中結(jié)構(gòu)體的定義、初始化和引用進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下
    2013-09-09
  • C++基于先序、中序遍歷結(jié)果重建二叉樹的方法

    C++基于先序、中序遍歷結(jié)果重建二叉樹的方法

    這篇文章主要介紹了C++基于先序、中序遍歷結(jié)果重建二叉樹的方法,結(jié)合實(shí)例形式分析了基于C++構(gòu)建二叉樹的相關(guān)操作技巧,需要的朋友可以參考下
    2017-05-05
  • 深入理解卡特蘭數(shù)及其應(yīng)用

    深入理解卡特蘭數(shù)及其應(yīng)用

    本篇文章是對(duì)卡特蘭數(shù)及其應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C++ Sqlite3的使用方法

    C++ Sqlite3的使用方法

    這篇文章主要介紹了C++ Sqlite3的使用方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C++超詳細(xì)講解操作符的重載

    C++超詳細(xì)講解操作符的重載

    C++預(yù)定義中的運(yùn)算符的操作對(duì)象只局限于基本的內(nèi)置數(shù)據(jù)類型,但是對(duì)于我們自定義的類型(類)是沒有辦法操作的。但是大多時(shí)候我們需要對(duì)我們定義的類型進(jìn)行類似的運(yùn)算,這個(gè)時(shí)候就需要我們對(duì)這么運(yùn)算符進(jìn)行重新定義,賦予其新的功能,以滿足自身的需求
    2022-06-06
  • 一篇文章教你在C++中操作符可分為哪幾種類和用法

    一篇文章教你在C++中操作符可分為哪幾種類和用法

    這篇文章主要介紹了C++編程中操作符的種類和用法,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-09-09

最新評(píng)論