C++实现一个复数类

复数类的实现中包含了众多C++的基础知识,对学习C++有很大的帮助。其中,运算符重载中的“前置++、–和后置++、–”稍微难些,需注意理解它的参数设计和返回值。

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>

using namespace std;

class Complex
{
private:
    double _real;
    double _image;

public://四个默认成员函数
    //构造函数
    Complex(double real = 0.0, double image = 0.0)
        :_real(real), _image(image)
    {

    }

    //拷贝构造函数
    Complex(const Complex& c)
    {
        _real = c._real;
        _image = c._image;
    }

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

    //赋值运算符的重载
    Complex operator = (const Complex& c)
    {
        this->_real = c._real;
        this->_image = c._image;
        return *this;
    }

public://复数“==”、“+”、“-”、“+=”、“-=”的情况
    //判断相等
    bool operator == (const Complex& d)
    {
        return ((_real == d._real) && (_image == d._image));
    }

    //两个复数相加
    Complex operator+(const Complex& d)
    {
        Complex tmp;
        tmp._real = _real + d._real;
        tmp._image = _image + d._image;
        return tmp;
    }

    //两个复数相减
    Complex operator-(const Complex& d)
    {
        Complex tmp;
        tmp._real = _real - d._real;
        tmp._image = _image - d._image;
        return tmp;
    }

    //复数对象-=d
    Complex& operator+=(const Complex& d)
    {
        _real += d._real;
        _image += d._image;
        return *this;
    }

    //复数对象-=d
    Complex& operator-=(const Complex& d)
    {
        _real -= d._real;
        _image -= d._image;
        return *this;
    }

public://复数“前置++、--”和“后置++、--”
    //前置++
    /*Complex operator++()
    {
        ++_real;
        ++_image;
        return *this;
    }*/
    Complex& operator++()
    {
        this->_real++;
        this->_image++;
        return *this;
    }

    //前置--
    /*Complex operator--()
    {
        --_real;
        --_image;
        return *this;
    }*/
    Complex& operator--()
    {
        this->_real--;
        this->_image--;
        return *this;
    }

    //后置++
    Complex operator++(int)
    {
        Complex tmp = *this;
        _real++;
        _image++;
        return tmp;
    }

    //后置--
    Complex operator--(int)
    {
        Complex tmp = *this;
        _real--;
        _image--;
        return tmp;
    }

    //打印复数
    void Display()
    {
        cout << _real << "+" << _image << "i" << endl;
    }
};

//测试函数
void Test1()
{
    cout << "初始化一个复数类" << endl;
    Complex c1(1.0, 2.0);//调用构造函数,新建复数类c1
    c1.Display();

    Complex c2(c1);//调用拷贝构造函数,新建复数类c2
    c2.Display();
    cout << endl;
}

void Test2()
{
    cout << "初始化一个复数类" << endl;
    Complex c1(1.0, 2.0);//调用构造函数,新建复数类c1
    c1.Display();

    Complex c2(c1);//调用拷贝构造函数,新建复数类c2
    c2.Display();

    //测试operator==
    cout << endl << "测试operator==" << endl;
    int ret = c1 == c2;
    if (ret)
    {
        cout << "c1 == c2" << endl;
    }
    else
    {
        cout << "c1 != c2" << endl;
    }

    //测试operator+
    cout << endl << "测试operator+" << endl;
    Complex c3 = c1 + c2;
    c3.Display();

    //测试operator-
    cout << endl << "测试operator-" << endl;
    Complex c4 = c1 - c2;
    c4.Display();

    //测试opertor+=
    cout << endl << "测试operator+=" << endl;
    c1 += c2;
    c1.Display();

    //测试opertor-=
    cout << endl << "测试operator-=" << endl;
    c1.operator-= (c2);
    c1.Display();

    cout << endl;
}

//测试前置++、--和后置++、--
void Test3()
{
    cout << "初始化一个复数类" << endl;
    Complex c1(1.0, 2.0);
    c1.Display();

    //前置++
    cout << endl << "测试前置++" << endl;
    Complex c2 = ++c1;
    c2.Display();

    //前置--
    cout << endl << "测试前置--" << endl;
    Complex c3 = --c1;
    c3.Display();
    c1.Display();

    //后置++
    cout << endl << "测试后置++" << endl;
    Complex c4 = c1++;
    c4.Display();
    c1.Display();

    //后置--
    cout << endl << "测试后置--" << endl;
    Complex c5 = c1--;
    c5.Display();
    c1.Display();

    cout << endl;
}

int main()
{
    Test1();
    Test2();
    Test3();
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值