复数类的运算符重载

1.题目:

Problem Description

定义描述复数complex的类
(前缀减)operator-- (成员)
(前缀加)operator++ (友元)
(后缀减)operator-- (成员)
(后缀加)operator++ (友元)

Input

输入多个测试实例,每个测试实例占一行,表示一个复数

Output

每个输出实例占一行。如下输出所示,每行有四组复数,分别是对输入的复数对象进行前缀减,前缀加,后缀减,后缀加。每个复数之间用空格隔开。

Sample Input

1 1
2 2
3 3

Sample Output

(0,0) (1,1) (0,0) (1,1)
(1,1) (2,2) (1,1) (2,2)
(2,2) (3,3) (2,2) (3,3)

 

2.参考代码:

 

#include <iostream>
using namespace std;

class Complex
{

private:
    int real, image;

public:
    Complex(int r = 0, int i = 0);
    Complex operator--();
    friend Complex operator++(Complex&);
    Complex operator--(int);
    friend Complex operator++(Complex&, int);
    void show();

};

Complex::Complex(int r, int i)
{
    real = r;
    image = i;
}

Complex Complex::operator--()
{
    --real;
    --image;

    return *this;
}

Complex operator++(Complex& c)
{
    ++c.real;
    ++c.image;

    return c;
}

Complex Complex::operator--(int)
{
    real--;
    image--;

    return *this;
}

Complex operator++(Complex& c, int)
{
    c.real++;
    c.image++;

    return c;
}

void Complex::show()
{
    cout << "(" << real << "," << image << ")";
}

int main()
{
    int r, i;

    while (cin >> r >> i) {

        Complex x(r, i);

        --x;

        x.show();

        cout << ' ';

        ++x;

        x.show();

        cout << ' ';

        x--;

        x.show();

        cout << ' ';

        x++;

        x.show();

        cout << endl;
    }

    return 0;
}



 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值