主函数已给出,只需写出Complex类的代码即可,代码中的 类名、方法名、参数名已经指定,请勿修改。 class Complex{ private: double ; public: Complex(double , double ) { } void add(Complex com){ } void show(){ } }; int main() { Complex c1(3,5); //initialize c1 with complex 3+5i Complex c2=4.5; //initialize c2 with 4.5 c1.add(c2); //c1+c2,result saved in c1 c1.show(); //print out c1.(result is 7.5+5i) return 0; }
时间: 2024-04-09 20:31:36 浏览: 53
代码规范表1
class Complex{
private:
double real;
double imaginary;
public:
Complex(double r, double i)
{
real = r;
imaginary = i;
}
void add(Complex com){
real += com.real;
imaginary += com.imaginary;
}
void show(){
cout << real << "+" << imaginary << "i" << endl;
}
};
int main()
{
Complex c1(3,5); //initialize c1 with complex 3+5i
Complex c2=4.5; //initialize c2 with 4.5
c1.add(c2); //c1+c2,result saved in c1
c1.show(); //print out c1.(result is 7.5+5i)
return 0;
}
阅读全文