【C++修行之路】类和对象实战——时间类

前言

大家好久不见,学完类和对象的内容,我们要尝试写一个时间类巩固。

用到知识在前两篇C++博客中都有详细介绍,建议看完再来学习。

功能概要

利用 类和对象 的知识,实现一个时间类,能够实现天数比较、加一天、减一天、计算两个日期之间天数差距,并重载部分符号。

date.h

#pragma once
#include <iostream>
using namespace std;

class Date
{
    
    
	// 友元
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	Date(int year = 0,int month = 0,int day = 0) : _year(year), _month(month), _day(day)
	{
    
    }

	//Date(const Date&) {} 会浅拷贝的可以不写

	void print() const;
	int GetMonthDay(int year,int month) const;

	bool operator<(const Date& d) const;
	bool operator==(const Date& d) const;
	bool operator!=(const Date& d) const;
	bool operator<=(const Date& d) const;
	bool operator>(const Date& d) const;
	bool operator>=(const Date& d) const;

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

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

	//++d1
	Date& operator++();
	//d1++
	Date operator++(int);

	//--d1
	Date& operator--();
	//d1--
	Date operator--(int);


	//d1-d2
	int operator-(const Date& d);
		
private:
	int _year;
	int _month;
	int _day;
};

功能实现

GetMonthDay

日期获取要注意二月的天数不是固定的,因此我们要写一个函数来获取闰年。

int Date:: GetMonthDay(int year,int month)
{
    
    
	int monthArray[13] = {
    
    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;
	}
	return monthArray[month];
}

日期比较

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

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

//>
bool Date::operator>(const Date& d)
{
    
    
	if(_year < d._year)
	{
    
    
		return false;
	}
	else if(_year == d._year && _month < d._month)
	{
    
    
		return false;
	}
	else if(_year == d._year && _month == d._month && _day < d._day)
	{
    
    
		return false;
	}
	return true;	
}
//>=
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);
}

日期加减

// +=
Date& Date::operator+=(int day)
{
    
    
	_day += day;
	while(_day > GetMonthDay(_year,_month))
	{
    
    
		_day -= GetMonthDay(_year,_month);
		_month++;
		if(_month > 12)
		{
    
    
			_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 += GetMonthDay(_year,_month);
	}
	return *this;
}

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

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

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

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

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

总结

到这里本文就结束了,希望对你有所帮助,知识点讲解在前两期文章中。

猜你喜欢

转载自blog.csdn.net/m0_73209194/article/details/129067382