【c++笔记】运算符重载的实现

双目运算符重载

双目运算符既可以使用成员函数重载,也可以使用全局函数重载,以下代码以加号重载为例

成员函数重载

#include<iostream>
using namespace std;

class Student
{
	public:
		void set_grade();
		void show_grade();
		
		//成员函数重载+号
		Student operator+(Student &s)
		{
			Student temp;
			temp.m_math = this->m_math + s.m_math;
			temp.m_science = this->m_science + s.m_science;
			return temp;
		} 
		
	private:
		int m_math;
		int m_science;
			
};

void Student::set_grade()
{
	cout<<"请输入数学、科学成绩:"<<endl;
	cin>>m_math;
	cin>>m_science;
}

void Student::show_grade()
{
	cout<<"数学成绩:"<<m_math<<endl;
	cout<<"科学成绩:"<<m_science<<endl;
}

void test()
{
	Student s1;
	Student s2;
	s1.set_grade();
	s2.set_grade();
	
	Student s3 = s1 + s2;
	s3.show_grade();
}

int main()
{
	test();
	
	system("pause");
	return 0;
}

全局函数重载

#include<iostream>
using namespace std;

class Student
{
	friend Student operator+(Student &s1,Student &s2);  //友元函数,使得指定的全局函数可以访问类内的私有成。
	public:
		void set_grade();
		void show_grade();
	
	private:
		int m_math;
		int m_science;
			
};

//全局函数重载+号,因为temp是临时对象,在函数结束的时候被释放,故函数不可用引用传递,应采用值传递
Student operator+(Student &s1,Student &s2)   //为增强程序可读性,利于代码维护,建议形参名称和实参名称保持一致
{
	Student temp;
	temp.m_math = s1.m_math + s2.m_math;
	temp.m_science = s1.m_science + s2.m_science;
	return temp;
} 
		
void Student::set_grade()
{
	cout<<"请输入数学、科学成绩:"<<endl;
	cin>>m_math;
	cin>>m_science;
}

void Student::show_grade()
{
	cout<<"数学成绩:"<<m_math<<endl;
	cout<<"科学成绩:"<<m_science<<endl;
}

void test()
{
	Student s1;
	Student s2;
	s1.set_grade();
	s2.set_grade();
	
	Student s3 = s1 + s2;
	s3.show_grade();
}

int main()
{
	test();
	
	system("pause");
	return 0;
}

流插入运算符(<<)重载

只能用全局函数做流插入运算符的重载

#include<iostream>
using namespace std;

class Student
{
	public:
		int m_math;
		int m_science;
		
};

ostream & operator<<(ostream &cout,Student &s)
{
	cout<<"m_math = "<<s.math<<" m_science = "<<s.m_science;
	return cout;
}

void test()
{
	Student s1;
	s1.m_math =11;
	s1.m_science = 22;
	cout<<s1<<endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

单目运算符重载

以递增运算符、成员函数为例
b=++a 前置递增运算符:a先递增,再赋值给b;
b=a++ 后置递增运算符:a先赋值给b,a再递增

前置自增运算符重载

#include<iostream>
using namespace std;

class Student
{
	friend ostream & operator<<(ostream & cout,Student &s);
	public:
		void get_grade();
		Student & operator++()
		{
			m_grade++;
			return *this;
		}
		
	private:
		int m_grade;	
}; 

void Student::get_grade()
{
	cout<<"请输入成绩:"<<endl;
	cin>>m_grade; 
}

ostream & operator<<(ostream & cout,Student &s)
{
	cout<<s.m_grade;
	return cout;
}

void test()
{
	Student s1;
	s1.get_grade();
	cout<<"递增后成绩为:"<< ++(++s1)<<endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

后置自增运算符重载

#include<iostream>
using namespace std;

class Student
{
	friend ostream & operator << (ostream & cout,Student &s);
	public:
		void get_grade();
		
		//后置递增返回值 
		Student operator++(int)
		{
			//先记录当时结果 
			Student temp = *this;
			//递增 
			m_grade++;
			//将最后结果返回 
			return temp;
		}
		
	private:
		int m_grade;	
}; 

void Student::get_grade()
{
	cout<<"请输入成绩:"<<endl;
	cin>>m_grade; 
}

ostream & operator << (ostream & cout,Student &s)
{
	cout<<s.m_grade;
	return cout;
}

void test()
{
	Student s;
	s.get_grade();
	s++;
	cout<<s<<endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

赋值运算符重载

用用户不必进行二者的重载,可直接使用对象间的赋值,但当系统默认的重载不是用户想要的时候,可以自己重载.

参考视频:https://www.bilibili.com/video/BV1Hb411Y7E5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值