网络知识 娱乐 C++——类和对象3|日期类型|Cout运算符重载|Cin运算符重载|const成员|

C++——类和对象3|日期类型|Cout运算符重载|Cin运算符重载|const成员|

目录

日期类型 

Date.h 

Date.cpp 

Test.cpp 

实现Cout运算符重载 

实现Cin运算符重载 

根据日期算星期 

修改后完整代码  

Date.h 

Date.cpp 

const成员

 取地址及const取地址操作符重载


日期类型 

Date.h 

#include
using namespace std;
class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int day = days[month];
		if (month == 2
			&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			day += 1;
		}
		return day;
	}
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print();

	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 day);
	Date& operator++();//默认是前置++,返回+完后的值,用引用返回
	Date operator++(int i);//后前置++,里面要写一个参数,这里的参数可以不用形参接收,括号里直接写int就行,但加了参数也无所谓,这里的参数只是为了区分
	//C++为了区分前后置++,使用重载进行区分,后置++重载增加一个int参数跟前置构成函数重载进行区分
	Date& operator+=(int day);
	Date operator-(int day);
	Date& operator-= (int day);
	Date& operator--();
	Date  operator--(int);
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp 

#include"date.h"
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);
}
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= d);
}
bool Date::operator>=(const Date& d)
{
	return (*this > d) || (*this == d);
}
bool Date::operator d);
}
// 自身要改变
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 ret(*this);//拷贝构造
	//Date ret= *this;在这里,这条语句是拷贝构造,拿*this初始化ret,已经存在的对线才是赋值
	ret += day;
	return ret;//传值返回的时候,会生成一份拷贝
}
void  Date::Print()
{
	cout << _year<<"/" << _month<<"/"<<_day << endl;
}
Date& Date::operator++()//前置
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)//后置,返回++之前的值
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator-= (int day)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year,_month);
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date ret = *this;
	ret -= day;
	return ret;
}
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)
	{
		min = *this;
		max = d;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n*flag;
}

Test.cpp 

int main()
{
	Date d1(2001, 1, 3);
	Date d2(2023, 7, 23);
	
	Date d3(2022, 6, 23);
	cout << (d1 < d2) << endl;
	cout < d2) << endl;
	cout << (d1 == d2) << endl;
	cout << (d1 < d3) << endl;
	cout < d3) << endl;
	cout << (d1 == d3) << endl;
	Date ret1 = d1 + 100;
	ret1.Print();
	Date ret2=++d1;//转换为d1.operator++(&d1)
	ret2.Print();
	Date ret3=d2++;//转换为d1.operator++(&d2,0)
	ret3.Print();
	d1 += 100;
	d1.Print();
	d1 -= 100;
	d1.Print();
	Date ret4 = d1 - 1000;
	ret4.Print();
	int ret5 = d1 - d2;
	cout << ret5 << endl;
	return 0;
}

 

 一个类到底可以重载哪些运算符,要看哪些运算符对这个类型有意义

日期减天数思路:先给天减,如果不够(<=0)就借位

程序有缺陷,构造这里出现了问题

我们在类里面加上判断日期的函数即可或者用assert去判

	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
         //assert(CheckDate());
		if (!CheckDate())
		{
			Print();
			cout << "日期非法" <= 1 && _month 0 && _day > 0 && _day <= GetMonthDay(_year, _month))
			return true;
		else
			return false;
	}

                      

加一个负数会报错,我们只需在+=里面进行调整就行,因为+是通过+=实现的

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

 

同理对-=也修改,如果天数小于0,-=之后 会因为负负得正,给加几天,我们此时调整一下即可

实现Cout运算符重载 

C++ cout和cin能自动识别的原因:

1.库里面写好了运算符重载

2.自动识别类型,它们构成重载函数 

运算符重载:让自定义类型对象可以用运算符,转换成调用这个重载函数

函数重载:支持函数名相同的函数同时存在

虽然俩者都是重载,但它们俩者没有关系

但如果这样使用cout和cin会报错

 此时我们自己在类里面写运算重载符就可以解决这个问题

cout是ostream类型对象,cin是istream类型对象

void Date::operator<<(ostream& out)
{
	out << _year << "-" << _month << _day;
}

此时如果写成这个样子,还是会报错

但这样可以

 运算符有多个操作数的时候,第一个参数是左操作数,第二个参数是右操作数

d1是第一个操作数(左操作数),cout是第二个操作数 (右操作数)

cout<<d1转换为cout.operatpr(d1),所以会报错

若想通过cout<<d1来打印,则不能修改上面的运算符重载,因为类对象抢占了第一个数,默认是this,所以无法通过修改运算符重载来实现该语句

此时把该运算符重载写到外面,当作一个函数,不能当成员函数使用,out就会是第一个操作数

 但会有一个缺陷,私有变量无法访问

 这里可以通过友元进行修改

此时就可以访问这些私有成员

 但此时这样写又会不支持

 这里需要返回值,返回值就是cout,这样才能连续的插入

此时可正常打印

 这个函数可能会被频繁调用,可能输出多行,因此搞成内联函数最好

 但是此时声明在.h定义在.cpp,声明和定义分离,链接时报错

直接在.h里面定义好就行 

实现Cin运算符重载 

cin是istream类型对象,流提取,cout不需要改变Date,但Cin需要改变Date

因为这是要从流里面提取出年月日参数,放到日期里面

 

但此时输入非法日期不报错

 此时在加个判断

根据日期算星期 

 以1年1月1日星期六为参考对象,输入一个日期后,俩日期相减,然后对7取模就行

0相当于周天,1-5对应周一到周六

void DateSwap()
{
	const char* WeekDayToStr[] = { "周一","周二","周三","周四","周五","周六","周日" };
	Date d1;
	Date start(1, 1, 1);
	cout <> d1;
	int n = d1 - start;
	int weekDay = 0;
	weekDay += n;
	cout << WeekDayToStr[ weekDay % 7] << endl;
}

修改后完整代码  

Date.h 

#include
#include
using namespace std;
class Date
{
	friend ostream& operator<>(istream& in, Date& d);
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int day = days[month];
		if (month == 2
			&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			day += 1;
		}
		return day;
	}
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		if (!CheckDate())
		{
			Print();
			cout << "日期非法" <= 1 && _month 0 && _day > 0 && _day (const Date& d);
	bool operator=(const Date& d);
	bool operator<=(const Date& d);
	Date operator+(int day);
	Date& operator++();//默认是前置++,返回+完后的值,用引用返回
	Date operator++(int i);//后前置++,里面要写一个参数,这里的参数可以不用形参接收,括号里直接写int就行,但加了参数也无所谓,这里的参数只是为了区分
	//C++为了区分前后置++,使用重载进行区分,后置++重载增加一个int参数跟前置构成函数重载进行区分
	Date& operator+=(int day);
	Date operator-(int day);
	Date& operator-= (int day);
	Date& operator--();
	Date  operator--(int);
	int operator-(const Date& d);
	
private:
	int _year;
	int _month;
	int _day;
};
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year <<"年" << d._month<< "月" << d._day<<"日" <>(istream & in, Date & d)
{
	assert(d.CheckDate());
		in >> d._year >> d._month >> d._day;
		return in;
}
void DateSwap();

Date.cpp 

#include"date.h"
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);
}
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= d);
}
bool Date::operator>=(const Date& d)
{
	return (*this > d) || (*this == d);
}
bool Date::operator d);
}
// 自身要改变
Date&  Date::operator+=(int day)
{
	if (day  GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_month = 1;
			_year ++;
		}
	}
	return *this;
}
//自身不改变
Date  Date::operator+(int day)
{
	Date ret(*this);//拷贝构造
	//Date ret= *this;在这里,这条语句是拷贝构造,拿*this初始化ret,已经存在的对线才是赋值
	ret += day;
	return ret;//传值返回的时候,会生成一份拷贝
}
void  Date::Print()
{
	cout << _year<<"/" << _month<<"/"<<_day << endl;
}
Date& Date::operator++()//前置
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)//后置,返回++之前的值
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator-= (int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year,_month);
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date ret = *this;
	ret -= day;
	return ret;
}
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)
	{
		min = *this;
		max = d;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n*flag;
}
void DateSwap()
{
	const char* WeekDayToStr[] = { "周一","周二","周三","周四","周五","周六","周日" };
	Date d1;
	Date start(1, 1, 1);
	cout <> d1;
	int n = d1 - start;
	int weekDay = 0;
	weekDay += n;
	cout << WeekDayToStr[ weekDay % 7] << endl;
}

//void Date::operator<<(ostream& out)
//{
//	out << _year << "-" << _month <<"-" << _day;
//}

const成员

 

这种情况下会报错 ,会把const Date转换为Date

这是因为this指针在传参的时候是这样的

参数里的const修饰this指针本身 

d1.Print();传参的时候是将d1的地址传过去,&d1,类型是Date*

d2.Print();&d2 传不过去,因为传过去的是const Date*类型,而形参是Date*类型

d1<d2可以,d2<d1不可以

 

 d1<d2,d1是Date *可以传过去

d2<d1,d2是const Date *不能传给this,所以会报错

如果要改变这种错误,就要给this加上const,Date *const this变为const Date *const this

把const加在这个位置

 但是对于d1,本来是Date *const this变为了const Date *const this 把权限缩小了,这样d1就不能给赋值了

 取地址及const取地址操作符重载

 权限可以缩小,平移,不能放大

这里可以写俩个Print,构成函数重载

 

 编译器在调用的时候会自动匹配,当取地址情况下需要它们同时存在

Date* operator&()
	{
		return this;
	}
	const Date* operator&()const
	{
		return this;
	}

一个返回Date *,一个返回const Date *

 调用相对应的

如果不写编译器会自动生成

 

 这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容

如果不想让别人取地址,一般自己写一个,返回nullptr即可