0
点赞
收藏
分享

微信扫一扫

[Cpp]类和对象 | 实现日期类

金穗_ec4b 2024-05-09 阅读 23

标题:[Cpp]类和对象 | 实现日期类

@水墨不写bug



正文开始:

        类和对象是Cpp面向对象编程区别于C的面向过程编程的重要的一部分,因此打好坚实的类和对象的基础对于深入学习Cpp语言是比较明智的。

        本文通过实现简单的日期类来加深对类和对象的理解,同时梳理相关知识,熟悉对象的编写过程,为项目的经验要求打下基础。

(一)头文件

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

class Date
{
public:
	friend ostream& operator<<(ostream& out, const Date d);

	// 获取某年某月的天数
	inline int GetMonthDay(int year, int month)
	{
		assert(month >= 1 && month <= 12);
		static int _month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		//如果进入if说明是闰年并且是二月
		if (month == 2 && ( (year % 4 == 0) && (year % 100 != 0) ) || (year % 400 == 0))
		{
			return 29;
		}
		else
			return _month[month];
	}
	// 全缺省的构造函数
	//声明时写缺省值
	Date(int year = 1900, int month = 1, int day = 1);

	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d);

	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);

	// 析构函数
	~Date();


	// 日期+=天数
	Date& operator+=(int day);

	// 日期+天数
	Date operator+(int day);

	// 日期-天数
	Date operator-(int day);

	// 日期-=天数
	Date& operator-=(int day);

	// 前置++
	Date& operator++();

	// 后置++
	Date operator++(int);

	// 后置--
	Date operator--(int);

	// 前置--
	Date& operator--();

	// >运算符重载
	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);

	// 日期-日期 返回天数
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};

(二)实现日期类

(1)构造函数


// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	//assert(month>=1 && month<=12 && day>=1);
	_year = year;
	_month = month;
	_day = day;
}

(2)析构函数


// 析构函数
Date::~Date()
{
	_year = -1;
	_month = -1;
	_day = -1;
}

(3)拷贝构造


// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

(4)赋值重载


// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
    //检查是否是给自己赋值,防止性能损失
    if(&d != this)
    {
	    _year = d._year;
	    _month = d._month;
	    _day = d._day;
    }
    
	return *this;
}

(5)日期的运算


// 日期+=天数
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		(*this) -= (-day);
	}
	else
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			++_month;
			if (_month == 13)
			{
				++_year;
				_month = 1;
			}
		}
	}
	return *this;
}


// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day >= 0)
	{
		_day -= day;
		//day<=0无意义
		while (_day <= 0)
		{
			_month--;
			if (_month == 0)
			{
				_month = 12;
				--_year;
			}
			_day += GetMonthDay(_year, _month);
		}
	}
	else
		(*this) += (-day);
	return *this;
}

// 日期+天数
Date Date::operator+(int day)
{
	//不改变*this
	Date tem = *this;
	tem += day;
	return tem;
}

// 日期-天数
Date Date::operator-(int day)
{
	Date tem = *this;
	tem -= day;
	return tem;
}

//先++再使用
// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

//先使用再++
// 后置++
Date Date::operator++(int)
{
	Date tem = *this;
	*this += 1;
	return tem;
}

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

// 后置--
Date Date::operator--(int)
{
	Date tem;
	*this -= 1;
	return tem;
}

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	//假设法
	Date max = *this;
	Date min = d;
	int c = 0, f = 1;

	if (min > max)
	{
		max = d;
		min = *this;
		f = -1;
	}
	//找出较大值和较小值
	while (min != max)
	{
		++min;
		++c;
	}
	return c*f;
}

 

(6)日期的比较


// ==运算符重载
bool Date::operator==(const Date& d)
{
	return (_year == d._year && _month == d._month && _day == d._day);
}

// >运算符重载
bool Date::operator>(const Date& d)
{
	return  (_year > d._year) ||
		(_year == d._year && _month > d._month) ||
		(_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) || (*this < d);
}

// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}


完~

未经作者同意禁止转载

举报

相关推荐

0 条评论