c++::实现一个日期类

这里我实现一个简单的日期类吐舌头

#ifndef __TEST__
#define __TEST__
#include<iostream>
using namespace std;
class Date
{
	friend ostream& operator<<(ostream& _cout,const Date& d);
	friend istream& operator>>(istream& _cin, Date& d);
public:
	Date();
	Date(int year,int month,int day);
	Date& operator=(const Date& d);
	Date(const Date& d);
	~Date();
	int Getdaysinmonth(int year,int month);
	Date operator-(int days);
	int operator-(const Date& d);
	Date& operator++();
	Date operator++(int);
	bool operator>(const Date& d);
	bool operator<(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);
	
private:
	int _year;
	int _month;
	int _day;

};
#endif


#include"test.h"
//构造函数
Date::Date()
{}
Date::Date(int year=2000,int month=1,int day=1)
{
	_year = year;
	_month = month;
	_day = day;
	if((year<2000) || 
		(month<=0) && (month>13) ||
		(day<0) && (day>Getdaysinmonth(year,month)))
	{
		cout<<"输入不合法,请重新输入:"<<endl;
		//cin>>"year:">>year>>"month:">>month>>"day:">>day>>endl;
	}
	else
	{
		cout<<"year: "<<year<<"month: "<<month<<"day: "<<day<<endl;
	}
}
//让天数合法化
int Date::Getdaysinmonth(int year,int month)
{
	int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if((((year%4==0) && (year%100 != 0))
		|| (year%400 == 0))&&(month == 2))
	{
		days[month] = 29;
	}
	
	return days[month];

}
//拷贝构造函数
Date::Date(const Date &d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
//赋值运算符重载
Date& Date::operator=(const Date&d)
{
	if(this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}
Date::~Date()
{
	cout<<"已被析构!"<<endl;
}
//减去一个任意天数
Date Date::operator-(int days)
{
	if(days<0)
	{
		days = 0-days;
	}
	Date temp(*this);
	temp._day-=days;
	while(temp._day<0)
	{
		if(temp._month == 1)
		{
			temp._month = 12;
			temp._day += 31;
		}
		else
		{
			--temp._month;
			temp._day += Getdaysinmonth(_year,temp._month);
		}
		
	}
		return temp;
}
//两个天数相减
int Date::operator-(const Date& d)
{
	Date mindate(*this);
	Date maxdate(d);
	int count = 0;
	if(mindate>maxdate)
	{
		std::swap(mindate,maxdate);
	}
	while(mindate<maxdate)
	{
		(mindate._day)++;
		count++;
	}
	return count;
}
//天数的前置加加
Date& Date::operator++()
{
	++_day;
	return *this;

}
//天数的后置加加
Date Date::operator++(int)
{
	Date temp(*this);
	temp._day = temp._day+1;
	//temp++;
	return temp;
}
//比较日期
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 ((_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 ((_year == d._year) && 
		(_month == d._month) &&
		(_day == d._day));
}
bool Date::operator!=(const Date& d)
{
	//return !(bool operator==(d));
	return ((_year!=d._year)||
		((_year == d._year)&&(_month!=d._month))||
		((_year == d._year)&&(_month==d._month)&&(_day!=_day)));
}
ostream& operator<<(ostream& _cout,const Date& d)
{
	_cout<<d._year<<" "<<d._month<<" "<<d._day;
	return _cout;
}
istream& operator>>(istream& _cin,Date& d)
{
	_cin>>d._year>>d._month>>d._day;
	return _cin;
}


#include"test.h"
//测试基本函数;构造。拷贝构造。赋值
void test1()
{
	Date d1(2013,4,5);
	Date d2(d1);
	Date d3(d1);
	d3 = d2;
	Date d4(1990,3,5);
}
//检查天数是否合法
void test2()
{
	Date d1;
	cout<<d1.Getdaysinmonth(2000,2)<<endl;
	cout<<d1.Getdaysinmonth(2013,2)<<endl;
}
//测试减去任意一个天数
void test3()
{
	Date d1(2014,5,6);
	d1= d1-30;
	cout<<d1<<endl;
}
//测试两个天数相减
void test4()
{
	Date d1(2014,3,5);
	Date d2(2014,3,30);
	cout<<d2-d1<<endl;
}
//测试比较
void test5()
{
	Date d1(2015,6,7);
	Date d2(2015,6,9);
	cout<<(d1<d2)<<endl;
	cout<<(d1>d2)<<endl;
	cout<<(d1==d2)<<endl;
	cout<<(d1!=d2)<<endl;
}
int main()
{
	//test1();
	//test2();
	//test3();
	//test4();
	test5();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值