成员运算符重载函数

目录

1.定义成员运算符重载函数的语法形式

2.双目运算符重载

3.单目运算符重载


        在C++中,可以把运算符重载函数定义成某个类的成员函数,称为成员运算符重载函数。

1.定义成员运算符重载函数的语法形式

        (1)在类的内部,定义成员运算符重载函数的格式如下:

函数类型 operator 运算符(形参表)
{
    函数体
}

        (2)成员运算符重载函数也可以在类中声明成员函数的原型,在类外定义。

        在类的内部,声明成员运算符重载函数原型的格式如下:

class X
{
    ...
    函数类型 operator 运算符(形参表);
    ...
};

        在类外,定义成员运算符重载函数格式如下:

函数类型 X::operator 运算符(形参表)
{
    函数体
}

2.双目运算符重载

        对双目运算符而言,成员运算符重载函数的形参表中仅有一个参数,它作为运算符的右操作数。另一个操作数(左操作数)是隐含的,是该类的当前对象,它是通过this指针隐含地传递给函数的。例如:

class X
{
    ...
    int operator+(X a);
    ...
};

        下面看一个用成员运算符重载函数进行复数运算。

#include <iostream>

using namespace std;

class Complex
{
public:
    Complex(double r = 0, double i = 0); // 构造函数
    void print(); // 显示输出复数
    Complex operator+(Complex c); // 声明运算符 + 重载函数
    Complex operator-(Complex c); // 声明运算符 - 重载函数
    Complex operator*(Complex c); // 声明运算符 * 重载函数
    Complex operator/(Complex c); // 声明运算符 / 重载函数
private:
    double real; // 复数实部
    double imag; // 复数虚部
};

Complex::Complex(double r, double i) // 定义构造函数
{
    real = r;
    imag = i;
}

Complex Complex::operator+(Complex c)
{
    Complex temp;
    temp.real = real + c.real;
    temp.imag = imag + c.imag;
    return temp;
}

Complex Complex::operator-(Complex c)
{
    Complex temp;
    temp.real = real - c.real;
    temp.imag = imag - c.imag;
    return temp;
}

Complex Complex::operator*(Complex c)
{
    Complex temp;
    temp.real = real * c.real - imag * c.imag;
    temp.imag = real * c.imag + imag * c.real;
    return temp;
}

Complex Complex::operator/(Complex c)
{
    Complex temp;
    double t;
    t = 1 / (c.real * c.real + c.imag * c.imag);
    temp.real = (real * c.real + imag * c.imag) * t;
    temp.imag = (c.real * imag - real * c.imag) * t;
    return temp;
}

void Complex::print()
{
    cout << real;
    if (imag > 0)
        cout << "+";
    if (imag != 0)
        cout << imag << 'i' << endl;
}

int main()
{
    Complex A1(2.3, 4.6), A2(3.6, 2.8), A3, A4, A5, A6; // 定义6个Complex类的对象
    A3 = A1 + A2; // 复数相加
    A4 = A1 - A2; // 复数相减
    A5 = A1 * A2; // 复数相乘
    A6 = A1 / A2; // 复数相除

    A1.print(); // 输出复数A1
    A2.print(); // 输出复数A2
    A3.print(); // 输出复数相加结果A3
    A4.print(); // 输出复数相减结果A4
    A5.print(); // 输出复数相乘结果A5
    A6.print(); // 输出复数相除结果A6

    return 0;
}

        程序运行结果如下:

3.单目运算符重载

        对单目运算符而言,成员运算符重载函数的参数表中没有参数,此时当前对象作为运算符的一个操作数。

        下面是一个用成员函数重载单目运算符“++”的例子。

#include <iostream>

using namespace std;

class Coord
{
public:
    Coord(int i = 0, int j = 0);
    Coord operator++(); // 定义单目运算符 ++ 重载函数operator++
    void print();
private:
    int x, y;
};

Coord::Coord(int i, int j)
{
    x = i;
    y = j;
}
void Coord::print()
{
    cout << "x = " << x << ", y = " << y << endl;
}

Coord Coord::operator++() // 定义运算符 ++ 重载函数operator++
{
    ++x;
    ++y;
    return *this; // 返回当前对象的值
}

int main()
{
    Coord ob(10, 20);
    ob.print();
    ++ob;
    ob.print();
    return 0;
}

        程序运行结果如下:

猜你喜欢

转载自blog.csdn.net/2301_80391227/article/details/142630677