C++中的运算符重载

本文详细介绍了C++中运算符重载的概念和实现方式,包括友元函数和类成员函数的重载,以及特殊运算符如流运算符、增量和减量运算符的重载。通过实例展示了如何赋予自定义类型数据新的运算功能,并讨论了运算符重载在类设计中的应用。
摘要由CSDN通过智能技术生成

运算符重载定义:赋予运算符具有操作自定义类型数据功能

实质:函数的调用

写法:函数返回值   函数名(函数参数)

函数返回值由运算完成后的值决定    函数名:operator加上重载运算符   参数:看运算符的操作数

友元函数&类成员函数重载运算符

#include <iostream>
using namespace std;

class Complex
{
public:
	Complex() = default;//指定使用默认的无参构造函数
	Complex(int a, int b) :a(a), b(b) {}
	void print()
	{
		cout << a << endl << b;
	}
	friend Complex operator+(Complex A, Complex B)//友元函数运算符重载函数写法,操作符是+
	{
		return Complex(A.a + B.a, A.b + B.b);//返回值类型为Complex
	}
    //类成员函数重载,参数个数等于操作数减一
	bool operator>(Complex A)
	{
		if (this->a > A.a)
		{
			return true;
		}
		if (this->a == A.a && this->b > A.b)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

protected:
	int a;
	int b;
};
int main()
{
	Complex A(2, 3);
	Complex B(7, 8);
	Complex C;
	C = A + B;          //重载函数的隐式调用
	Complex D;
	D = operator+(C, A);//重载函数的显示调用

	if (A > B)//A>B时是bool值
	{
		cout << "A" << endl;
	}
	//对象可以表示一个数据,所以参数应该少一个
	if (B.operator>(C))
	{
		cout << "B" << endl;
	}
	return 0;
}

特殊运算符重载

          流运算符重载(流重载必须采用友元的方式)

                   cin类型:istream类的对象

                   cout类型:ostream类的对象

                   流运算符:   <<    >>

class student
{
public:
	student() = default;
	student(string name, int age) :name(name), age(age) {}
	friend istream& operator>>(istream& in, student& student)
	{//cin是istream类
		cin >> student.name >> student.age;
	}
	friend ostream& operator<<(ostream& out, student& student)
	{//cout是ostream类
		cout << student.name << endl << student.age;
	}
protected:
	string name;
	int age;
};

int main()
{
	string str;
	cin >> str;
	cout << str << endl;
	student A;
	cin >> A;
	cout << A << endl;
	return 0;
}

          ++   --运算符重载

                    前置和后置的问题

                    增加无用参数int来确定++的前置还是后置

class student
{
public:
	student() = default;
	student(string name, int age) :name(name), age(age) {}
	friend ostream& operator<<(ostream& out, student& A)
	{
		cout << A.name << endl << A.age;
		return out;
	}
	const student& operator++(int)//int 为无用参数 充当标记
	{
		return student(name, age++);
	}
     student operator++()
	{
		return student(name, ++age);
	}
	 student operator--(int)
	 {
		 return student(name, age--);
	 }
	 student operator--()
	 {
		 return student(name, --age);
	 }
protected:
	string name;
	int age;
};

int main()
{
	student A("jfhf", 18);
	cout << A << endl;
	int num = 1;
	int result = num++;
	result = num--;

	student B = A++;
	cout << B;
	B = ++A;
	cout << B;
	student C;
	C = --A;
	cout << C << endl;
	C = A--;
	cout << C;
	return 0;
}

             文本重载(新标准中的,有的编译工具不支持),以及类的对象的隐式转换就不写啦

其他运算符:=  () -> [] 只能采用类的成员函数形式重载

   .     .*    ?:    ::  不能重载

案例小int:

#include <iostream>
using namespace std;

class INT
{
public:
	INT() = default;
	INT(int numa) :num(num) {}
	int& data()
	{
		return num;
	}
	string tostr()
	{
		return to_string(num);
	}
	//友元函数重载操作数-1=重载函数的参数个数
	friend INT operator-(const INT& A, const INT& B)
	{
		return(A.num - B.num);
	}
	friend ostream& operator<<(ostream& out, const INT& A)
	{
		out << A.num << endl;
		return out;
	}
	friend istream& operator<<(istream& in, INT& A)
	{
		in >> A.num;
		return in;
	}
    INT operator+(const INT& value)
	{
		return INT(this->num + value.num);
	}
	INT operator+=(const INT& A)
	{
		return INT(this->num + A.num);
	}
	INT operator+=(const int& A)
	{
		return INT(this->num + A);
	}
	INT operator-=(const INT& A)
	{
		return INT(this->num - A.num);
	}
	INT operator-=(const int& A)
	{
		return INT(this->num - A);
	}
	INT operator++(int)
	{
		return INT(this->num++);
	}
	INT operator++()
	{
		return INT(++this->num);
	}
	INT operator--(int)
	{
		return INT(this->num--);
	}
	INT operator--()
	{
		return INT(--this->num);
	}
	INT operator-()
	{
		return INT(-this->num);
	}
	INT operator&(const INT& A)
	{
		return INT(this->num & A.num);
	}
	INT operator^(const INT& A)
	{
		return this->num ^ A.num;
	}
	bool operator!()
	{
		return !this->num;
	}
	bool operator>(const INT& A)
	{
		return this->num>A.num;
	}
	bool operator<(const INT& A)
	{
		return this->num < A.num;
	}
	int* operator&()
	{
		return &this->num;
	}
protected:
	int num = 0;
};
void print(const INT& num)
{
	cout << num << endl;
}
int main()
{
	INT A(99);
	INT B = 88;
	print(A.operator-());
	print(A.operator+(B));
	print(B.operator+=(8));
	return 0;
}

重载个人理解就是自己写一个运算方式把不能进行相关运算的数据进行运算

operator是标志

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值