如何使Complex类乘以int

时间:2019-05-21 15:59:00

标签: c++

我正在用c ++创建一个复数类。我正在执行此步骤: 我正在尝试使运算符*-在“ int”和“ complex”之间工作。 规则如下

c = int, (a, b) = complex number (a + bi)
1. c - (a, b) = (c – a, – b)
2. (a, b) – c = (a – c, b)
3. c * (a, b) = (ca, cb) 
4. (a, b) * c = (ac, bc)
(1 and 2 must be worked differently)

我已经成功地制作了2,4(起初它们具有复数),但是对于用2,4(起初它们具有“ INT”)运算符我一无所知。

这是我的代码

// h file
myComplex operator -(int value) const;
myComplex operator *(int value) const;


// cpp file
myComplex myComplex::operator -(int value) const {
int newReal = realPart - value;
    return myComplex(newReal, imaginaryPart);
} // this is for number 2


myComplex myComplex::operator *(int value) const {
    int newReal = realPart * value;
    int newImag = imaginaryPart * value;
    return myComplex(newReal, newImag);
} // this is for number 4

请帮助我。如何使第一个术语为INT的运算符?

0 个答案:

没有答案