日期类的实现,const成员

目录

一:日期类实现

二:const成员

三:取地址及const取地址操作符重载


一:日期类实现

复制代码
//头文件

#include <iostream>
using namespace std;

class Date
{
	friend ostream& operator<<(ostream& out, const Date d);
	friend istream& operator>>(istream& in, const Date d);
public:
	Date(int year = 1900, int month = 1, int day = 1);

	void Print()
	{
		cout << "年 " << _year << "月 " << _month<<"日 " << _day << endl;
	}

	//得到对应年月的天数
	int GetMonthDay(int year, int month)
	{
		static int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

		if(month == 2 &&((year%100 != 0&& year%4==0) || (year % 400 == 0)))
			return day[month]+1;
		else
			return day[month];
	}

	bool CheckDay();

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

	Date operator++();
	Date operator++(int);

	Date operator--();
	Date operator--(int);

	int operator-(const Date d);

private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& out, const Date d);
istream& operator>>(istream& in, const Date d);

//实现

#include "Date.h"

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

bool Date::CheckDay()
{
	if (_month > 12 || _month < 0 || _day <0 || _day > GetMonthDay(_year, _month))
		return false;
	else
		return true;

}

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

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

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

}

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

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

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


Date& Date::operator+=(int day)
{
	if (day < 0)
		return (*this) -= -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)
{
	if (day < 0)
		return (*this) += -day;

	_day -= day;

	while (_day <= 0)
	{
		if (_month--  < 1)
		{
			_month = 12;
			_year--;
		}

		_day += GetMonthDay(_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;
	int n = 0;
	//假设d1 大, d2小
	Date d1 = *this;
	Date d2 = d;

	if (d1 < d2)
	{
		flag = -1;
		d1 = d;
		d2 = *this;
	}

	while (d1 != d2)
	{
		n++;
		d2++;
	}

	return n * flag;
}

ostream& operator<<(ostream& out, const Date d)
{
	out << "年 " << d._year << "月 " << d._month << "日 " << d._day << endl;

	return out;
}

istream& operator>>(istream& in, const Date d)
{
	cout << "请输入年月日->";

	in >> d._year >> d._month >> d._day;
	
	return in;
}

二:const成员

const 修饰的 " 成员函数 " 称之为 const 成员函数 , const 修饰类成员函数,实际修饰该成员函数
隐含的 this 指针 ,表明在该成员函数中 不能对类的任何成员进行修改。

复制代码
class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//void Print(Date* const this)  本身指向的对象是不能修改的,但是指向的内容可以改 
	void Print()
	{
		cout << "年" << _year << endl;
		cout << "月" << _month << endl;
		cout << "日" << _day << endl;
	}

	//void Print(const Date* const this)  本身指向的对象 和 指向的对象的内容 都不可以修改
	void Print()const
	{
		cout << "年" << _year << endl;
		cout << "月" << _month << endl;
		cout << "日" << _day << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2024, 4, 28);
	d1.Print();

	Date d2(2024, 4, 27);
	d2.Print();

	return 0;
}

三:取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

复制代码
class Date
{
public:
	Date* operator&()
	{
		return this;
	}

	const Date* operator&()const
	{
		return this;
	}
private:
	int _year;
	int _month;
	int _day;
};

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如 想让别人获取到指定的内容!

相关推荐
mit6.8247 分钟前
[Cyclone] 哈希算法 | SIMD优化哈希计算 | 大数运算 (Int类)
算法·哈希算法
c++bug10 分钟前
动态规划VS记忆化搜索(2)
算法·动态规划
哪 吒12 分钟前
2025B卷 - 华为OD机试七日集训第5期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
-凌凌漆-15 分钟前
【Qt】QStringLiteral 介绍
开发语言·qt
程序员爱钓鱼15 分钟前
Go语言项目工程化 — 常见开发工具与 CI/CD 支持
开发语言·后端·golang·gin
努力写代码的熊大43 分钟前
单链表和双向链表
数据结构·链表
小刘同学3211 小时前
C++11 特性
c++·c11新特性
军训猫猫头1 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
success1 小时前
【爆刷力扣-数组】二分查找 及 衍生题型
算法