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