C++基础——前后置++--,流插入提取运算符重载函数

 前言:

再来说一说函数运算

        运算符重载函数是用来体现运算符的多态性,在学习C语言的过程中,运算符全都是用来解决内置类型数据、逻辑、关系表达式间的运算,而在学习C++的过程中,我们了解了类与对象,就会思考到自定义类型的对象之间是否也能进行+-*/等多种运算呢?基于此种想法,C++官方研发出了运算符重载函数,就是为了让自定义类型也能做运算,体现出了语言的多态性。

目录

前言

一.前后置++运算重载

代码视图:

测试:

二.前后置--运算重载

测试:

三.流插入运算符重载

代码如图:

优化后,代码如下:

测试: 

此外还有一个问题:

代码如下: 

测试: 

四.流提取运算符重载

代码如下:


一.前后置++运算重载

代码视图:

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

	void Print() {
		cout << _year << "-" << _month << "-" << _day << endl;
	}
    
    //获取月份天数
	int 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& operator+=(int day) {
		_day += day;
		while (_day >  GetMonthDay(_year, _month)) {
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month == 13) {
				_year++;
				_month = 1;
			}
			
		}		return *this;
	}
    
    //前置++运算重载
	Date& operator++() {
		*this+=1;
		return *this;
	}

	//后置为了和前置区分,在后置上约定加个int
	Date operator++(int) {
		Date tmp(*this);			//调用拷贝构造
		*this += 1;
		return tmp;		//返回临时对象,也要调用拷贝构造
	}

private:

	int _year;
	int _month;
	int _day;
};

int main() {
	Date d1(2022, 2, 2);
	Date d2(d1);

	cout << "d1的日期前置结果:" << endl;
	(++d1).Print();	//前置++
	d1.Print();

	cout << "d2的日期后置结果:" << endl;
	(d2++).Print();
	d2.Print();

	return 0;
}

       注意事项:

        使用++运算符重载,需要用到+=的运算重载。

        前置++中,this指针出了作用域已经不在了,但*this(d1)是整个main函数生命周期,仍在,所以用引用返回;而后置++中,tmp是临时拷贝对象,出了作用域不在了,不可用引用返回。

        最后就是C++官方为了区分前置++与后置++的不同,专门为后置++的重载形参处多加了个int,这是约定好的,就好比双方谈好了,已经签了合同了,就不能再改了,所以大家不要以为能够换个char、double、short,这些写法都不对!!! 

 

函数调用图示如下: 

测试:

二.前后置--运算重载

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

	void Print() {
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	//获取月份天数
	int 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& operator-=(int day) {
		_day -= day;
		while(_day<=0) {
			_month--;
			_day += GetMonthDay(_year, _month);
			if (_month == 0) {
				_month = 12;
				_year--;
			}
		}
		return *this;
	}

	Date operator-(int day) {
		Date tmp(*this);		//调用拷贝构造
		tmp -= day;
		return tmp;	//tmp出了作用域就不见了,所以不能用引用返回
	}

	Date& operator--() {
		*this -= 1;
		return *this;
	}
	Date operator--(int) {
		Date tmp(*this);	//调用拷贝构造
		*this -= 1;
		return tmp;			//返回时也要用拷贝构造
	}
private:
	int _year;
	int _month;
	int _day;
};

int main() {
	Date d1(2022, 2, 2);
	Date d2(d1);

	cout << "d1的日期前置--结果:" << endl;
	(--d1).Print();	//前置++
	d1.Print();

	cout << "d2的日期后置--结果:" << endl;
	(d2--).Print();
	d2.Print();
	return 0;
}

前后置的--与++同根同理,就不再过多解释了。 

测试:

        总结:在编写代码的习惯上要常用前置++或前置--,因为后置的总要比前置的多进行两次拷贝,在运行效率上不如前置! 

三.流插入运算符重载
 

        流插入运算符为 “<<",它与cout一起用,作用就是输出变量的值,若要使用cout,就要包含头文件#include<ostream>,cout<<它的一般作用也只是支持内置类型的数值输出例:

#include<iostream>
using namespace std;

int main() {
	int a = 10, b = 20;
	cout << a<<endl;
	cout << b << endl;
	return 0;
}

而我们若是要想输出类的对象呢?这时我们便需要对流插入运算符<<做重载了。

        通过之前对赋值运算符=、==、+=、-=、++、--、<等多个运算符重载的学习来看,我们都是把这些运算符重载函数放到类内,好处就是方便用到this指针和成员变量,毕竟若是私有成员,类外不能访问到,所以我们先放在类内看看:

代码如图:

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
    
    //流插入运算符重载函数
	void operator<<(ostream& out) {
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}


	void Print() {
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:

	int _year;
	int _month;
	int _day;
};

int main() {
	Date d1, d2;
	cout << d1;    //报错
	cout << d2;    //报错


	d1 << cout;	//不报错
    d2<< cout;	//不报错
	return 0;
}

        通过程序的编译,我们发现cout<<d1,cout<<d2处出现了红色波浪线,说明有错误:

而d1<<cout,d2<<cout却可以正常运行输出

        但我们已经写了<<的运算符重载函数了,为什么会出现这种情况?

        之前,我曾经说过,类内的每个成员函数都有一个this指针,只不过它被隐藏起来了,形参中并没有它的身影,而且运算符重载的特性之一就是: 

        注意:成员函数的第一个参数就是隐藏的this指针,那么cout<<d1,这条语句是隐式调用operator<<的写法,而显示调用是:d1.operator<<(cout),实参cout与this指针不匹配,所以

cout<<d1,cout<<d2,编译器并不能识别出来,所以有误。而d1<<cout 等价于cout.operator(d1),实参d1能够与重载函数<<的第一个形参隐藏this指针相对应,所以成功重载,并成功输出。但是没人这样写——d1<<cout,所以为了能让d1.operator(cout)与重载函数对应匹配,且形参不会有this指针,需要把重载函数<<放在类外。

优化后,代码如下:

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

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


	void Print() {
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:

	int _year;
	int _month;
	int _day;
};

 void operator<<(ostream& out ,const Date& d) {
	cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
}
int main() {
	Date d1, d2;
	cout << d1;		//不报错了
	cout << d2;
	return 0;
}

        但此时有了一个老问题,类外函数对于私有成员无法访问,这时,我简单提一提C++的一个新内容——友元,友元的作用就是让类外的函数能够访问到类内的private、protected型成员,C++规定:类中有这两种访问限定修饰符的成员会把类外的一切函数都当作敌人,只有身上被标识过友元的函数,它们才会认为这是朋友并让其有权限进行访问。这就是友元的作用。

        而成为友元的方式就是在类中写一句友元函数声明! 

        friend void operator<<(ostream& out, const Date& d);

        这样处在类外的operator<<函数就能够对私有成员进行访问了,从而cout<<d1,cout<<d2也就可以正常运行了。 

测试: 

此外还有一个问题:

        上面的cout<<d1这句只支持单次访问一个对象,若是想进行链式访问呢?

代码如下: 

class Date
{
    //友元声明
	friend ostream& operator<<(ostream& out, const Date& d);
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

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


	void Print() {
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:

	int _year;
	int _month;
	int _day;
};

//有引用返回,支持链式访问
ostream& operator<<(ostream& out, const Date& d) {
	cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
int main() {
	Date d1(2022,10,11);
	Date d2(2020, 12, 12);
	Date d3(1999, 1, 15);
	cout << d1 <<d2 << d3 << endl;
	
	return 0;
}

测试: 

四.流提取运算符重载

        流提取运算符为>>,其写法与operator<<有着异曲同工之妙,它与cin结合使用,而使用cin需要用到头文件#include<istream>,所以写法上只需要把ostream改为istream即可。

代码如下:

class Date
{
	friend istream& operator>>(istream& in,  Date& d);
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}


	void Print() {
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:

	int _year;
	int _month;
	int _day;
};

//有引用返回,支持链式访问

istream& operator>>(istream& in, Date& d) {
	in >> d._year >> d._month >> d._day;
	return in;
}

int main() {
	Date d1,d2,d3;
    cin >>d1;
    //链式访问
	cin >>d2 >> d3;
	
	return 0;
}

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
下面是一个基本的 C++ 电子时钟实现,其中重载了 + 和 - 运算符: ```c++ #include <iostream> using namespace std; class Clock { private: int hour, minute, second; public: Clock(int h = 0, int m = 0, int s = 0) { hour = h; minute = m; second = s; } void display() { cout << hour << ":" << minute << ":" << second << endl; } Clock operator+(const Clock& c) const { int h = hour + c.hour; int m = minute + c.minute; int s = second + c.second; if (s >= 60) { s -= 60; m++; } if (m >= 60) { m -= 60; h++; } if (h >= 24) { h -= 24; } return Clock(h, m, s); } Clock operator-(const Clock& c) const { int h = hour - c.hour; int m = minute - c.minute; int s = second - c.second; if (s < 0) { s += 60; m--; } if (m < 0) { m += 60; h--; } if (h < 0) { h += 24; } return Clock(h, m, s); } }; int main() { Clock c1(10, 30, 45), c2(12, 50, 15); cout << "c1 = "; c1.display(); cout << "c2 = "; c2.display(); Clock c3 = c1 + c2; cout << "c3 = "; c3.display(); Clock c4 = c2 - c1; cout << "c4 = "; c4.display(); return 0; } ``` 在上面的代码中,Clock 类具有小时、分钟和秒属性,并且有一个默认构造函数。display() 函数用于显示当前时间。operator+() 和 operator-() 分别重载了 + 和 - 运算符,以使 Clock 对象可以相加和相减。 在主函数中,我们创建了两个 Clock 对象 c1 和 c2,并显示了它们的值。然后我们通过重载的 + 运算符将它们相加,将结果存储在 c3 中,并将其显示出来。接下来,我们通过重载的 - 运算符将 c2 从 c1 中减去,将结果存储在 c4 中,并将其显示出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙予清的zzz~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值