你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

c++运算符重载

2021/12/6 3:46:53

运算符重载

  • 什么是运算符重载
  • 友元函数重载运算符
  • 类成员函数重载运算符
  • 特殊运算符重载

什么是运算符重载

  • 什么是运算符重载?
    赋予运算符具有操作自定义类型数据功能

  • 运算符重载的实质是什么?
    运算符重载的实质本身就是函数调用

  • 运算符重载函数的写法

     `函数返回值   函数名(函数参数)`
    
    • 函数返回值 :运算完成后的值决定的 Complex
    • 函数名 : operator 加上重载运算符组成函数名 operator+
    • 参数 :看运算符的操作数,具体参数个数是要看你重载函数形式是什么
    • 函数体 : 写运算符具体想要的操作

友元函数重载运算符

  • 就是将重载函数写为友元函数

  • 参数个数就是操作数个数

  • 显示调用
    operator重载符号(参数1,参数2);

  • 隐式调用
    参数1(重载符号)参数2

类成员函数重载运算符

  • 类的成员函数重载,参数个数是操作数减一
    参数1.operator重载符号(参数2);
#include <iostream>
using namespace std;
//复数
class Complex{
public:
	Complex(int a = 0, int b = 0) :a(a), b(b){}
	void print(){
		cout << a << endl;
		cout << b << endl;
	}
	//友元重载参数个数就是操作数
	friend Complex operator+(Complex A, Complex B){
		return Complex(B.a + A.a, B.b + A.b);
	}
	//类的成员函数重载,参数个数是操作数减一
	bool  operator>(Complex Object){
		if (pow(this->a, 2) + pow(this->b, 2) >
			pow(Object.a, 2) + pow(Object.b, 2)){
			return true;
		}
		else
			return false;
	}
	bool  operator==(Complex Object){
		if (pow(this->a, 2) + pow(this->b, 2) ==
			pow(Object.a, 2) + pow(Object.b, 2)){
			return true;
		}
		else
			return false;
	}
protected:
	int a; //实部
	int b; //虚部
};
int main(){
	Complex A(3, 2);
	Complex B(2, 5);
	Complex C = A + B;
	C.print();
	if (A > B)
		cout << "A的模大于B的模" << endl;
	else if (A==B)
		cout << "A的模等于B的模" << endl;
	else
		cout << "A的模小于B的模" << endl;

	while (1);
	return 0;
}

特殊运算符重载

  • 流运算符重载(必须采用友元的方式重载)
    • cin类型:istream类的对象
    • cout类型:ostream类的对象
    • 流运算<<、>>
#include <iostream>
#include <string>
using namespace std;
class Tihu{
public:
	Tihu(string name="Tihu", int age=19) :name(name), age(age){}
	friend istream& operator>>(istream& in, Tihu& A){
		in >> A.name >> A.age;
		return in;
	}
	friend ostream& operator<<(ostream& out, Tihu& A){
		out << A.name << "\t" << A.age << endl;
		return out ;
	}
protected:
	string name;
	int age;
};
int main(){
	string str="Tihu";
	cout << str << endl;
	Tihu A;
	Tihu B;
	cin >> A >> B;
	cout << A << B<<endl;


	while (1);
	return 0;
}
  • ++、–运算符重载
    -解决前置后置问题:增加了个无用参数,充当标志使用
  • 其他运算符
    • = () -> [] 只能采用类的成员函数形式重载
    • 流重载采用友元方式
    • . .* ?: :: 不能重载
  • 文本重载
#include <iostream>
#include <string>
using namespace std;
class Tihu{
public:
	Tihu(string name= "1", int age = 1) :name(name), age(age){}
	friend ostream& operator<<(ostream& out, Tihu& A){
		out << A.name << "\t" << A.age << endl;
		return out;
	}

	Tihu operator++(int){
		return Tihu(name, age++);
	}
	Tihu operator++(){
		return Tihu(name, ++age);
	}
	Tihu operator--(int){
		return Tihu(name, age--);
	}
	Tihu operator--(){
		return Tihu(name, --age);
	}
	//类的隐式转换
	operator int(){
		return age;
	}
protected:
	string name;
	int age;


};
//文本重载
//unsigned long long operator""_hl(unsigned long long num){
//	return 60 * 60 * num;	""
//}
int main(){
	int num = 3;

	Tihu A("刘雄安伯",10);
	cout << A;
	Tihu result = A++;		  //A 11 10
	num = A;
	cout << "A:" << A << "resule:" <<"num:"<<num<< result;
	result = ++A;			  //A 12 12
	num = A;
	cout << "A:" << A << "resule:" << "num:" << num << result;
	result = A--;			  //A 11 12
	num = A;
	cout << "A:" << A << "resule:" << "num:" << num << result;
	result = --A;			  //A 10 10
	num = A;
	cout << "A:" << A << "resule:" << "num:" << num << result;



	
	while (1);
	return 0;
}