运算符重载(复数类(Complex))

环境:win10,vs2013
复数运算法则
运算后结果仍然是一个复数

1.加法
两个复数的和依然是复数,它的实部是原来两个复数实部的和,它的虚部是原来两个复数虚部的和
z1=a+bi,z2=c+di
它们的和是:(a+bi)+(c+di)=(a+c)+(b+d)i

2.减法
z1=a+bi,z2=c+di
它们的差是:(a+bi)-(c+di)=(a-c)+(b-d)i

3.乘法
z1=a+bi,z2=c+di
它们的积是:(a+bi)(c+di)=(ac-bd)+(bc+ad)i

4.除法
z1=a+bi,z2=c+di
它们的商是:(a+bi)/(c+di)=((ac+bd)+(bc-ad)i)/(c^2+d^2)

Complex.h

using namespace std;
#include<stdlib.h>
#include<iostream>
class Complex
{
public :
    Complex();//默认构造函数
    Complex(double real, double image);//构造函数(构造对象)
    //赋值操作符重载需要注意:1.自己给自己赋值;2.返回值是引用的形式:3.返回当前对象;4.参数,传引用
    Complex& operator=(const Complex&d);//赋值操作符重载
    bool operator==(const Complex&c);//重载==
    bool operator!=(const Complex&c);//重载!=
    Complex(const Complex&c);//拷贝构造函数
    Complex operator+(const Complex&c);//重载+
    Complex operator*(const Complex&c);//重载*
    Complex operator-(const Complex&c);//重载-
    Complex operator/(const Complex&c);//重载/
    Complex operator+=(const Complex&c);//重载+=
    Complex operator-=(const Complex&c);//重载-=
    Complex operator*=(const Complex&c);//重载*=
    Complex operator/=(const Complex&c);//重载/=
    Complex operator++(int c);//后置++
    Complex& operator++();//前置++
    Complex operator--(int c);//后置--
    Complex& operator--();//前置--
    void Print();
private:
    double _real;
    double _image;
    friend ostream&operator<<(ostream&_cout, Complex&c);//输出运算符重载

};

Complex.cpp

#include"Complex.h"
void Complex::Print()
{
    cout << _real << "+" << _image << "i" << endl;
}
ostream&operator<<(ostream&_cout, Complex&c)//输出运算符重载
{
    _cout << c._real << "+" <<c._image << "i";
    return _cout;
}

Complex::Complex()//默认构造函数
{
    _real = 1.3;
    _image = 1.4;
}
Complex::Complex(double real=0.1, double image=0.2)
:_real(real)
, _image(image)
{}
Complex::Complex(const Complex&c)//拷贝构造函数
{
    _real = c._real;
    _image = c._image;
}

Complex& Complex::operator=(const Complex&c)//赋值运算符重载
{
    if (*this != c)
    {
        _real = c._real;
        _image = c._image;
    }
    return *this;
}
bool Complex::operator==(const Complex&c)//重载==
{
    if ((_real == c._real) && (_image = c._image))
        return true;
    return false;
}
bool Complex::operator!=(const Complex&c)//重载!=
{
    return!(*this == c);
}
Complex Complex::operator+(const Complex&c)//重载+
{
    return Complex(_real + c._real, _image + c._image);
}
Complex Complex::operator*(const Complex&c)//重载*
{
    return Complex(  ( (_real*c._real) - (_image*c._image)), ((_image*c._real) + (_real*c._image) )  );
}
Complex Complex::operator-(const Complex&c)//重载—
{
    return Complex(_real - c._real, _image - c._image);
}
Complex Complex::operator+=(const Complex&c)//重载+=
{
    _real = _real+c._real;
    _image = _image+c._image;
    return *this;
}
Complex Complex::operator-=(const Complex&c)//重载-=
{
    _real = _real - c._real;
    _image = _image -c._image;
    return *this;
}
Complex Complex::operator++(int c)//后置++
{
    Complex temp(*this);
    _real += 1;
    _image += 1;
    return temp;
}
Complex& Complex::operator++()//前置++
{
    _real += 1;
    _image += 1;
    return *this;
}
Complex Complex::operator--(int c)//后置——
{
    Complex temp(*this);
    _real -= 1;
    _image -= 1;
    return temp;
}
Complex& Complex::operator--()//前置--
{
    _real -= 1;
    _image -= 1;
    return *this;
}

test.cpp

#include"Complex.h"
int main()
{
    Complex c1(1.1, 2.2);
    c1.Print();
    Complex c2(3.3, 4.4);
    c2.Print();
    Complex c3;
    c3.Print();
    c3 = c2;
    cout << c3 << endl;
    cout << (c3 != c1) << endl;
    cout << (c3 == c1) << endl;
    cout << c1 + c2 << endl;
    cout << c1 - c2 << endl;
    cout << c1 * c2 << endl;
    cout << endl;
    Complex c4(5.5, 6.6);
    c4.Print();
    cout << (c1 += c4) << endl;
    cout << (c1 -= c4) << endl;
    cout << endl;
    cout << c1++ << endl;
    cout << c2-- << endl;
    cout << ++c3 << endl;
    cout << --c4 << endl;
    system("pause");
    return 0;
}

运行结果:
这里写图片描述

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
复数类定义包括两个私有成员变量:实部和虚部,以及一些公有成员函数来进行复数的基本运算。以下是一个简单的复数类定义示例: ```C++ class Complex { private: double real; // 实部 double imag; // 虚部 public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} Complex operator+(const Complex& c) const { // 加法运算符重载 return Complex(real + c.real, imag + c.imag); } Complex operator-(const Complex& c) const { // 减法运算符重载 return Complex(real - c.real, imag - c.imag); } Complex operator*(const Complex& c) const { // 乘法运算符重载 return Complex(real*c.real - imag*c.imag, real*c.imag + imag*c.real); } Complex operator/(const Complex& c) const { // 除法运算符重载 double denominator = c.real*c.real + c.imag*c.imag; return Complex((real*c.real + imag*c.imag) / denominator, (imag*c.real - real*c.imag) / denominator); } friend ostream& operator<<(ostream& os, const Complex& c) { // 输出运算符重载 os << c.real << "+" << c.imag << "i"; return os; } }; ``` 这个复数类定义包括了加、减、乘、除四种基本运算符的重载,以及一个友元函数重载输出运算符`<<`。其中加、减、乘、除四种运算符均返回一个新的复数对象,而不改变原先的两个对象。 需要注意的是,除法运算符重载时需要注意分母不能为零,否则会导致除数为零的错误。因此,在除法运算符重载中需要进行分母的计算,并且需要对分母是否为零进行判断。 此外,可以根据需要添加其他运算符的重载,比如复数的取模、幂运算等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值