美文网首页
运算符重载

运算符重载

作者: arkliu | 来源:发表于2022-11-25 08:54 被阅读0次

操作符重载

  • C++不允许用户自己定义新的运算符,只能对已有的 C++运算符进行重载


    image.png
  • 运算符重载函数可以是类的成员函数,也可以是类的友元函数,还可以是既非
    类 的成员函数也不是友元函数的普通函数

操作符重载基本语法

#include <iostream>

using namespace std;

class Complex {
    private:
        int m_a;
        int m_b;
    public:
        Complex(int a, int b) {
            m_a = a;
            m_b = b;
        }
        void printComplex() {
            cout << "( m_a = "<< m_a << ", m_b = " << m_b << "i)" << endl;
        }

        Complex operator-(Complex& c1) {
            Complex tmp(this->m_a - c1.m_a, this->m_b - c1.m_b);
            return tmp;
        }

        friend Complex operator+(Complex& com1, Complex& com2);
};

//操作符重载写在全局  operator和要重载的操作符中间不能有空格
Complex operator+(Complex& com1, Complex& com2) {
    Complex tmp(com1.m_a + com2.m_a, com1.m_b + com2.m_b);
    return tmp;
}

int main() {
    Complex c1(1,2);
    Complex c2(2,3);
    c1.printComplex();
    c2.printComplex();

    Complex c3 = c1+c2;
    c3.printComplex();

    Complex c4 = c3 - c2 - c1;
    c4.printComplex();
    return 0;
}

双目运算符重载

#include <iostream>

using namespace std;

class Complex {
    private:
        int m_a;
        int m_b;
    public:
        Complex(int a, int b) {
            m_a = a;
            m_b = b;
        }
        Complex(){}
        void printComplex() {
            cout << "( m_a = "<< m_a << ", m_b = " << m_b << "i)" << endl;
        }

        Complex& operator+=(Complex& c1) {
            this->m_a += c1.m_a;
            this->m_b += c1.m_b;
            return *this;
        }

        Complex& operator++() {
            this->m_a++;
            this->m_b++;
            return *this;
        }

        const Complex& operator++(int) {
            Complex temp(this->m_a, this->m_b);
            this->m_a++;
            this->m_b++;
            return temp;
        }
};

int main() {
    Complex c1(1,2);
    Complex c2(2,3);
    
    (c1 += c2) += c2;
    c1.printComplex();

    Complex c3(5,6);
    ++++c3;
    c3.printComplex();

    Complex c4(3,2);
    c4++;
    c4.printComplex();
    
    return 0;
}

左移右移运算符重载

#include <iostream>

using namespace std;

class Complex {
    private:
        int m_a;
        int m_b;
    public:
        Complex(int a, int b) {
            m_a = a;
            m_b = b;
        }
        friend ostream& operator<<(ostream &os, Complex &c1);
        // 左移操作符,只能写在全局, 不能写在成员方法在,因为调用顺序会变反 c1 << cout;
        // ostream& operator<<(ostream &os){
        //     os <<"( m_a = "<< this->m_a << ", m_b = " << this->m_b << "i)" << endl;
        //     return os;
        // }
        friend istream& operator>>(istream &is, Complex &c1);
};


ostream& operator<<(ostream &os, Complex &c1){
    os <<"( m_a = "<< c1.m_a << ", m_b = " << c1.m_b << "i)";
    return os;
}

istream& operator>>(istream &is, Complex &c1) {
    cout <<" m_a = ";
    is >> c1.m_a;
    cout <<" m_b = ";
    is >> c1.m_b;
    return is;
}

int main() {
    Complex c1(1,2);
    cout << c1 << "    "<< c1 << endl;
    
    cin >> c1;
    cout << c1 << "    "<< c1 << endl;
    return 0;
}

=号操作符重载

#include <iostream>
#include <string.h>

using namespace std;

class Student {
    private:
        int id;
        char * name;
    public:
        Student() {
            this->id = 0;
            this->name = NULL;
        }
        Student(int id, char *name) {
            this->id = id;
            int length = strlen(name);
            this->name = new char[length +1];
            strcpy(this->name, name);
        }

        Student& operator=(const Student &stu) {
            //防止自身赋值
            if (this == &stu)
            {
                return *this;
            }
            // 先将自身的额外开辟的空间回收
            if (this->name != NULL)
            {
                delete this->name;
                this->name = NULL;
                this->id = 0;
            }
            
            //执行深拷贝
            this->id = stu.id;
            int length = strlen(stu.name);
            this->name = new char[length +1];
            strcpy(this->name, stu.name);
        }

        void print() {
            cout<< "id = " << id << "   name = " << name << endl;
        }

        ~Student() {
            if (this->name != NULL)
            {
                delete this->name;
                this->name = NULL;
            }
        }
};

int main() {
    Student stu1(22, "张三丰");
    Student stu2;
    stu2 = stu1;
    stu2.print();
    return 0;
}

相关文章

  • 运算符重载及其他约定

    7.1 重载算术运算符 重载二元算术运算符 重载复合赋值运算符 重载一元运算符 重载比较运算符 等号运算符:‘eq...

  • C++ 部分运算符重载

    可重载的运算符 不可重载的运算符和符号 重载运算符为类的成员函数 重载运算符为友元函数 重载赋值运算符 重载流插入...

  • 2019-07-11 运算符的重载

    运算符的重载 重载运算符 (“::”,“.*”,“.”,“?:”)不能被重载。 重载运算符时,不能改变其本质,如不...

  • 运算符重载

    一.+号运算符重载 二.<<号运算符重载 三.指针运算符重载(智能指针)

  • C++运算符重载-下篇 (Boolan)

    C++运算符重载-下篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++运算符重载-上篇 (Boolan)

    C++运算符重载-上篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++重载

    重载 C++语言规定: 重载的运算符要保持原运算符的意义。只能对已有的运算符重载,不能增加新的运算符。重载的运算符...

  • C++运算符重载详解

    运算符重载规则 1.被重载的运算符必须是已经存在的C++运算符,不能重载自己创建的运算符; 2.运算符被重载之后,...

  • 第十一章 使用类

    运算符重载 运算符重载是一种形式的C++多态。运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。...

  • 4.0 C++远征:重载运算符

    重载运算符 [TOC] 四、重载运算符 ​ 概念 : 给原有运算符赋予新功能。 ​ 本质 : 函数重载。 ...

网友评论

      本文标题:运算符重载

      本文链接:https://www.haomeiwen.com/subject/ycprtdtx.html