1 / 18

高等程式語言實習課

高等程式語言實習課. http://helldeathscythe.myweb.hinet.net/. 使用 const. int main() { const double pi = 3.1415926; const char *p = “ Hello ” ; char * const p = “ Hello ” ; const char * const p = “ hello ” }. const 物件 的 值 只能在宣告時設值 只有建構子能設定 除非是 const 函數. 使用 const.

more
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. 使用const int main() { const double pi = 3.1415926; const char *p = “Hello”; char * const p = “Hello”; const char * const p = “hello” } const 物件 的 值 只能在宣告時設值 只有建構子能設定 除非是const函數

  4. 使用const void print(int &a,int &b){ cout << a << ““ << b << endl; a=0; b=0;} int main() { int x=3,y=5; print(x,y); cout << x << ““ << y << endl;}

  5. 使用const void print(const int &a,const int &b){ cout << a << ““ << b << endl; a=0; b=0; //<=能跑嗎?} int main() { int x=3,y=5; print(x,y); cout << x << ““ << y << endl;}

  6. 常數成員與靜態成員 class test{ const int a; int b;public: test(int _a,int _b) { a=_a; b=_a; } }; error C2758: 'a' : must be initialized in constructor base/member initializer list

  7. 常數成員與靜態成員 class test{ const int a; int b;public: test(int _a,int _b):a(_a),b(_b){} }; 呼叫 a 類別的建構子去 幫 a 設值

  8. 常數成員與靜態成員 class clock{ int hr; int min; int sec;public:… int gethr() const {return hr;} //為什麼要加const? int getmin() const {return min;} int getsec() const {return sec;}… }; const 常數函數 不可修改成員變數

  9. 常數成員與靜態成員 class test{ int a; int b; public: int geta() { return a;} int getb() { return b;}… }; test operator+(const test &t1,const test &t2) { test t; t.set(t1.geta(),t2.getb()); return t; } error C2662: 'geta' : cannot convert 'this' pointer from 'const class test' to 'class test Conversion loses qualifiers

  10. 常數成員與靜態成員 class test{ int a; int b; public: int geta() const { return a;} int getb() const { return b;}… }; test operator+(const test &t1,const test &t2) { test t; t.set(t1.geta(),t2.getb()); return t; } 不然 operator+ 要跟test作朋友 才能分享私有成員變數

  11. 休息一下 有家鞋店來了個客人,買了一雙30元的鞋,並拿出一張50紙鈔。 但是老闆沒有零錢,於是找了隔壁賣菜的老闆兌換一下,並把零錢找給客人。 過了一會兒,隔壁買菜的老闆跑來抱怨剛才的那張紙鈔是假的,但是買鞋的客人早就拿著鞋子跑了,鞋店老闆只好賠給賣菜老闆50元。請問鞋店老闆總共損失多少錢?

  12. 常數成員與靜態成員 Non-const Non-static object const static member function

  13. 常數成員與靜態成員 • 想要計數程式執行了幾次這個物件 class test{ int xxoo;public: test(int &t,int ooxx) { t=t++; xxoo=ooxx; }}; 因此這個類別不能有Default constructor…囧rz

  14. 常數成員與靜態成員 class test{ static int t; int xxoo;public: test(int ooxx) { t++; xxoo = ooxx; }}; error LNK2001: unresolved external symbol "private: static int test::t" (?t@test@@0HA)

  15. 常數成員與靜態成員 class test{ static int t=0; int xxoo;public: test(int ooxx) { t++; xxoo = ooxx; }}; error C2252: 't' : pure specifier can only be specified for functions

  16. 常數成員與靜態成員 靜態成員初始化 class test{ static int t; int xxoo;public: test(int ooxx) { t++; xxoo = ooxx; }}; int test::t = 0;

  17. 常數成員與靜態成員 靜態函數 class math{public: int abs(int n) { return –n; } int pow(int n,int m) {… } int sin(int c) { … } }; voin main() { math m; cout<<m.abs(1-5); cout<<m.pow(2,10); cout<<m.sin(60); }

  18. 常數成員與靜態成員 靜態函數 (工具函數) voin main() { cout<<math::abs(1-5); cout<<math::pow(2,10); cout<<math::sin(60); } class math{public: static int abs(int n) { return –n; } static int pow(int n,int m) {… } staticint sin(int c) { … } };

More Related