|

楼主 |
发表于 2021-7-28 17:24:42
|
显示全部楼层
类名是Complex,成员函数名不能和类名一致呀
下面是视频的源码
class Complex
{
public:
Complex();
Complex(double r, double i); //带参数的构造函数
Complex operator+(Complex &d); //运算符重载,返回类型最好就是类的类型,
//不要用int
void print();
private:
double real;
double imag;
};
Complex::Complex()
{
real = 0;
imag = 0;
}
Complex::Complex(double r, double i)
{
real = r;
imag = i;
}
Complex Complex::operator+(Complex &d)
{
Complex c;
c.real = real + d.real;
c.imag = imag + d.imag;
return c;
}
void Complex::print()
{
cout << "(" << real << "," << imag << "i)\n";
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;
c3 = c1 + c2; //加号已被重载
cout << "c1 = ";
c1.print();
cout << "c2 = ";
c2.print();
cout << "c1 + c2 = ";
c3.print();
return 0;
}
|
|