C++类和对象综合练习——日期类

本文详细介绍了如何在C++中使用类和对象实现日期类,包括日期的构造函数、运算符重载(如+、-、==等)以及流插入和提取的实现。通过实例展示了如何进行日期的加减操作和比较。
摘要由CSDN通过智能技术生成

结合对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 = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	// 获取某年某月的天数
	int GetMonthDay(int year, int month);

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

	// 赋值运算符重载
    // 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;
};

Date.cpp文件如下

#include"Date.h"

// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	int days[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 days[2] + 1;
	}
	return days[month];
}

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

	return *this;
}

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

// 日期+=天数
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)
{
	Date tmp(*this);
	tmp -= day;

	return tmp;
}

// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day);
	}

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

	return *this;
}

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

// 后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;

	return tmp;
}

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

	return tmp;
}

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

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	Date min = *this;
	Date max = d;
	int flag = 1;
	int count = 0;

	if (*this > d)
	{
		min = d;
		max = *this;
		flag = -1;
	}
	while (min != max)
	{
		++min;
		++count;
	}

	return count * flag;
}

// <<流插入
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;

	return _cout;
}

istream& operator>>(istream& _cin, Date& d)
{
	_cin >> d._year;
	_cin >> d._month;
	_cin >> d._day;

	return _cin;
}

测试文件Test.cpp文件

#include"Date.h"

void Test1()
{
	Date d1;
	int day = d1.GetMonthDay(1900, 2);
}

void Test2()
{
	Date d2(2024,4,3);
	Date d3(d2);
	Date d4;
	
	//if (d2 == d4)
	//{
	//	return;
	//}
	//else
	//{
	//	d4 = d2;
	//}
	if (d2 > d4)
	{
		d4 = d2;
	}
}

void Test3()
{
	Date d5(2024, 4, 4);
	Date d6(d5);

	d6 = d5 + 100;
	d5 += 100;
	d6 += -100;
	d6 -= -100;
}

void Test4()
{
	Date d7(2024, 4, 4);
	Date d8;

	d8 = d7 - 100;
	d7 -= 100;
}

void Test5()
{
	Date d9(2024, 4, 4);
	Date d10(2024, 1, 1);

	int a = d9 - d10;

	++d9;
	d10 = d9++;

	--d9;
	d10 = d9--;
}

void Test6()
{
	Date d11(2024, 4, 4);
	Date d12;

	cout << d11 << d12;
	cin >> d11 >> d12;
}

int main()
{
	//Test1();
	//Test2();
	Test3();
	//Test4();
	//Test5();
	//Test6();

	return 0;
}

对于流插入和流提取运算符的重载需要了解io流的一些基本原理,如需深入学习,可以访问C++官方文档查阅对应资料。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值