Complex類的實(shí)現(xiàn)
Complex類的實(shí)現(xiàn)
#include <iostream> #include <cmath> using namespace std; class Complex{ public: Complex (double a=0,double b=0); Complex (Complex &t); void add( Complex y); void show(); double mod(); private: double real; double imaginary; }; Complex::Complex(double a,double b){ real=a; imaginary=b; } Complex::Complex(Complex &t){ real=t.real; imaginary=t.imaginary; } void Complex::add(Complex y){ real=real+y.real; imaginary=imaginary+y.imaginary; } double Complex::mod(){ return sqrt(real*real+imaginary*imaginary); } void Complex::show(){ cout<<real<<"+"<<imaginary<<"i"<<endl; } int main (){ Complex c1(3,5); cout << "c1 = " ; c1.show(); cout <<"|c1| = "<<c1.mod()<<endl; Complex c2(4.5); cout << "c2 = "; c2.show(); cout << "c1 + c2 = "; c1.add(c2); c1.show(); return 0; }
結(jié)果

實(shí)驗(yàn)總結(jié)與體會(huì)
這次程序還是花了一個(gè)半小時(shí),一開(kāi)始構(gòu)造函數(shù)出錯(cuò),沒(méi)有寫(xiě)初始值,然后重新修改,再?gòu)膹?fù)制構(gòu)造函數(shù)中找錯(cuò)。總之還是對(duì)C++的題目缺少練習(xí)。希望以后能能把程序做的又快又好。
posted on 2019-03-31 15:36 棲枝Fairy 閱讀(94) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)