• 【C++】运算符的重载


    一.重载运算符加减乘除“+”“-“*”“/”

    函数类型 operator 运算符名称(形参表)
    	{对运算符的重载处理}
    
    • 1
    • 2

    例题1:复数类进行加减乘除

    复数运算相关公式

    1.加法运算
    (a+bi)+(c+di)=(a+c)+(b+d)i

    2.减法运算
    (a+bi)-(c+di)=(a-c)+(b-d)i

    3.乘法运算
    (a+bi)(c+di)=(ac-bd)+(bc+ad)i

    4.除法运算
    (a+bi)/(c+di)=(ac + bd)/(c^2 + d ^2) +((bc - ad)/(c ^2 + d ^2)) i

    #include
    using namespace std;
    class Complex {
    public:
    	Complex(double a, double b) :real(a), imag(b) {}
    	void display();
    	double get_real();
    	double get_imag();
    	Complex operator+(Complex&);
    	Complex operator-(Complex&);
    	Complex operator*(Complex&);
    	Complex operator/(Complex&);
    private:
    	double real;
    	double imag;
    };
    Complex Complex::operator+(Complex& c) {
    	return Complex(this->get_real() + c.get_real(), this->get_imag() + c.get_imag());
    }
    Complex Complex::operator-(Complex& c)
    {
    	return Complex(this->get_real() - c.get_real(), this->get_imag() - c.get_imag());
    }
    Complex Complex::operator*(Complex& c)
    {
    	return Complex(this->get_real() * c.get_real()- this->get_imag() * c.get_imag(), this->get_imag() * c.get_real()+this->get_real() * c.get_imag());
    }
    Complex Complex::operator/(Complex& c)
    {
    	return Complex((real * c.get_real() + imag * c.get_imag())/(pow(c.get_real(),2)+(pow(c.get_imag(), 2))),
    		(imag * c.get_real() - real * c.get_imag()) / (pow(c.get_real(), 2) +(pow(c.get_imag(), 2))));
    }
    void Complex::display() {
    	cout << "(" << real << "," << imag << "i)" << endl;
    }
    double Complex::get_real() {
    	return real;
    }
    double Complex::get_imag() {
    	return imag;
    }
    int main() {
    	Complex c1(3, 4);
    	Complex c2(5, -10);
    	Complex c3 = c1 + c2;
    	c3.display();//(8,-6i)
    	c3 = c1 - c2;
    	c3.display();//(-2,14i)
    	c3 = c1 * c2;
    	c3.display();//(55,-10i)
    	c3 = c1 / c2;
    	c3.display();//(-0.2,0.4i)
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    例题2:求两个复数之和(c1+c2)、整数和复数之和(c+i或者i+c)

    #include
    using namespace std;
    class Complex {
    public:
    	Complex(double a, double b) :real(a), imag(b) {}
    	void display();
    	double get_real();
    	double get_imag();
    	Complex operator+(int &i);
    	Complex operator+(Complex &c);
    	friend Complex operator+(int &,Complex&);
    private:
    	double real;
    	double imag;
    };
    double Complex::get_real() {
    	return real;
    }
    double Complex::get_imag() {
    	return imag;
    }
    Complex Complex::operator+(int& i) {
    	return Complex(real + i, imag);
    }
    Complex Complex::operator+(Complex &c) {
    	return Complex(real + c.get_real(), imag+c.get_imag());
    }
    void Complex::display() {
    	cout << "(" << real << "," << imag << "i)" << endl;
    }
    Complex operator+(int& i, Complex& c)
    {
    	return Complex(c.get_real() + i, c.get_imag());
    }
    int main() {
    	Complex c1(3, 4);
    	Complex c2(5, -10);
    	int i = 5;
    	Complex c3 = c1 + c2;
    	c3.display();//(8,-6i)
    	c3 = c1 + i;
    	c3.display();//(8,4i)
    	c3 = i + c1;
    	c3.display();//(8,4i)
    	return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    二.重载流插入运算符“<<”和流提取运算符“>>”

    只能作为友元函数,不能将它们定义为成员函数
    【个人理解:统一写成非成员函数可能是怕原来的流运算符会调用类库里的 ,无法重载自己定义的】

    例题:输入两个矩阵,输出两个矩阵的和

    istream& operator>>(istream &,自定义类 &);
    ostream& operator<<(ostream &,自定义类 &);
    
    • 1
    • 2
    #include
    using namespace std;
    class Matrix {
    public:
    	Matrix();
    	friend ostream& operator<<(ostream& out,Matrix &m);
    	friend istream& operator>>(istream& input, Matrix& m);
    	friend Matrix operator+(Matrix& a, Matrix& b);
    private:
    	int a[2][3];
    };
    
    //重载运算符“++”————矩阵的相加
    Matrix operator+(Matrix& x, Matrix& z) {
    	Matrix t;
    	for (int i = 0;i < 2;i++) {
    		for (int j = 0;j < 3;j++) {
    			t.a[i][j] = x.a[i][j] + z.a[i][j];
    		}
    	}
    	return t;
    }
    //重载运算符“<<”————矩阵的输入
    ostream& operator<<(ostream& out, Matrix& m) {
    	for (int i = 0;i < 2;i++) {
    		for (int j = 0;j < 3;j++) {
    			out << m.a[i][j] << " ";
    		}
    		out << endl;
    	}
    	return out;
    }
    //重载运算符“<<”————矩阵的输出
    istream& operator>>(istream& in, Matrix& m) {
    	for (int i = 0;i < 2;i++) {
    		for (int j = 0;j < 3;j++) {
    			in >> m.a[i][j];
    		}
    	}
    	return in;
    }
    //构造函数
    Matrix::Matrix() {
    	for (int i = 0;i < 2;i++) {
    		for (int j = 0;j < 3;j++) {
    			a[i][j] = 0;
    		}
    	}
    }
    
    
    int main() {
    	Matrix m1, m2, m3;
    	cin >> m1 >> m2;
    	m3 = m1 + m2;
    	cout << m3 << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    三.重载类型转换符

    将一个类的对象转换成另一类型的数据。

    • 函数名前不能指定函数类型,函数没有参数
    • 类型转换函数只能作为成员函数,因为转换的主体是本类的对象,不能作为友元函数或普通函数。
    operator 类型名
    	{实现转换的语句}
    
    • 1
    • 2

    例题:处理一个复数和一个double数相加的运算

    #include
    using namespace std;
    class Complex {
    public:
    	Complex() {
    		real = 0;
    		imag = 0;
    	}
    	Complex(double r) {
    		real = r;
    		imag = 0;
    	}
    	Complex(double a, double b) :real(a), imag(b) {}
    	void display();
    	double get_real();
    	double get_imag();
    	operator double() { return real; }//重载类型转换符
    private:
    	double real;
    	double imag;
    };
    void Complex::display() {
    	cout << "(" << real << "," << imag << ")" << endl;
    }
    int main() {
    	Complex c1(3, 4), c2;
    	double d;
    	d = 2.5 + c1;
    	cout << d << endl;
    	c2 = Complex(d);
    	c2.display();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    【云原生之Docker实战】使用Docker部署Flarum开源论坛
    一个简单的Python案例教学,用商品评论来做词云分析
    使用 Vue.js 和 Element Plus 实现自动完成搜索功能
    ASEMI整流桥GBL610参数,GBL610尺寸,GBL610特征
    【Vue全家桶】Vuex状态管理
    前缀、中缀、后缀表达式相互转换工具
    1-2二分查找
    Hadoop_HDFS
    MATLAB programming interface for STK software stkInit()
    批量删除docker中tag为<none>的镜像
  • 原文地址:https://blog.csdn.net/weixin_45867159/article/details/127365750