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

C++實現(xiàn)Date類各種運算符重載的示例代碼

 更新時間:2024年02月18日 09:38:59   作者:傷心男孩拯救世界(Code King)  
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)Date類各種運算符重載的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

上一篇文章只實現(xiàn)了operator==操作符重載,由于運算符較多,該篇文章單獨實現(xiàn)剩余所有的運算符重載。繼續(xù)以Date類為例,實現(xiàn)運算符重載:

1.Date.h

#pragma once
 
#include <iostream>
#include <assert.h>

using namespace std;

class Date
{
private:
	int _year;
	int _month;
	int _day;
public:
	void Print();
	Date(int yaer, int month, int day);

	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 GetMonthDays(int year, int month)
	{
		assert(month > 0 && month < 13);

		static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month==2&&(year % 4 == 0 && year % 100 != 0))
		{
			return 29;
		}
		return MonthDay[month];
	}

	Date& operator+=(int day);
	Date operator+(int day);

	Date& operator-=(int day);
	Date operator-(int day);
	//++d,前置++
	Date& operator++();
	//d++,后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);

	//兩個日期相減:d1-d2
	int operator-(const Date& d);
};

2.Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>

#include "Date.h"

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

Date::Date(int year=1, int month=1, int day=1)
{
	_year = year;
	_month = month;
	_day = day;
}
//   寫好一個直接復(fù)用?。?!
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) && (_day < d._day))
			return true;
		else
			return false;
	}
	else
		return false;
}

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

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

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

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

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

Date& Date::operator+=(int day)
{
	_day += day;//先加
	//這里是while,因為如果是if的話,如果一次加了很大的數(shù)據(jù)減一次不一定能減得完!!!
	while(_day > GetMonthDays(_year, _month))
	{
		_day -= GetMonthDays(_year, _month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day)
{
	Date tmp=*this;

   
	tmp += day;
	return tmp;
}


Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDays(_year, _month);
	}
	return *this;
}


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

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

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

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

Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
//日期-日期,計算兩個日期之間相差多少天

int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;

	if (*this < d)
	{
		//賦值為-1的原因:因為這個函數(shù)是有順序的d1-d2,如果遇到d1<d2,也就是小減大,最終返回的結(jié)果是個負(fù)數(shù),所以說這里要變成-1。
		flag = -1;
		max = d;
		min = *this;
	}
	//定義一個變量
	int n = 0;
	// 用循環(huán)算兩個日期中間的差值
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;
}

3.Test.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>
#include "Date.h"

int main()
{
	Date d1(2024, 2, 15);

	Date d2 = d1 + 20;
	d1.Print();
	d2.Print();

	bool ret=d1 > d2;
	if (ret)
	{
		d1.Print();
	}

	d2 += 100;
	d2.Print();


	d2 -= 100;
	d2.Print();
	Date d3 = d2 - 10;
	d3.Print();

	Date d4(2024, 1, 29);
	Date d5(2024, 8, 1);
	cout << d5 - d4 << endl;

	++d5;
	d5.Print();

	d5++;
	d5.Print();

	--d5;
	d5.Print();

	d5--;
	d5.Print();
	return 0;
}

以上就是C++實現(xiàn)Date類各種運算符重載的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++運算符重載的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C語言從代碼中加載動態(tài)鏈接庫過程解析

    C語言從代碼中加載動態(tài)鏈接庫過程解析

    這篇文章主要介紹了C語言從代碼中加載動態(tài)鏈接庫過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • 通過示例詳解C++智能指針

    通過示例詳解C++智能指針

    這篇文章主要為大家通過示例介紹了C++智能指針的使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • C/C++實現(xiàn)蛇形矩陣的示例代碼

    C/C++實現(xiàn)蛇形矩陣的示例代碼

    本文主要介紹了C/C++實現(xiàn)蛇形矩陣的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C++使用opencv調(diào)用級聯(lián)分類器來識別目標(biāo)物體的詳細(xì)流程

    C++使用opencv調(diào)用級聯(lián)分類器來識別目標(biāo)物體的詳細(xì)流程

    所謂級聯(lián)分類器其實就是把分類器按照一定的順序聯(lián)合到一起,下面這篇文章主要給大家介紹了關(guān)于C++使用opencv調(diào)用級聯(lián)分類器來識別目標(biāo)物體的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • C++實現(xiàn)猜數(shù)游戲

    C++實現(xiàn)猜數(shù)游戲

    這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)猜數(shù)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 基于稀疏圖上的Johnson算法的詳解

    基于稀疏圖上的Johnson算法的詳解

    本篇文章介紹了,稀疏圖上的Johnson算法的詳解。需要的朋友參考下
    2013-05-05
  • C/C++?Qt?TabWidget?實現(xiàn)多窗體創(chuàng)建詳解

    C/C++?Qt?TabWidget?實現(xiàn)多窗體創(chuàng)建詳解

    TabWidget組件配合自定義Dialog組件,可實現(xiàn)一個復(fù)雜的多窗體分頁結(jié)構(gòu)。這篇文章就主要介紹了如何通過TabWidget實現(xiàn)多窗體的創(chuàng)建,感興趣的小伙伴可以了解一下
    2021-12-12
  • C++概念重載、覆蓋、隱藏的使用說明

    C++概念重載、覆蓋、隱藏的使用說明

    本篇文章介紹了,在C++中概念重載、覆蓋、隱藏的使用分析說明。需要的朋友參考下
    2013-05-05
  • C++中rapidjson組裝map和數(shù)組array的代碼示例

    C++中rapidjson組裝map和數(shù)組array的代碼示例

    今天小編就為大家分享一篇關(guān)于C++中rapidjson組裝map和數(shù)組array的代碼示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • C++指針學(xué)習(xí)詳解

    C++指針學(xué)習(xí)詳解

    指針在 C\C++ 語言中是很重要的內(nèi)容,并且和指針有關(guān)的內(nèi)容一向令初學(xué)者頭大,這篇文章主要給大家介紹了關(guān)于C/C++中指針的相關(guān)資料,需要的朋友可以參考下
    2021-09-09

最新評論

包头市| 同德县| 弥渡县| 正镶白旗| 香河县| 紫云| 威信县| 通辽市| 临江市| 台北县| 米易县| 昌图县| 德昌县| 弥勒县| 蕲春县| 塔城市| 安图县| 嘉鱼县| 建德市| 贵阳市| 平泉县| 宿州市| 哈密市| 濉溪县| 阳曲县| 绥棱县| 嘉黎县| 泸水县| 洪湖市| 香河县| 拉萨市| 龙游县| 巴马| 资中县| 古田县| 新乡市| 成都市| 洪泽县| 含山县| 咸阳市| 河曲县|