一个c++的题,我不会,请教一下

定义一个复数类,并实现其必要的构造函数、显示函数,实现加减乘除其中一个运算。程序框架如下。
#include
using namespace std;
class Complex {
private:
double r; // 实部
double i; // 虚部
public:
Complex();
Complex(double r, double i);
Complex(const Complex & c);
~Complex();
void show();
Complex add(const Complex & c);
Complex sub(const Complex & c);
Complex mul(const Complex & c);
Complex div(const Complex & c);
};
void main() {
Complex c1(3, 4), c2(5, 8);
Complex c3;
c3 = c1.add(c2);
c3.show();
c3 = c1.sub(c2);
c3.show();
c3 = c1.mul(c2);
c3.show();
c3 = c1.div(c2);
c3.show();
}

代码补充如下:

img

代码:


#include <iostream>
using namespace std;
class Complex 
{
private:
    double r; // 实部
    double i; // 虚部
public:
    Complex(){};
    Complex(double r, double i){this->r = r;this->i = i;};
    Complex(const Complex & c){this->r = c.getReal();this->i = c.getVir();};
    ~Complex(){};
    void show();
    Complex add(const Complex & c) ;
    Complex sub(const Complex & c);
    Complex mul(const Complex & c);
    Complex div(const Complex & c);

    double getReal() const {return r;}
    double getVir() const {return i;}
    void setReal(double r){this->r = r;}
    void setVir(double i){this->i = i;}
};
void main() {
    Complex c1(3, 4), c2(5, 8);
    Complex c3;
    c3 = c1.add(c2);
    c3.show();
    c3 = c1.sub(c2);
    c3.show();
    c3 = c1.mul(c2);
    c3.show();
    c3 = c1.div(c2);
    c3.show();
}

Complex Complex::add(const Complex & c) 
{
    this->r += c.getReal();
    this->i += c.getVir();
    return *this;
}
Complex Complex::sub(const Complex & c)
{
    this->r -= c.getReal();
    this->i -= c.getVir();
    return *this;
}
Complex Complex::mul(const Complex & c)
{
    this->r *= c.getReal();
    this->i *= c.getVir();
    return *this;
}
Complex Complex::div(const Complex & c)
{
    this->r /= c.getReal();
    this->i /= c.getVir();
    return *this;
}

void Complex::show()
{
    cout << r << "+" << i <<"i"<<endl;
}

https://blog.csdn.net/qq_34328833/article/details/52115612

Complex::Complex(): r(0), i(0) {}
Complex::Complex(double r, double i): r(r), i(i) {}
Complex::Complex(const Complex &c): r(c.r), i(c.i) {}
Complex::~Complex() {}
void Complex::show()  {
  cout << r << "+" << i << "i";
}
Complex Complex::add(const Complex &c) {
  Complex result;
  result.r = r + c.r;
  result.i = i + c.i;
  return result;
}

Complex Complex::sub(const Complex &c) {
  Complex result;
  result.r = r - c.r;
  result.i = i - c.i;
  return result;
}

Complex Complex::mul(const Complex &c) {
  Complex result;
  result.r = r * c.r - i * c.i;
  result.i = r * c.i + i * c.r;
  return result;
}

Complex Complex::div(const Complex &c) {
  Complex result;
  double bottom = c.r * c.r + c.i * c.i;
  result.r = (r * c.r + i * b.i) / bottom;
  result.i = (i * c.r - r * b.i) / bottom;
  return result;
}