当前位置: 首页 > news >正文

实现一个日期类(类和对象实践项目)

1.变量定义

对于一个日期的记录我们需要年,月,日来进行表示,所以我们定义_year,month,day,这三个变量来进行表示。

我们同时还需要定义三个我文件,一个test.cpp,Date.h,Date.cpp。

我们类的定义和函数声明放在Date.h头文件里面。

我们函数的定义放在Date.cpp里面。

实现一个函数功能后放在test.cpp里面进行测试查看是否正确。

class Date
{	
private:
	int _year;
	int _month;
	int _day;
};

由于函数的成员变量是不可以直接访问的所以我们设置为私有。 

2.功能函数(操作符重载)

日期类里面的功能需要设计日期比较,日期推算,操作符+的重载,-的重载,++,--,流插入,流提取,=,-=,+=,>,<,<=,>=,!=,==,对这些操作符的重载。 

2.1拷贝构造

由于我们日期类的构造都是使用的内置类型没有自定义类型所以只需要使用构造函数就可以满足了

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

2.2日期比较大小

我们再进行日期比较大小时只需要写两个操作符重载然后进行复用就可以了。我们需要实现一个<的操作符重载和一个==的操作符重载就可以了,其他的操作符重载就进行这两操作符进行复用就可以了。

bool Date::operator<(const Date& d)
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_month < d._month)
	{
		return true;
	}
	else if (_day < d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator>(const Date& d)
{
	return !(*this < d) && (*this != d);
}
bool Date::operator<=(const Date& d)
{
	return (*this < d) || (*this == d);
}
bool Date::operator>=(const Date& d)
{
	return (*this > d) || (*this == d);
}
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);
}

Date& Date::operator=(Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
	return *this;

}

2.3日期加减操作

我们要实现一个用日期加上一个天数的方式来获得一个日期,或者减去一个日期的方式来获得一个日期。

先来实现+=

Date& Date::operator+=(int day)
{
	int MonthArr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

	_day += day;

	while (_day > MonthArr[_month])
	{
		MonthArr[2] = 28;
		if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0)
		{
			MonthArr[2] = 29;
		}

		if (_day > MonthArr[_month])
		{
			_day -= MonthArr[_month];
			_month++;
		}
		else if (_month > 12)
		{
			_year++;
		}
	}

	return *this;
}

-=:

Date& Date::operator-=(int day)
{
	int MonthArr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

	_day -= day;

	while (_day <1)
	{
		MonthArr[2] = 28;
		if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0)
		{
			MonthArr[2] = 29;
		}
		if (_day < 1)
		{
			_month--;
			_day += MonthArr[_month];			
		}
		else if (_month < 1)
		{
			_year--;
		}
	}

	return *this;
}

我们有了+=和-=来实现加就太容易了,可以直接套用+=,-=。我们要获得一个日期减后(加后)的一个新日期我们的返回值就不能再使用引用了的方式了,这样的话我们的新变量只是以前的一个别名,会改变以前的那个变量。所以会使用他的拷贝构造。

+和-:

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

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

2.4++和--的操作

我们有了+=可以使用+=来实现++,就是对this*+=1,但是我们这样实现出来的是前置++,要想实现后置++就需要在后面函数传参的地方加一个int。这样才可以实现后置++。--是同理的。

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

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

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

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

2.5流插入,流提取操作符重载

我们需要对类里面的私有变量进行操作的时候我们需要流插入流提取的变量,但是放在类里面会将函数的this指针当作是第一个变量,我们把函数放在类外面的时候有访问不到私有变量。我们就要使用友元函数来对其声明。

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;
}

3.完整代码展示

完整Gitee代码链接https://gitee.com/he-minju/c---project/blob/master/2005_2_27/2005_2_27

Date.h

#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 = 1900, int month = 1, int day = 1);

	void DatePrintf()const
	{
		cout << this->_year << "-" << this->_month << "-" << this->_day << endl;

	}

	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);

	//ֵ
	Date& operator=(Date& d);
	//+=
	Date& operator+=(int day);
	//-=
	Date& operator-=(int day);
	//+
	Date operator+(int day);
	//-
	Date operator-(int day);

	//ǰ++
	Date& operator++();
	
	//ǰ--
	Date& operator--();
	
	//++
	Date operator++(int);
	
	//--
	Date operator--(int);

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

 Date.cpp

#include"Date.h"



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

bool Date::operator<(const Date& d)
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_month < d._month)
	{
		return true;
	}
	else if (_day < d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator>(const Date& d)
{
	return !(*this < d) && (*this != d);
}
bool Date::operator<=(const Date& d)
{
	return (*this < d) || (*this == d);
}
bool Date::operator>=(const Date& d)
{
	return (*this > d) || (*this == d);
}
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);
}

Date& Date::operator=(Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
	return *this;

}

Date& Date::operator+=(int day)
{
	int MonthArr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

	_day += day;

	while (_day > MonthArr[_month])
	{
		MonthArr[2] = 28;
		if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0)
		{
			MonthArr[2] = 29;
		}

		if (_day > MonthArr[_month])
		{
			_day -= MonthArr[_month];
			_month++;
		}
		else if (_month > 12)
		{
			_year++;
		}
	}

	return *this;
}

Date& Date::operator-=(int day)
{
	int MonthArr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

	_day -= day;

	while (_day <1)
	{
		MonthArr[2] = 28;
		if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0)
		{
			MonthArr[2] = 29;
		}
		if (_day < 1)
		{
			_month--;
			_day += MonthArr[_month];			
		}
		else if (_month < 1)
		{
			_year--;
		}
	}

	return *this;
}


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

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


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

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

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

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

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;
}

相关文章:

  • 使用 potrace.js实现图像矢量化教程
  • Windows控制台函数:标准输入输出流交互函数GetStdHandle()
  • 基于Spring Boot的城市垃圾分类管理系统的设计与实现(LW+源码+讲解)
  • 使用 Python 开发的简单招聘信息采集系统
  • 人工智能里的深度学习指的是什么?
  • Next.js 的基本了解
  • 【工具使用】IDEA 社区版如何创建 Spring Boot 项目(详细教程)
  • 蓝耘赋能通义万相 2.1:用 C++ 构建高效 AI 视频生成生态
  • CSS定位布局-五个定位实现自由布局(Static, Relative, Absolute, Fixed, Sticky)
  • 力扣刷题DAY8(动态规划)
  • C/C++实现显微镜玻片球状细胞识别与计数
  • 计算机组成原理(第三章 存储系统)
  • 【自学笔记】R语言基础知识点总览-持续更新
  • 爬虫案例六用协程爬取趣笔阁
  • 13.【线性代数】——复习课
  • MyBatis增删改查:静态与动态SQL语句拼接及SQL注入问题解析
  • 如何选择开源向量数据库
  • XPath 语法无法定位到 svg 标签
  • Vue源码解析之mustache模板引擎
  • nodejs express设置允许跨域示例
  • 南京做网站的/企业邮箱账号
  • 上海市建设协会考试网站/推广链接点击器网页
  • 给人做网站赚钱/seo优化教程自学网
  • 小榄网站建设公司/南京seo外包
  • 怎么查看网站外链效果/自己有网站怎么推广
  • 学而思的网站哪里做的/最近在线直播免费观看