課堂小練習: 設計、定義並實現Complex類
阿新 • • 發佈:2019-04-01
ostream style std gin img pri 課堂 .sh complex類
#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(double real1 = 0, double imaginary1= 0); Complex(Complex &p); void add(Complex t); void show(); double mod(); private: double real, imaginary; }; Complex::Complex(類的定義double real1, double imaginary1) { real= real1; imaginary= imaginary1; } Complex::Complex(Complex &p) { real=p.real; imaginary=p.imaginary; } void Complex::add(Complex t) { real=t.real+real; imaginary=t.imaginary+imaginary; } void Complex::show() { cout<< real << "+" << imaginary<< "i" << endl; } double Complex::mod() { double a; a=sqrt(real*real+imaginary*imaginary); return a; } int main() { Complex c1(3, 5); Complex c2(4.5); Complex c3(c1); c1.add(c2); c1.show(); cout<<c1.mod(); return 0; }
總結:運用類的定義,這個框架運用還是不太熟悉。
課堂小練習: 設計、定義並實現Complex類