C++之运算符重载

本文详细介绍了C++中的运算符重载,包括算术运算符、流输出运算符、递增/递减运算符、赋值运算符、关系运算符以及函数调用运算符的重载。通过实例展示了如何实现这些运算符的自定义行为,并解释了重载过程中需要注意的内存管理和深拷贝问题。此外,还讨论了运算符重载在类对象操作中的重要性。

目录

1.算数运算符重载p1+p2

2.重载运算符<<

3.递增运算符重载

4.递减运算符重载

5.赋值运算符重载

6.关系运算符重载

7.函数调用运算符重载


1.算数运算符重载p1+p2

#include<iostream>
#include<string>
using namespace std;

class Student
{
public:
	Student(double a,double b,double c):Chinese(a),Math(b),English(c){}
	//Student operator+(const Student& s) {//通过成员函数调用
	//	Student temp(0,0,0);
	//	temp.Chinese = Chinese + s.Chinese;
	//	temp.Math = Math + s.Math;
	//	temp.English = English + s.English;
	//	return temp;
	//}
public:
	double Chinese;
	double Math;
	double English;
};
//通过全局函数调用
Student operator + (const Student & s_1, const Student & s_2)
{
	Student temp(0,0,0);
	temp.Chinese = s_1.Chinese + s_2.Chinese;
	temp.Math = s_1.Math + s_2.Math;
	temp.English = s_1.English + s_2.English;
}
int main() {
	Student s1(98, 99, 85);
	Student s2(90, 99, 85);
	Student s3 = s1 + s2;
	cout << s3.Chinese << endl;
	cout << s3.Math<< endl;
	cout << s3.English << endl;
	system("pause");
	return 0;
}

2.重载运算符<<

#include<iostream>
#include<string>
using namespace std;

class Student
{
	friend ostream& operator<<(ostream& cout, Student& s);

public:
	Student(double a,double b,double c):Chinese(a),Math(b),English(c){}
private:
	double Chinese;
	double Math;
	double English;
};
//通过全局函数调用
ostream& operator<<(ostream& cout, Student& s)
//1.如果在成员函数种调用,格式应该是operator<<( cout),形式简写为p.operator<<(cout),等价于p<<cout
//因此,不能在成员函数中调用
//2.接收cout的类型是输出流,属于ostream类,而流传递只能使用引用,不能说再创建一个新对象
//3.由于你要多个<<,也就是链式编程,因此函数调用完返回值仍应该是cout,所以返回类型是ostream,
//而且你还不能返回的值,是一个新的cout,所以要以引用的方式返回
{
	cout << s.Chinese << s.Math << s.English << endl;
	return cout;
}

int main() {
	Student s1(98, 99, 85);
	cout << s1;//通过调用函数的形式,进行重载输出运算符
	system("pause");
	return 0;
}

3.递增运算符重载

#include<iostream>
#include<string>
using namespace std;

class  MyInteger
{
	friend ostream& operator<<(ostream& cout, MyInteger p);
public:	
	MyInteger(int m);
	MyInteger& operator++();
	MyInteger operator++(int);//后置运算符,用int站位

private:
	int My_int;
};

MyInteger::MyInteger(int m)
{
	My_int=m;
}
MyInteger& MyInteger::operator++()
{
	My_int++;
	return *this;
}

MyInteger MyInteger::operator++(int)
{
	MyInteger temp=*this;
	++*this;
	return temp;
}

ostream& operator<<(ostream& cout, MyInteger p)
{
	cout << p.My_int << endl;
	return cout;

}

int main() {
	
	MyInteger p(5);
	cout << ++p << endl;
	cout << p << endl;
	cout << p++ << endl;
	cout << p << endl;
	system("pause");
	return 0;
}

4.递减运算符重载

#include<iostream>
#include<string>
using namespace std;

class  MyInteger
{
	friend ostream& operator<<(ostream& cout, MyInteger p);
public:	
	MyInteger(int m);
	MyInteger& operator--();
	MyInteger operator--(int);//后置运算符,用int占位

private:
	int My_int;
};

MyInteger::MyInteger(int m)
{
	My_int=m;
}
MyInteger& MyInteger::operator--()//前置运算符
{
	My_int--;
	return *this;
}

MyInteger MyInteger::operator--(int)//后置运算,因为你返回的是一个局部变量,所以不再用引用返回,
//但就不再能执行链式编程了,比如(p--)--
{
	MyInteger temp=*this;
	--*this;
	return temp;
}

ostream& operator<<(ostream& cout, MyInteger p)
{
	cout << p.My_int << endl;
	return cout;

}

int main() {
	
	MyInteger p(5);
	cout << --p << endl;
	cout << p << endl;
	cout << p-- << endl;
	cout << p << endl;
	system("pause");
	return 0;
}

5.赋值运算符重载

//调用编译器默认的赋值运算符可能出现的问题
#include<iostream>
#include<string>
using namespace std;

class Student
{
public:
	Student(int s);
	~Student();
	int *score;
};

Student::Student(int s)
{
	score = new int(s);
}

Student::~Student()
{
	if (score != NULL)
	{
		delete score;
		score = NULL;
	}
}

void fun()
{
	Student s1(98);
	Student s2(97);
	s2 = s1;//可以运行,但是问题在哪?
	//调用默认的赋值运算符,但如果成员属性中有在堆区开辟的,那么在人为释放的时候就会出现问题
	//浅拷贝堆区空间被重复释放。
	cout << *s1.score << endl;
	cout << *s2.score << endl;
}


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

可以利用深拷贝进行赋值运算符重载

#include<iostream>
#include<string>
using namespace std;

class Student
{
public:
	Student(int c, int m);
	~Student();
	Student& operator=(Student& s1);
	
	int* Chinese;
	int *Math;
};

Student::Student(int c,int m)
{
	Chinese = new int(c);
	Math = new int(m);
}

Student::~Student()
{
	if (Chinese != NULL || Math != NULL)
	{
		delete Chinese;
		Chinese = NULL;
		delete Math;
		Math = NULL;
	}
}

Student& Student::operator=(Student &s)
{
	if (Chinese != NULL || Math != NULL)//先把之前内存中存的地址删掉
	{
		delete Chinese;
		Chinese = NULL;
		delete Math;
		Math = NULL;
	}

	Chinese = new int(*s.Chinese);
	Math = new int(*s.Math);
	//如果只是针对s2=s1赋值,那么可以说,已经可以了,s2已经完全赋值完毕了,但是为了更完整的接近
	//s3=s2=s1,这种你如果还是void类型,这时s3=void,肯定不行,一定还要返回一个Student类
	//而且是以引用返回
	return *this;

}

void fun()
{
	Student s1(100,100);
	Student s2(98,98);
	Student s3(99, 99);
	s3=s2 = s1;//可以运行,但是问题在哪?
	//调用默认的赋值运算符,但如果成员属性中有在堆区开辟的,那么在人为释放的时候就会出现问题
	//浅拷贝堆区空间被重复释放。
	cout << *s2.Chinese << endl;
	cout << *s2.Math << endl;
	cout << *s3.Chinese << endl;
	cout << *s3.Math << endl;
}


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

6.关系运算符重载

//代码给的是==重载,比如!=、>、<都可以根据这个案例进行修改
#include<iostream>
#include<string>
using namespace std;

class Student
{
public:
	Student(string s,int c,int m,int e);//之前有错误,我把引用去掉之后就没错误了
	bool operator==(Student &stu);
public:
	string s_name;
	int Chinese;
	int Math;
	int English;
};
Student::Student(string s, int c, int m, int e)
{
	s_name = s; Chinese = c; Math = m; English = e;
}

bool Student::operator==(Student&stu )
{
	if ((Chinese == stu.Chinese) && (Math == stu.Math) && (English = stu.English))
		return true;
	else
		return false;

}

void fun()
{
	Student s1("张三",98,99,100);
	Student s2("李四", 98, 99, 100);
	if (s1 == s2)
		cout << "s1和s2相等!" << endl;
	else
		cout << "s1和s2不相等!" << endl;

}


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

7.函数调用运算符重载

#include<iostream>
#include<string>
using namespace std;


class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void fun()
{
	MyAdd add;
	int ret = add(10, 10);
	cout << "ret = " << ret << endl;

	//匿名对象调用  
	cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {

	fun();

	system("pause");

	return 0;
}

//区别于函数调用,函数调用是函数名+实参列表
//函数调用运算符重载是类对象+实参列表
//第21行给出了匿名对象的调用

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值