有关类与对象的练习题——完整版Date

本文详细介绍了使用C++实现一个完整的Date类的过程,包括构造函数、析构函数、拷贝构造函数、赋值操作符重载及多种操作符重载。此外还实现了日期加减运算及日期差值计算等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上次讲解了类的所有基本知识和各种有关函数,今天我就来将这些全部用在一起,完成一个完整版的Date类。

首先这个类中我会加上构造函数、析构函数、拷贝构造函数、类的赋值操作符重载,以及一系列的操作符重载。最重要的是这个Date可以完成给给定的日期加上多少天后的日期,以及减去多少天后的日期,同时也能算出两个日期的差值。

代码如下:

//Date类
#include<iostream>
#include<windows.h>

using namespace std;

class Date
{

//公有成员函数。
public:
	//构造函数
	Date(int year=2018,int month=8,int day=10)     //附有默认参数的函数。
		:_year(year)                               //初始化列表。
		,_month(month)
		,_day(day)
	{}
	//析构函数
	~Date()
	{
	}
	//拷贝构造函数
	Date(Date& d)
	{
		_year=d._year;
		_month=d._month;
		_day=d._day;
	}
	//类的赋值操作符重载
	Date& operator=(const Date& d)
	{
		if(this!=&d){                   //判断两个值不相等,若相等直接退出。
		_year=d._year;
		_month=d._month;
		_day=d._day;
		}
		return *this;
	}
	//打印函数。
	void show()
	{
		cout<<"year :"<<_year<<"month :"<<_month<<"day :"<<_day<<endl;
	}

	//相等操作符重载
	bool operator==(Date& d)
	{
		return ((_year==d._year) && (_month==d._month) && (_day==d._day));
	}

	//大于号重载。
	bool operator>(Date& d)
	{
		return ((_year>d._year) || ((_year==d._year) && (_month>d._month)) || ((_year==d._year) && (_month==d._month) && (_day>d._day)));
	}
	//小于号重载。
	bool operator<(Date& d)
	{
		return (!(*this==d) && !(*this>d));
	}

	//不等号重载。
	bool operator!=(Date& d)
	{
		return (!(*this==d));
	}

	//大于等于重载。
	bool operator>=(Date& d)
	{
		return ((*this>d) || (*this==d));
	}
	//小于等于重载。
	bool operator<=(Date& d)
	{

		return (!(*this>d));
	}

	//判断是否为闰年的函数。是返回1,不是返回0.
	int Isleapyear(int year)
	{
		return (((year%4==0) && (year%100!=0)) || (year%400==0));
	}

	//得到当前月的天数。
	int Day(int year,int month)
	{
		int D[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
		if(Isleapyear(year)==1 && month==2)   //若为闰年并且是2月,就返回29天,否则返回对应的天数。
		{
			return 29;
		}
		else{
			return D[month];
		}
	}

	//加号重载。
	Date operator+(int daynum)
	{
		Date t(*this);               //拷贝一个变量。
		t._day+=daynum;              
		while(t._day>t.Day(t._year,t._month))   //当这个天数加上所给的天数之和大于当前月的天数时进行循环。
		{
			t._day-=t.Day(t._year,t._month);     //天数往小的减。
			t._month+=1;                         //月份却往上加。
			if(t._month>12)                     //若月份大于12,年往上加。
			{
				t._month=1;
				t._year+=1;
			}
		}
		return t;                              //返回天数。
	}

	//减号重载。与加号重载差不多。
	Date operator-(int daynum)
	{
		Date t(*this);
		t._day-=daynum;
		while(t._day<=0)                 //当两个天数相减小于0时进入循环。
		{
			t._day+=t.Day(t._year,t._month);  
			t._month-=1;
			if(t._month<1)
			{
				t._month=12;
				t._year-=1;
			}
		}
		return t;
	}

	//后置++重载。
	Date operator++(int)
	{
		Date t(*this);
		*this =*this+1;
		return t;
	}
	//前置++重载。
	Date& operator++()
	{
		*this=*this+1;
		return *this;
	}
	//后置--重载。
	Date operator--(int)
	{
		Date t(*this);
		*this=*this-1;
		return t;
	}
	//前置--重载。
	Date& operator--()
	{
		*this=*this-1;
		return *this;
	}
	//加等号重载。
	Date& operator+=(int daynum)
	{
		*this=*this+daynum;
		return *this;
	}
	//减等号重载。
	Date& operator-=(int daynum)
	{
		*this=*this-daynum;
		return *this;
	}

	//算出两个日期的差值。
	int operator-(Date& d)
	{
		Date t(*this);
		int n=0;
		if(t>d)                   
		{
			while(t>d)
			{
				++d;
				n++;
			}
		}
		if(t==d)
		{
			return n;
		}
		else{
			while(t<d)
			{
				++t;
				n++;
			}
		}
		return n;

	}

//私有成员变量
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	Date d2;
	int d3;
	d2.show();
	d2=d1-20;
	d2.show();
	d3=d1-d2;
	cout<<d3<<endl;
	system("pause");
	return 0;
}

这些函数十分简单,但是如果不亲手练习的话也不会掌握的很好,希望大家能多练练,巩固自己所学的知识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值