优秀的编程知识分享平台

网站首页 > 技术文章 正文

「C++」 运算符重载总结及复数类实现

nanyue 2024-07-26 15:38:18 技术文章 8 ℃

C++ 预定义中的运算符的操作对象只局限于基本的内置数据类型,但是对于我们自定义的类型(类)是没有办法操作的。但是大多时候我们需要对我们定义的类型进行类似的运算,这个时候就需要我们对这么运算符进行重新定义,赋予其新的功能,以满足自身的需求。

运算符重载的实质就是函数重载或函数多态。运算符重载是一种形式的 C++ 多态。目的在于让人能够用同名的函数来完成不同的基本操作。要重载运算符,需要使用被称为运算符函数的特殊函数形式,运算符函数形式:operator p(argument-list)//operator后面的p为要重载的运算符符号。

一、运算符重载规则

1、运算符重载限制

不可以重载的运算符列表:

  • . 成员访问运算符
  • -> 成员指针访问运算符
  • :: 域运算符
  • sizeof 长度运算符
  • ?: 三目运算符
  • # 预处理运算符

重载运算符函数特性:

  • 不改变运算符的优先级
  • 不改变运算符的结合性
  • 不改变运算符所需要的操作数
  • 不能创建新的运算符

2、运算符重载方法

  1. 类成员函数
  2. 友元函数
  3. ++, --的前置重载和后置重载后置重载需要加占位参数int
  4. 重载<<的链式规则只能重载为非成员函数

注意点:

  1. 一般来说,单目运算符重载为类的成员函数,双目运算符重载为友元函数
  2. 双目运算符不能将=, (), [],->重载为类的友元函数
  3. 如果运算符的第一次操作数要求为隐式类型转换则必须为友元函数
  4. 友元函数重载运算常用于运算符的左右操作数类型不同的情况

二、运算符重载示例:复数类实现

// complex.h
#pragma once
#include <iostream>
using namespace std;

class Complex
{
public:
    // 构造函数
    Complex(double r = 0, double i = 0): re(r), im(i){}

    // 拷贝构造函数
    Complex(const Complex& obj):re(obj.re), im(obj.im){}

    // 析构函数
    ~Complex() {}

    // +
    Complex operator +(const Complex&);

    // -
    Complex operator -(Complex&);

    // +=
    Complex& operator += (Complex&);

    // -=
    Complex& operator -=(Complex&);

    // ==
    bool operator ==(Complex&);

    // 前置 --
    Complex& operator --();

    // 前置 ++
    Complex& operator ++();

    // 后置 --
    Complex operator --(int);

    // 后置 ++
    Complex operator ++(int);

    // <<
    friend ostream& operator <<(ostream& out, const Complex& obj);

    double real() const { return this->re; }
    double imag() const { return this->im; }

private:
    double re, im;
};
// complex.cpp
#include "complex.h"

ostream& operator <<(ostream& out, const Complex& obj)
{
    return out << "(" << obj.re << ", " << obj.im << ") ";
}

Complex Complex::operator+(const Complex & obj)
{
    return Complex(this->re + obj.re, this->im + obj.im);
}

Complex Complex::operator-(Complex &obj)
{
    return Complex(this->re - obj.re, this->im - obj.im);
}

Complex & Complex::operator+=(Complex &obj)
{
    this->re += obj.re;
    this->im += obj.im;
    return *this;
}

Complex & Complex::operator-=(Complex &obj)
{
    this->re -= obj.re;
    this->im -= obj.im;
    return *this;
}

bool Complex::operator==(Complex &obj)
{
    return (this->re == obj.re && this->im == obj.im);
}

Complex & Complex::operator--()
{
    this->re--;
    this->im--;
    return *this;
}

Complex & Complex::operator++()
{
    (this->re)++;
    (this->im)++;
    return *this;
}

Complex Complex::operator--(int)
{
    Complex tmp = *this;
    this->re--;
    this->im--;
    return tmp;
}

Complex Complex::operator++(int)
{
    Complex tmp = *this;
    this->re++;
    this->im++;
    return tmp;
}
// main.cpp
#include <iostream>
#include "complex.h"

using namespace std;

int main()
{
    Complex c0;
    Complex c1(2, -3);
    Complex c2(c1);
    Complex c3 = c1 + c2;
    Complex c4;


    c4 += c1;


    cout << c0  << c1 << endl;
    cout << c2 << endl;
    cout << c3 << endl;
    cout << c4 << endl;

    cout << (c1 == c4) << endl;
    cout << ++c4 << endl;
    cout << c4++ << c4 << endl;

    cout << c4.real() << ", " << c4.imag() << endl;


    system("pause");
    return 0;
}

输出结果:

(0, 0) (2, -3)
(2, -3)
(4, -6)
(2, -3)
1
(3, -2)
(3, -2) (4, -1)
4, -1
请按任意键继续. . .

Tags:

最近发表
标签列表