標籤:img .com cout int space using c++ cin 使用
//4-11
#include<iostream>using namespace std;class Rectangle{ public: Rectangle(double L=1,double W=1); Rectangle(Rectangle &r); ~Rectangle(){}; void show(double L,double W); private: double length; double width;}; Rectangle::Rectangle(double L,double W){ length=L; width=W;}Rectangle::Rectangle(Rectangle &r){ length = r.length; width = r.width;}void Rectangle::show(double L,double W){ length=L; width=W; cout<<"length:"<<length<<endl; cout<<"width:"<<width<<endl; cout<< "area:"<<length*width<<endl; }int main(){ double length,width; cin>>length>>width; Rectangle area(length,width); area.show(length,width); return 0; }
#include<iostream> using namespace std;class Complex{ public: Complex(double r0=0,double i0=0); void add(Complex &c0); void show() { cout<<"complex:"<<Real<<"+"<<imaginary<<"i"<<endl; } private: double Real; double imaginary; };Complex::Complex(double r0,double i0){ Real=r0; imaginary=i0;}void Complex::add(Complex &c0){ Real+=c0.Real; imaginary+=c0.imaginary;}int main(){ Complex c1(3,5); Complex c2=4.5; c1.show(); c2.show(); c1.add(c2); cout<<"additon:"<<endl; c1.show(); return 0;}
在做第一個程式的時候 建構函式沒有設預設形參 導致在使用的時候一直不知道怎麼改錯 使用時應該直接賦值 一開始自己看書真的是模模糊糊 在寫了代碼自己慢慢找錯誤 該錯誤 對這單元的內容才有了比較清晰的概念 可以慢慢理解 在第一個實驗理解的基礎上 第二個寫下來就比較順利了
C++作業3