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

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

 更新時間:2024年02月18日 09:38:59   作者:傷心男孩拯救世界(Code King)  
這篇文章主要為大家詳細介紹了C++實現(xiàn)Date類各種運算符重載的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(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é)果是個負數(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類各種運算符重載的示例代碼的詳細內(nèi)容,更多關(guān)于C++運算符重載的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論