C++类与对象基础(5)——日期类的实现

发布时间:2024年01月08日

? ? ? ?对于实现日期类中需要用到的例如:构造函数,析构函数,运算符重载等内容,已经在前面几篇文章中进行介绍,故本文只给出关于类和对象中日期类的代码实现,对于代码的原理不给予详细的解释:

1.头文件violent.h:

#pragma once

#include<stdio.h>
#include<iostream>
#include<assert.h>
#include<stdbool.h>

using std::cout;
using std::cin;
using std::endl;


class Date
{
public:

	//构造函数:函数名与类名相同,没有返回值,可以构成重载,自动调用
	//针对内置类型不做处理,针对自定义类型会自动调用其自己的构造函数
	Date(int year = 1, int month = 1, int day = 1);


	//拷贝构造函数,对于日期类可以不写
	//拷贝构造函数针对内置类型会完成值拷贝,针对自定义类型会自动调用其自己的拷贝构造函数
	//Date(Date& d1)
	//{
	//	_year = 2024;
	//	_month = 1;
	//	_day = 6;
	//}

	

	//类的比较运算函数
	bool operator==(Date& d);
	bool operator!=(Date& d);
	bool operator>(Date& d);
	bool operator>=(Date& d);
	bool operator<=(Date& d);
	bool operator<(Date& d);
	
	//获取年月份对应的日期
	int GetMonthDay(int _year, int _month);
	void Print();

	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-(Date& d);


	//析构函数,不是清除对象,而是对对象中的资源进行清理,作用方式与构造函数类似,针对内置类型不作用
	//针对自定义类型会调用自己的析构函数
	//函数名是类名之前加上波浪线,每个类中只能存在一个析构函数,类生命周期结束会自动调用,不能构成重载
	~Date();
	
	



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

2.函数功能实现文件violent.c:

#define _CRT_SECURE_NO_WARNINGS 1

#include"violent.h"

//构造函数
Date::Date(int year, int month, int day)
{
	if ((year < 1) || (month > 12) || (month < 1) || (day < 1))
	{
		cout << "日期信息非法" << endl;
	}
	_year = year;
	_month = month;
	_day = day;
}
//析构函数
Date:: ~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}


void Date::Print()
{
	cout << _year << " " << _month << " " << _day << endl;
}
bool Date::operator==(Date& d)
{
	return _year == d._year 
		&& _month == d._month 
		&& _day == d._day;
}

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

bool Date::operator>(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;
	}
}

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

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

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

}

int Date::GetMonthDay(const int _year,const int _month)
{
	int Day[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 Day[_month];
}

//运算符重载:+=(会改变类)
Date& Date::operator+=(int day)
{
	_day = _day + day;

	while(_day > GetMonthDay(_year,_month))
	{
		_day = _day - GetMonthDay(_year, _month);

		_month++;
		if (_month > 12)
		{
			_month = 0;
			_year++;
		}
	}

	return (*this);
}

Date Date::operator+(int day)
{
	Date tmp(*this);

	tmp = tmp += day;

	return tmp;
}

Date& Date::operator-=(int day)
{
	_day = _day - day;

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

	return (*this);
}

Date Date::operator-(int day)
{
	Date tmp(*this);

	tmp = (tmp -= day);

	return tmp;
}

Date& Date::operator++()
{
	*this += 1;

	return (*this);
}

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-(Date& d)
{
	int flag = 1;
	Date max = (*this);
	Date min = (d);

	if ((*this) < d)
	{
		max = d;
		min = (*this);
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		min++;
		n++;
	}

	return n * flag;
}

3.功能测试文件Test.c:

#define _CRT_SECURE_NO_WARNINGS 1

#include"violent.h"

void Test1()
{
	Date d1(2024,1,6);
	d1.Print();

	Date d2(2024, 1, 6);
	d2.Print();

	Date d3(2024, 1, 15);

	cout << "测试结果校验: ==" << endl;
	bool ret =(d1==d2);
	cout << " ret = " << ret << endl;

	cout << "测试结果校验: != " << endl;
	bool ret1 = (d2 != d1);
	cout << " ret1 = " << ret1 << endl;

	cout << "测试结果校验:>" << endl;
	bool ret2 = (d3 > d1);
	cout << " ret3 =  " << ret2 << endl;

	cout << "测试结果校验: >=" << endl;
	bool ret3 = (d3 >= d1);
	bool ret4 = (d2 >= d1);
	cout << " ret3 = " << ret3 << endl << "ret4 = " << ret4 << endl;

	cout << "测试结果校验: <=" << endl;
	bool ret5 = (d1 <= d3);
	bool ret6 = (d2 <= d1);
	cout << " ret5 = " << ret5 << endl << " ret6 = " << ret6 << endl;

	cout << "测试结果校验: <" << endl;
	bool ret7 = (d2 < d3);
	cout << "ret7 = " << ret7 << endl;
}

void Test2()
{

	cout << endl << " 下面内容为Test2中的测试" << endl;
	Date dd(2024, 1, 6);
	dd.Print();
	Date dd1(2024, 1, 6);

	cout << "测试+=" << endl;
	Date ret1 = (dd += 100);
	ret1.Print();

	cout << "测试+" << endl;
	Date ret2 = (dd1 + 100);
	ret2.Print();

	cout << "测试-=" << endl;
	Date ret3 = (dd -= 100);
	ret3.Print();

	cout << "测试-" << endl;
	Date ret4 = (dd1 - 100);
	ret4.Print();
}

void Test3()
{
	cout << endl << " 下面内容为Test3中的测试" << endl;
	Date d3(2024, 1, 6);
	cout << " 测试++d" << endl;
	Date ret1 = (++d3);
	Date d4(2024, 1, 6);

	ret1.Print();
	cout << " 测试d++" << endl;
	Date ret2 = (d4++);
	ret2.Print();
}

void Test4()
{
	cout << endl << " 下面内容为Test4中的测试" << endl;
	Date d(2024, 1, 6);
	Date d2(2024, 1, 6);
	cout << "测试--d" << endl;
	Date ret1 = (--d);
	ret1.Print();
	cout << "测试d--" << endl;
	Date ret2 = (d2--);
	ret2.Print();
}

void Test5()
{
	Date d1(2024, 1, 6);
	Date d2(2024, 4, 15);

	int ret1 = d2 - d1;

	cout << ret1 << endl;
}
int main()
{
	Test1();

	Test2();

	Test3();	

	Test4();

	Test5();
}

4. 代码运行结果展示:

?

文章来源:https://blog.csdn.net/2301_76836325/article/details/135459672
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。