cpp--实战练习:日期类,加强练习!

日期类

相信大家在看完

  • 构造函数与析构函数cpp – 构造函数与析构函数
  • 拷贝构造函数cpp–拷贝构造函数详解
  • 赋值运算符重载cpp–赋值运算符重载,浅显易懂!

这三篇文章后对c++中的类和对象都会有一定的了解
下面我们通过日期类Date 来加强一下对上述知识的理解

  • Date.h存放头文件以及函数的声明
  • Date.cpp存放函数的定义
  • test.cpp存放主函数以及调用函数

Date.h

#pragma once
#include 
using namespace std;
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	//全缺省参数的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	//浅拷贝构造函数
	Date(const Date& d);
	void Print();
	void Print() const;
	//赋值运算符重载
	//d2 = d3  ->  d2.operator=(&d2,&d3)
	Date& operator=(const Date& d);

	int getmonthday(int year, int month);
	//日期+=天数
	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);

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

	//取地址的操作符重载
	Date* operator&()
	{
		return this;
	}

	//const取地址的操作符重载
	/*const Date* operator&()const
	{
		return this;
	}*/
private:
	int _year;
	int _month;
	int _day;
};

//只能全局,否则方向相反
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

Date.cpp

#include "Date.h"

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

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

void Date::Print() const
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

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;
}
int Date::getmonthday(int year, int month)
{
	int monthday[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 monthday[month];
}

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > getmonthday(_year, _month))
	{
		_day -= getmonthday(_year, _month);
		_month++;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

Date Date::operator+(int day)
{
	Date temp(*this);
	temp += day;
	return temp;
}


Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		//减法要借位,借的时上个月的天数,所以首先对_month--;
		_day += getmonthday(_year, _month);

	}
	return *this;
}

Date Date::operator-(int day)
{
	Date temp = *this;
	temp -= day;
	return temp;
}

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

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

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

bool Date::operator>(const 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==(const Date& d)
{
	return _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);
}

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

int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this >= d)
	{
		max = *this;
		min = d;
	}
	else
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int count = 0;
	while (max != min)
	{
		min++;
		count++;
	}
	return flag* count;
}


ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "/" << d._month << "/" << d._day << endl;
	return out;
}
istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

test.cpp

#include "Date.h"
void TestDate1()
{
	Date d1(2020, 8, 28);
	Date d2(2025, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	d1 = d2;
	d1.Print();
}
void TestDate2()
{
	Date d1(2021, 2, 28);
	Date d2(2025, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	Date d3 = d1 += 1;
	d3.Print();
}
void TestDate3()
{
	Date d1(2024, 3, 1);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	Date d3 = d1 -= 1;
	d3.Print();
}

void TestDate4()
{
	Date d1(2024, 2, 28);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	Date d3 = d1 + 1;
	d3.Print();
}

void TestDate5()
{
	Date d1(2023, 3, 1);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	Date d3 = d1 - 1;
	d3.Print();
}

void TestDate6()
{
	Date d1(2024, 2, 28);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	Date d3 = ++d1;
	d3.Print();
}

void TestDate7()
{
	Date d1(2024, 2, 28);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	Date d3 = d1++;
	d3.Print();
	Date d4 = d1;
	d4.Print();
}

void TestDate8()
{
	Date d1(2023, 3, 1);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	Date d3 = --d1;
	d3.Print();
}

void TestDate9()
{
	Date d1(2024, 2, 28);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	cout << endl;
	Date d3 = d1--;
	d3.Print();
	Date d4 = d1;
	d4.Print();
}
void TestDate10()
{
	Date d1(2024, 2, 28);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	if (d1 > d2)
	{
		cout << "d1>d2" << endl;
	}
	else
	{
		cout << "d1<=d2" << endl;
	}
}
void TestDate11()
{
	Date d1(2024, 2, 28);
	Date d2(2024, 1, 23);
	d1.Print();
	d2.Print();

	if (d1 == d2)
	{
		cout << "d1==d2" << endl;
	}
	else
	{
		cout << "d1!=d2" << endl;
	}
}

void TestDate12()
{
	Date d1(2024, 1, 22);
	Date d2(2024, 1, 22);
	d1.Print();
	d2.Print();

	if (d1 != d2)
	{
		cout << "d1 << endl;
	}
	else
	{
		cout << "d1>=d2" << endl;
	}
}

void TestDate13()
{
	Date d1(2024, 1, 21);
	Date d2(2024, 1, 22);
	d1.Print();
	d2.Print();

	int ret = d1 - d2;
	cout << ret << endl;
}

void TestDate14()
{
	Date d1(2025, 1, 23);
	const Date d2(2024, 1, 22);//const对象不可以调用非const成员函数
	d1.Print();

	//d2.Print();

	//cout << &d1 << endl;
	cout << &d2 << endl;
	
}
void TestDate15()
{
	Date d1;
	Date d2;
	
	cin >> d1;
	cout << d1;
}
int main()
{
	TestDate15();

}

你可能感兴趣的:(c++,开发语言,c++)