日期类的模拟实现与夺命七问

x33g5p2x  于2022-05-22 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(309)

前言


哈喽大家好,我是鹿 九 丸 \color{red}{鹿九丸}鹿九丸,今天给大家带来的是日期类的模拟实现以及在实现过程中遇到的相关的问题,此处以问答的形式给大家进行展示,这些问题虽小,但是却很耐人寻味,希望大家能够注意到!
如果大家在看我的博客的过程中或者学习的过程中以及在学习方向上有什么问题或者想跟我交流的话可以加我的企鹅号:2361038962 \color{red}{2361038962}2361038962,或者寄邮件到相应的邮箱里:2361038962 @ q q . c o m \color{red}{2361038962@qq.com}2361038962@qq.com,我会尽量帮大家进行解答!

代码实现

头文件Date.h

#pragma once
#include<iostream>
#include<assert.h>
using std::cout;
using std::cin;
using std::endl;
class Date
{
public:
	//获取某年某月的天数
	int GetMonthDay(int year, int month);
	Date(int year = 1, int month = 1, int day = 1)
	{
		if (year >= 1
			&& month <= 12
			&& month >= 1
			&& day <= GetMonthDay(year, month))
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			cout << "日期非法!" << endl;
		}
	}
	//拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	//打印输出函数
	void Print()
	{
		cout << _year << "-" << _month << "-" << _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+(int x);
	//+=运算符重载
	Date& operator+=(int x);
	//-=运算符重载
	Date& operator-=(int x);
	//-运算符重载
	Date& operator-(int x);
	//前置++运算符重载
	Date& operator++();
	//后置++运算符重载
	Date operator++(int i);//C++语法规定的参数中有一个int为后置++
	//-运算符重载
	int operator-(const Date&d );
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp文件

#include"Date.h"
//<运算符重载
bool Date::operator<(const Date& d)
{
	if (_year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//==运算符重载
bool Date::operator==(const Date& d)//因为自动传了this指针
{
	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::GetMonthDay(int year, int month)
{
	assert(year >= 0 && month > 0 && month < 13);
	const static int monthDayArray[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 monthDayArray[month];
}

//+运算符重载
Date Date::operator+(int x)
{
	Date tmp(*this);//一次拷贝构造
	tmp += x;
	return tmp;//一次拷贝构造
}
//+=运算符重载
Date& Date::operator+=(int x)
{
	if (x < 0)
	{
		return *this -= -x;
	}
	_day += x;
	while (_day > GetMonthDay(_year, _month))//大于当前月的最大天数
	{
		_day -= GetMonthDay(_year, _month);//将当前月的天数减去
		_month++;
		if (_month == 13)//月达到了13
		{
			_month = 1;
			++_year;
		}
	}
	return *this;//传引用返回,没有拷贝构造
}
//-=运算符重载
Date& Date::operator-=(int x)
{
	if (x < 0)
	{
		return *this += -x;
	}
	_day -= x;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}
		_day += GetMonthDay(_year, _month);
		
	}
	return *this;
}
//-运算符重载
Date& Date::operator-(int x)
{
	Date tmp(*this);
	tmp -= x;
	return tmp;
}
//前置++重载(d.operaror++();)
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//后置++重载
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
int Date::operator-(const 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)
	{
		++n;
		++min;
	}
	return n * flag;
}

夺命七问

多名第一问

问:在进行+=和+的时候,我们有两种方式,第一种是+=复用+,另一种方式是+复用+=,那么这两种方式哪一种会更好呢?为什么?

第一种:+=复用+

//+运算符的重载
Date Date::operator+(int x)//返回值不能写成引用,因为tmp只是一个临时对象,出了作用域之后就会自动销毁
{
	Date tmp(*this);//一次拷贝
	tmp._day += x;
	while(tmp._day > GetMonthDay(tmp._year, tmp._month))//大于当前月的最大天数
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);//将当前月的天数减去
		tmp._month++;
		if (tmp._month == 13)//月达到了13
		{
			tmp._month = 1;
			++tmp._year;
		}
	}
	return tmp;//一次拷贝
}
//+=运算符重载
Date& Date::operator+=(int x)
{
	*this = *this + x;//调用了一次+有两次拷贝构造
	return *this;//传引用返回,没有拷贝构造
}

在上面的代码中,调用+会有两次拷贝构造重载,如果调用+=,复用+,又会出现两次构造重载,总共是有4次构造函数调用。

第二种:+复用+=

//+运算符重载
Date Date::operator+(int x)//返回值不能写成引用,因为tmp只是一个临时对象,出了作用域之后就会自动销毁
{
	Date tmp(*this);//一次拷贝构造
	tmp += x;
	return tmp;//一次拷贝构造
}
//+=运算符重载
Date& Date::operator+=(int x)
{
	if (x < 0)
	{
		return *this -= -x;
	}
	_day += x;
	while (_day > GetMonthDay(_year, _month))//大于当前月的最大天数
	{
		_day -= GetMonthDay(_year, _month);//将当前月的天数减去
		_month++;
		if (_month == 13)//月达到了13
		{
			_month = 1;
			++_year;
		}
	}
	return *this;//传引用返回,没有拷贝构造
}

在上面的代码中,只有+有2次构造函数调用,所以比较来说+复用+=相对来说构造函数调用次数较少,效率会比较高。

夺命第二问

问:在定义monthDayArray数组的时候,我们为什么要将其定义为静态数组?

答:因为我们会频繁的调用这个函数,定义为静态数组之后就不需要每次调用都在栈区上开辟内存空间了。

夺命第三问

问:在后置++运算符重载中,我们加了一个为int类型的形参,这个是为什么?这个形参必须是int类型的嘛?这个形参是否可以为缺省的?

答:加形参的原因是为了防止和前置冲突。这个形参必须是int类型的,这是编译器强制规定的。这个形参是不可以缺省的,因为我们一旦缺省之后,我们在进行前置或者后置++操作时,编译器就不知道调用哪一个了,就比如下面这个函数:

void func(int a = 10)
{}
void func()
{}

上面是两个函数的定义,因为函数重载的原因,它们能够同时存在,但是在像面这样进行调用的时候就会出错:

func();

因为这个时候编译器调用上面两个函数中的任何一个函数都可以成功调用,此时就会出现问题。

夺命第四问

问:在+运算符重载的时候,我们为什么不能将返回值写成引用的形式?

答:因为我们定义了一个tmp临时对象,该对象的生命周期只是在这个函数内,出了作用域之后就会自动销毁,所以不能将引用作为返回值。

夺命第五问

问:在进行>运算符重载的时候,我们有两种方式:一种是对<=运算符的复用,另一种是对<运算符的复用,第一种不必再多做解释,那么第二种我们发现我们写的时候会出现问题,为什么会出现问题?这种问题该如何解决?

答:首先我们先看我们写出来的代码:

bool Date::operator>(const Date& d)//有两种,一种是对<的复用,另一种是对<=的复用(取反)
{
	return d < (*this);
}
//问:这个地方为什么要进行强制类型转换?
//答:,如果进行类型转换就发生了权限的扩大,所以我们要

因为d的地址类型是const Date,而this指针的类型是Date const,此时在传参的时候,进行了类型转换属于权限的扩大,这是非法操作。**

有两种解决方案:

第一种解决方案

bool Date::operator>(const Date& d)//有两种,一种是对<的复用,另一种是对<=的复用(取反)
{
	return (Date)d < (*this);
}

将d进行类型转换为Date类型的,那么此时d的地址的类型就是Date,在传参时候进行的类型转换(转换为Date const)就属于权限的缩小了,权限的缩小是合法操作

第二种解决方案

修改<运算符的重载:

bool Date::operator<(const Date& d)const 
{
	if (_year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day))
	{
		return true;
	}
	else
	{
		return false;
	}
}

加了之后,this指针的类型就变成了const Date const,此时再进行传参就属于是权限的缩小了。*

夺命第六问

问:在拷贝构造函数的参数中我们为什么要加const?像下面这种情况我们不加const了为什么程序会报错?

Date d2 = d1 + 5;//这个地方调用的是拷贝构造函数,而不是赋值运算符重载函数,为什么?因为这是由一个已知的去初始化一个新创建的变量

答:d1 + 5d1 + 5返回的是一个临时拷贝,具有常量的属性,即类型应该是const Date,当我们执行上面这条语句的时候,就会自动调用拷贝构造函数,但是拷贝构造函数在没有加const的时候类型是Date& d,此时就相当于是将只读权限变为可读可写的权限,属于权限的扩大,自然报错。加const有两个好处:防止报错;可以接收常量右值。

夺命第七问

问:在分文件进行定义类中的成员函数的时候,我们想要将类中的成员函数定义成内联函数,但是我们这样定义之后会报错,这是为什么?有什么比较好的解决方案?

答:因为内联函数是不支持函数的定义和声明的分离的,如果我们非要想要将那些比较短的成员函数定义为内联函数的话,可以将那些比较短的成员函数定义在类中(定义在类中的函数编译器自动默认为是内联函数),这样编译器就会自动将比较短的成员函数默认为是内联函数。

相关文章