C++在Complex类中重载运算符

main.cpp

#include <iostream>
#include "Complex.h"
using namespace std;

int main()
{
    Complex c1(3.0, 4.0);
    Complex c2(4.0, 5.0);
    Complex c3;
    Complex c4;
    Complex c5;
    Complex c6;

    c3 = c1 + c2;
    c4 = c1 - c2;
    c5 = c1 * c2;
    c6 = c1 / c2;

    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    cout << "c1 + c2 = " << c3 << endl;
    cout << "c1 - c2 = " << c4 << endl;
    cout << "c1 * c2 = " << c5 << endl;
    cout << "c1 / c2 = " << c6 << endl;

    Complex *ptr1;
    Complex *ptr2;
    Complex *ptr3;
    Complex *ptr4;
    Complex *ptr5;
    Complex *ptr6;

    ptr1 = new Complex(7.0, 8.0);
    ptr2 = new Complex(6.0, 5.0);
    ptr3 = new Complex();
    ptr4 = new Complex();
    ptr5 = new Complex();
    ptr6 = new Complex();

    *ptr3 = *ptr1 + *ptr2;
    *ptr4 = *ptr1 - *ptr2;
    *ptr5 = *ptr1 * *ptr2;
    *ptr6 = *ptr1 / *ptr2;

    cout << "*ptr1 = " << *ptr1 << endl;
    cout << "*ptr2 = " << *ptr2 << endl;
    cout << "*ptr1 + *ptr2 = " << *ptr3 << endl;
    cout << "*ptr1 - *ptr2 = " << *ptr4 << endl;
    cout << "*ptr1 * *ptr2 = " << *ptr5 << endl;
    cout << "*ptr1 / *ptr2 = " << *ptr6 << endl;

    delete ptr3;
    delete ptr4;
    delete ptr5;
    delete ptr6;

    return 0;
}

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>

using namespace std;

class Complex
{
public:
    Complex();
    Complex(double real, double imaginary); // default constructor
    ~Complex();

    void setReal(double real);
    void setImaginary(double imaginary);

    double getReal();
    double getImaginary();

    Complex operator+(const Complex& c); // 重载
    Complex operator-(const Complex& c); //
    Complex operator*(const Complex& c); //
    Complex operator/(const Complex& c); //

    friend istream& operator>>(istream& input, Complex& a);
    friend ostream& operator<<(ostream& output, Complex& a);

    void print(); // in real + i imaginary
private:
    double Real;
    double Imaginary;
};
#endif // COMPLEX_H

Complex.cpp

#include "Complex.h"

using namespace std;

Complex::Complex()
{
    Real = 0.0;
    Imaginary = 0.0;
}

Complex::Complex(double real, double imaginary)
{
    setReal(real);
    setImaginary(imaginary);
}

Complex::~Complex()
{
    //cout << "object Destructor is called" << endl;
}

void Complex::setReal(double real)
{
    Real = real;
}

void Complex::setImaginary(double imaginary)
{
    Imaginary = imaginary;
}

double Complex::getReal()
{
    return Real;
}

double Complex::getImaginary()
{
    return Imaginary;
}

Complex Complex::operator+(const Complex& c)
{
    return Complex(getReal() + c.Real, getImaginary() + c.Imaginary);
}

Complex Complex::operator-(const Complex& c)
{
    return Complex(getReal() - c.Real, getImaginary() - c.Imaginary);
}

Complex Complex::operator*(const Complex& c)
{
    return Complex(getReal() * c.Real - getImaginary() * c.Imaginary, getReal() * c.Imaginary + getImaginary() * c.Real);
}

Complex Complex::operator/(const Complex& c)
{
    double denominator = c.Real * c.Real + c.Imaginary * c.Imaginary;
    double a = getReal() * c.Real + getImaginary() * c.Imaginary;
    double b = getImaginary() * c.Real - getReal() * c.Imaginary;
    return Complex(a / denominator, b / denominator);
}

// 重载输入运算符
istream& operator>>(istream& input, Complex& c)
{
    input >> c.Real >> c.Imaginary;
    return input;
}

// 重载输出运算符
ostream& operator<<(ostream& output, Complex& c)
{
    output << c.getReal() << "+" << c.getImaginary() << "i";
    return output;
}

void Complex::print()
{
    cout << "Real is " << getReal() << endl;
    cout << "Imaginary is " << getImaginary() << endl;
    cout << endl;
}

Result

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值