C++作業3

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.