1 / 20

高等程式語言實習課

高等程式語言實習課. http://helldeathscythe.myweb.hinet.net/. Constructor and Destructor. Constructor 建構子 物件生成時設定初值 沒有回傳值 必須被宣告成 public 類似 int x=10; => int x(10);. Constructor and Destructor. frac.

everly
Download Presentation

高等程式語言實習課

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 高等程式語言實習課

  2. http://helldeathscythe.myweb.hinet.net/

  3. Constructor and Destructor • Constructor 建構子 • 物件生成時設定初值 • 沒有回傳值 • 必須被宣告成public • 類似int x=10; => int x(10);

  4. Constructor and Destructor • frac class frac{public: int up; //分子int down; //分母int gcd(int x, int y);// 約分時呼叫用void setfrac(int u,int d); //設定分數void reduce(); // 約分void print(string msg); //印出分數frac frac_add(const frac &f); //分數加法frac frac_div(const frac &f); //分數除法};

  5. Constructor and Destructor • frac void main() { frac a; a.setfrac(28,-35); a.print("a="); // 印出 f=-15/20 frac b; b.setfrac(21,24); frac c; c.setfrac(27,-36);}

  6. 使用建構子呢?

  7. Constructor and Destructor • frac void main() { frac a(28,-35); a.print("a="); // 印出 f=-15/20 frac b(21,24); frac c(27,-36); }

  8. Constructor and Destructor • 建構子的種類 • Default constructor (沒有參數的建構子) • Copy Constructor (參數是自己的建構子) • Conversion Constructor (有參數的建構子)

  9. Constructor and Destructor • Conversion Constructor (有參數的建構子) class frac{public: int up; //分子int down; //分母frac(int u,int d):up(u),down(d) {} }; frac(int u,int d) { up=u; down=d; } 當你傳遞的物件 沒有實作”=”時 使用constructor傳遞物件!!!

  10. Constructor and Destructor • Default constructor (沒有參數的建構子) class frac{public: int up; //分子int down; //分母frac() { up=0; down=1; }};

  11. Constructor and Destructor • Copy Constructor (參數是自己的建構子) class frac{public: int up; //分子int down; //分母frac(const frac &f) { up=f.up; down=f.down; }};

  12. Constructor and Destructor • 這是什麼建構子? class frac{public: int up; //分子int down; //分母frac(int n) { up=n; down=1; }};

  13. Constructor and Destructor • 建構子的使用時機 • 物件產生時

  14. Constructor and Destructor • 請問以下程式被執行時,會執行哪些Constructor int main(){ frac *f1, *f2, *f3, *f4; f1 = new frac ; p1-> print (); f2 = new frac (3); p2-> print(); f3 = new frac (3,5); p3-> print(); f4 = new frac (f3); p4-> print(); }

  15. Constructor and Destructor • 請問以下程式被執行時,會執行哪些Constructor frac frac_add(frac a,frac b) { frac rlt(a.up*b.down+b.up*a.down,a.down*b.down); return rlt; } int main(){ frac a(3,4); frac b(1,4); frac c = frac_add(a,b); } 物件生成!!!

  16. Constructor and Destructor • 如果Copy Constructor這樣寫,結果如何? class frac{public: int up; //分子int down; //分母frac(frac f) { up=f.up; down=f.down; }};

  17. Constructor and Destructor • 請問以下程式被執行時,會執行哪些Constructor int main(){ frac *f; f = new frac[10]; }

  18. Constructor and Destructor • Destructor 解構子 • 當物件被消滅時執行 • 沒有回傳值 不允許傳遞參數 • 必須被宣告成public

  19. Constructor and Destructor • Destructor 解構子 • 當你的物件中含有指標時,一定要寫解構子 • 當物件含有指標時,一定要寫 • Copy Constructor • Operator =() • Destructor

  20. Constructor and Destructor • 萬年考題 • 請寫出以下程式執行時物件的生成與消滅順序 void fun1() { xstr s1; } void fun2(xstr s3) {} xstr fun3() { xstr s4; retrun s4;} void main(){ xstr s; fun1(); fun2(); s = fun3(); }

More Related