源代码:
#include<iostream> using namespace std; class Complex { public: Complex(float r1,float i1); Complex(float r1); void add(Complex c); void show(); private: float r; float i; }; Complex::Complex(float r1,float i1) { r=r1; i=i1; } Complex::Complex(float r1) { r=r1; } void Complex::add(Complex c) { r+=c.r; i+=c.i; } void Complex::show() { cout<<r<<"+"<<i<<"i"<<endl; } int main() { Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show(); }
运行结果: