【C++】类和对象——流插入和流提取运算符重载


请添加图片描述

前言

  我们在上一节实现日期类时,在输入和输出打印时,经常会调用两个函数:

void Insert()//输入函数
	{
		cin >> _year;
		cin >> _month;
		cin >> _day;
		if (!CheakDay())
			cout << "输入错误,请重新输入" << endl;
	}
void Print()//输出函数
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}

  我们经常调用这两个函数进行输入输出日期,我会觉得麻烦,那我可不可以直接使用cout和cin来输出输入呢,这就用到我们流插入和流提取运算符的重载。

ostream和istream

  在cplusplus网站中,就有详细介绍:
在这里插入图片描述
其实,cout是ostream类型的全局对象,cin是一个istream类型的全局对象。这些都是在C++的标准库中写好了,它们被包含在iostream这个头文件里面。
  ostreamistream里面都写了很多函数,想要输入或者输出带精度的都可以进行调用。
  我们都知道,我们可以直接调用cout来输出内置类型,是因为它已经在库中写好了重载函数,如下图所示:
在这里插入图片描述

例如:
int i=1;
cout<<i ——》等价于cout.operator<<(i)
double d=1.1
cout<<d ——》等价于cout.operator<<(d)
cout能自动识别类型,本质上是因为这些流插入重载自动构成函数重载。
cin也一样。

  当我们想要cout一个自定义类型,即cout<<d1,发现代码会报错,因为库里面没有对应写自定义类型的输出,我们要自己重载写一个。

自定义类型的流插入重载

  虽然上图中函数的参数只有一个,但我们要知道的是operator<<是写在ostream这个类里面的,所以这个函数应该是有两个参数,一个是隐藏的this指针,所以实际上库里声明定义的重载应该为:

ostream& operator<<(ostream& this, int val);

  当我们仿照其写自定义类型的流插入重载函数时,ostream& this,这个参数是不能省略的。
  明白了这个,现在我们在日期类中类中声明流插入重载函数:

void operator<<(ostream& out);
//必须用引用传参是因为ostream类型不支持拷贝构造
//(传参时如果传的自定义类型会调用它的拷贝构造)

  在类外定义这个函数时:

void Date::operator<<(ostream& out)
{
    out << _year << "年" << _month << "月" << _day << "日" << endl;
}

  我们重载的是自定义类型,但自定义类型内部最终还是内置类型。

out << _year << “年” << _month << “月” << _day << “日” << endl;
这一行其实是多个函数的调用,
先执行out<<_year,
它会调用库里的函数:ostream& operator<< (int val);输出
ostream&也就是out作为返回值又变成:out << “年” << _month << “月” << _day << “日” << endl;然后再输出。

到此刻,我们调用cout<<d1时发现会报错,这是为什么呢?

我们将调用void Date::operator<<(ostream& out)这个函数的式子写出来其实是: d1.operator<<(cout);写成这样就可以正常调用。
即d1<<cout。

  其实原因很简单:在运算符重载过程中,参数顺序和操作数顺序必须保持一致。
在这里插入图片描述

我们实际想要写成:cout<<d1,
则参数顺序则应该为:ostream Date
但是存在一个问题,这个函数是Date类中的成员函数,它有隐含一个this指针,把第一个参数占用了,ostream则不能作为第一个参数。此时我们只能将其重载成全局函数。

总结起来就是:operator<<想重载为成员函数,可以,但是使用时d1<<cout不符合习惯,建议重载成全局函数。
如下:

void operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
}

在这里插入图片描述
  此时会出现一个私有不可访问的问题,为方便使用,我们先将私有成员变量设为公有。此时可以正常使用,但当我们想连续输出时,如:

cout << d1 << d2;

  此时编译器又会继续报错。因为函数调用会先调用cout << d1,此时没有返回值,所以会报错,我们应该有个返回值,且这个返回值应该是cout,才能使得表达式继续执行,变为cout << d2,就可以连续输出,又因为out是cout的引用,即out是cout的别名,只要返回out即可,所以函数可改为:

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

自定义类型的流提取重载

  与自定义类型的流插入重载一致,就不再细说,直接得代码:

//函数的声明
istream & operator>>(istream & in, Date & d);
//函数的定义
istream& operator>>(istream& in, Date& d)
{
	cout << "请依次输入年月日:" << endl;
	in >> d._year >> d._month >> d._day;
	return in;
}

  这时我们也可以将上节课写的日期检查写入:

istream& operator>>(istream& in, Date& d)
{
	cout << "请依次输入年月日:" << endl;
	in >> d._year >> d._month >> d._day;
	if (!d.CheakDay())
	{
		cout << "日期非法" << endl;
	}
	return in;
}

解决私有问题

  将函数在类中声明为友元函数,这在类和对象终章会讲到。
  代码如下:

class Date
{
    //声明友元函数
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
istream& operator>>(istream& in, Date& d)
{
	cout << "请依次输入年月日:" << endl;
	in >> d._year >> d._month >> d._day;
	if (!d.CheakDay())
	{
		cout << "日期非法" << endl;
	}
	return in;
}

  声明函数是类的朋友,则函数可以访问类中的所有成员,友元的语法就是这么简单。

日期类总接口

  补充了这两个流的重载,我们可以将Date.h完善:

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year, int month, int day)
		:_year(year)
		, _month(month)
		, _day(day)
	{
		if (!CheakDay())
		{
			cout << "日期非法" << endl;
		}
	}
	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);
		static int MonthDayArr[] = { 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;
		else
			return MonthDayArr[month];
	}

	bool CheakDay()
	{
		if (_year <= 0 || _month <= 0 || _month > 12 || _day<0 || _day>GetMonthDay(_year, _month))
			return false;
		else
			return true;
	}
	bool operator<(const Date& d)const;
	bool operator<=(const Date& d)const;
	bool operator>(const Date& d)const;
	bool operator>=(const Date& d)const;
	bool operator==(const Date& d)const;
	bool operator!=(const Date& d)const;

	Date& operator+=(int n);
	Date operator+(int n);

	Date& operator-=(int n);
	Date operator-(int n);

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

	int operator-(const Date& d)const;
private:
	int _year;
	int _month;
	int _day;
};

请添加图片描述

评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值