Date类的实现(7)

目录

1、前置++与后置++的成员函数

1、后置++的占位参数

2、前置++与后置++的成员函数实现

1、前置++与后置++的区别

2、实现

2、类中不仅可以调用成员变量,还可以调用成员函数

3、类中运算符重载的复用

4、Date类的源码

1、Date.h

2、Date.cpp

3、test.cpp


1、前置++与后置++的成员函数

1、后置++的占位参数

2、前置++与后置++的成员函数实现

1、前置++与后置++的区别

2、实现

2、类中不仅可以调用成员变量,还可以调用成员函数

3、类中运算符重载的复用

对于>、>=、==、!=、<、<=,这六个运算符重载的实现,可以只实现>、==,剩下的进行复用

例如:对日期类:

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

那么剩下的就可以复用:

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);
}

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

4、Date类的源码

1、Date.h

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:
	Date(int year = 0, int month = 1, int day = 1);
	void Print();
	int GetMonthDay(int year,int month);
	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);

	Date& operator+=(int day);
	Date operator+(int day);
	Date& operator-=(int day);
	Date operator-(int day);
	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};

2、Date.cpp

#include"Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(_year >= 0 && (_month > 0 && _month < 13) && 
		(_day > 0 && _day <= GetMonthDay(_year, _month))))
	{
		cout << "非法日期->";
		Print();//类里面不仅可以调用成员变量,还可以调用成员函数
		        //this->Print();	         
	}
}

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

int Date::GetMonthDay(int year,int month)
{
	static int MonthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = MonthDayArray[month];
	if (month==2&&(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		day += 1;
	}
	return day;
}

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

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

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);
}

bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}
//d1+=10
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day >GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

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

Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return ret;
}
//前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//后置++
Date Date::operator++(int)
{
	Date ret(*this);
	*this += 1;
	return ret;
}
//d2-d1
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int count = 0;
	while (min != max)
	{
		++min;
		++count;
	}
	return count * flag;
}

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

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

3、test.cpp

#include"Date.h"

void Test1()
{
	Date d1;
	Date d2(2022, 10, 18);
	Date d3(2022, 10, 19);
	d1.Print();
	d2.Print();

	cout<<(d2 > d3)<<endl;
	cout << (d2 == d3) << endl;
	cout << (d2 >= d3) << endl;
	cout << (d2 < d3) << endl;
	cout << (d2 <= d3) << endl;
	cout << (d2 == d3) << endl;
}

void Test2()
{
	Date d1(2022, 10, 18);
	d1.Print();

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

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

	Date d4(d1);
	d4 += 30;
	d4.Print();

	Date d5(d1);
	d5 += 370;
	d5.Print();

	Date d6(d1);
	Date d7 = d6 + 10;
	d7.Print();
}

void Test3()
{
	Date d1(2022, 10, 18);
	d1.Print();

	Date d2(d1);
	d2 -= 10;
	d2.Print();

	Date d3(d1);
	d3 -= 20;
	d3.Print();

	Date d4(d1);
	d4 -= 30;
	d4.Print();

	Date d5(d1);
	d5 -= 370;
	d5.Print();

	Date d6(d1);
	Date d7 = d6 - 10;
	d7.Print();

	Date d8(d1);
	++d8;
	d8.Print();
}

int main()
{
	//Test1();
	//Test2();
	Test3();
	
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值