C++基本运算符重载怎么使用

发布时间:2022-06-07 10:16:21 作者:zzz
来源:亿速云 阅读:132

C++基本运算符重载怎么使用

在C++中,运算符重载是一种强大的特性,它允许程序员为自定义类型(如类或结构体)定义运算符的行为。通过运算符重载,我们可以使自定义类型的对象像内置类型一样使用运算符,从而提高代码的可读性和简洁性。

1. 运算符重载的基本概念

运算符重载是通过定义一个特殊的成员函数或全局函数来实现的。这个函数的名称是operator后跟要重载的运算符符号。例如,要重载加法运算符+,可以定义一个名为operator+的函数。

1.1 成员函数形式的运算符重载

当运算符重载作为类的成员函数时,它只能访问该类的成员变量和成员函数。成员函数形式的运算符重载通常用于二元运算符(如+, -, *, /等)。

class Complex {
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 重载加法运算符
    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }

    void display() const {
        std::cout << real << " + " << imag << "i" << std::endl;
    }

private:
    double real;
    double imag;
};

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(1.0, 2.0);
    Complex c3 = c1 + c2;  // 使用重载的加法运算符
    c3.display();  // 输出: 4 + 6i
    return 0;
}

1.2 全局函数形式的运算符重载

当运算符重载作为全局函数时,它可以访问类的公有成员变量和成员函数。全局函数形式的运算符重载通常用于一元运算符(如++, --, !等)或需要访问多个类的运算符。

class Complex {
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 声明为友元函数,以便访问私有成员
    friend Complex operator+(const Complex& c1, const Complex& c2);

    void display() const {
        std::cout << real << " + " << imag << "i" << std::endl;
    }

private:
    double real;
    double imag;
};

// 定义全局的加法运算符重载
Complex operator+(const Complex& c1, const Complex& c2) {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(1.0, 2.0);
    Complex c3 = c1 + c2;  // 使用重载的加法运算符
    c3.display();  // 输出: 4 + 6i
    return 0;
}

2. 常见的运算符重载

2.1 算术运算符

算术运算符(如+, -, *, /等)通常用于执行数值计算。我们可以为自定义类型重载这些运算符,以便它们能够像内置类型一样进行算术运算。

class Complex {
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }

    Complex operator-(const Complex& other) const {
        return Complex(real - other.real, imag - other.imag);
    }

    void display() const {
        std::cout << real << " + " << imag << "i" << std::endl;
    }

private:
    double real;
    double imag;
};

2.2 关系运算符

关系运算符(如==, !=, <, >, <=, >=等)用于比较两个对象。我们可以为自定义类型重载这些运算符,以便它们能够像内置类型一样进行比较。

class Complex {
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    bool operator==(const Complex& other) const {
        return real == other.real && imag == other.imag;
    }

    bool operator!=(const Complex& other) const {
        return !(*this == other);
    }

    void display() const {
        std::cout << real << " + " << imag << "i" << std::endl;
    }

private:
    double real;
    double imag;
};

2.3 赋值运算符

赋值运算符(如=, +=, -=, *=, /=等)用于将一个对象的值赋给另一个对象。我们可以为自定义类型重载这些运算符,以便它们能够像内置类型一样进行赋值操作。

class Complex {
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    Complex& operator=(const Complex& other) {
        if (this != &other) {
            real = other.real;
            imag = other.imag;
        }
        return *this;
    }

    Complex& operator+=(const Complex& other) {
        real += other.real;
        imag += other.imag;
        return *this;
    }

    void display() const {
        std::cout << real << " + " << imag << "i" << std::endl;
    }

private:
    double real;
    double imag;
};

2.4 流插入和提取运算符

流插入运算符(<<)和流提取运算符(>>)通常用于输入输出操作。我们可以为自定义类型重载这些运算符,以便它们能够像内置类型一样进行输入输出操作。

class Complex {
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    friend std::ostream& operator<<(std::ostream& os, const Complex& c);
    friend std::istream& operator>>(std::istream& is, Complex& c);

private:
    double real;
    double imag;
};

std::ostream& operator<<(std::ostream& os, const Complex& c) {
    os << c.real << " + " << c.imag << "i";
    return os;
}

std::istream& operator>>(std::istream& is, Complex& c) {
    is >> c.real >> c.imag;
    return is;
}

int main() {
    Complex c;
    std::cin >> c;  // 输入: 3 4
    std::cout << c;  // 输出: 3 + 4i
    return 0;
}

3. 注意事项

4. 总结

运算符重载是C++中一项强大的特性,它允许我们为自定义类型定义运算符的行为。通过合理地使用运算符重载,我们可以使代码更加简洁、易读,并且更符合直觉。然而,使用运算符重载时也需要注意保持语义一致性,避免滥用。

推荐阅读:
  1. C++ 运算符重载(二)
  2. C++ 运算符重载(一)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:Python如何实现批量识别图片文字并存为Excel

下一篇:OpenCV如何根据面积筛选连通域

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》