1 / 23

第十三章 名稱空間 (Namespace) 與 轉換函數與其他主題

第十三章 名稱空間 (Namespace) 與 轉換函數與其他主題. Namespace 關鍵字 , 它會將所定義的名稱區域化 , 只有在該區域時方能看到在該區域中所定義的名稱 , 因此其許可同樣的名稱在不同區域中並存. Namespace 的宣告. Namespace name{ // declarations } Ex: namespace Mynamespace{ int i,k; void myfunc(int j) {cout<<j;} class myclass { public:

isi
Download Presentation

第十三章 名稱空間 (Namespace) 與 轉換函數與其他主題

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. 第十三章 名稱空間(Namespace) 與 轉換函數與其他主題 • Namespace 關鍵字, 它會將所定義的名稱區域化, 只有在該區域時方能看到在該區域中所定義的名稱, 因此其許可同樣的名稱在不同區域中並存.

  2. Namespace的宣告 Namespace name{ // declarations } Ex: namespace Mynamespace{ int i,k; void myfunc(int j) {cout<<j;} class myclass { public: void seti(int x) { i=x;} int geti() { return i;} }; }

  3. Namespace的使用 • 在名稱空間中所宣告的識別字在該名稱 空間內可以直接被使用. 例如MyNamespace 中的return i即是直接使用變數I. 但若您是在名稱空間外要使用該名稱空間所定義的識別字時, 您便需使用作用範圍運算子(scope operator) 以指定您所要使用的名稱空間為何. MyNamespace::I =10;

  4. Namespace的使用 • 若在程式中常要使用到某一名稱空間的成員時, 若每次要存取該成員時便需指定名稱空間及作用圍範運算子, 然後才跟著所所要使用的成員, 如此便顯得非常麻煩. 因此using敘述便是用來減輕此一負擔, using敘述有以下兩種形式: using namespace name; using name::member;

  5. Ex:namespace firstNs{ class demo{ int I; public: demo(int x){I=x;} void seti(int I) {I=x;} int geti() {return I;} }; char str[]=“Illustrating namespace\n” int counter; } Int main() { using namespace firstNs;//方法一 for(counter=10;counter;counter--) cout<<counter<<“”; using firstNs::str; //方法二 cout<<str; } Namespace的使用

  6. #include<iostream> using namespace std; namespace Demo{ int a; } int x; namespace Demo{ int b; } int main() { using namespace Demo; a=b=x=100; cout<<a<<“”<<b<<“”<<x; return 0; } Namespace的使用

  7. #include<iostream> #include<fstream> using namespace std; int main(int agrc,char *argv[]) { if(argc!=3){ cout<<“Usage Convert <input> <output>\n”; return 1; } ifstream fin(argv[1]);//open input file ofstream fout(argv[2]);//create output file if(!fout){ cout<<“Cannot open output file\n”; return 1; } if(!fin){ cout<<“Cannot open input file\n”; return 1; } char ch; fin.unsetf(ios::skipws); while(!fin.eof()){ fin>>ch; if(ch==‘ ’) ch= ‘|’; if(!fin.eof()){ fout<<ch;} } fin.close(); fout.close(); return 0; } 練習改寫成不需使用using namespace std敘述

  8. 建立轉換函數 • 轉換函數將一物件的值轉換成另一與其相容之資料型態 宣告: operator type() { return value;} 轉換函數必需是屬於要做型態轉換的類別中之成員

  9. class coord { int x,y; public: coord(int i,int j) { x=I;y=j;} operator int() { return x*y;} //轉換函數 }; int main() { coord o1(2,3), o2(4,3); int i; i=o1;//auto convert to integer cout<<i<<‘\n’; i=100+o2;//convert o2 to integer cout<<i<<‘\n’; return 0; } 轉換函數範例

  10. 練習 class pwr{ int base; int exp; public: pwr(int b,int e){ base=b;exp=e;} }; 建立一轉換函數將pwr型態的物件轉換成整數型態,並使得傳回值為Baseexp

  11. Class pwr #include <math.h> class pwr{ int base; int exp; public: pwr(int b,int e){ base=b;exp=e;} operator int() { return base*exp;} void show(){cout<<pow(base,exp)<<endl;} }; int main() { pwr o1(2,3); o1.show(); int i=o1; cout<<i<<endl; return 0; }

  12. Static 類別成員 當一成員被宣告成為static時, 則無論您建立了多少該類別的物件, 該static成員變數只有一份而已, 每一份物件皆會共亨此一份static變數.事實上static成員變數在任何物件被建立之前就已經存在了

  13. Static 類別成員 當您在一類別中宣告static成員變數時, 您並未定義它.所以您必須使用作用範圍決定運算子(scope resolution operator)指定它所屬的變數,再對該static成員變數定義. static成員函數並不能使用this指標, 而且不允許有虛擬的成員函數.而且static成員函數也不能宣告成const或volatile.

  14. #include<iostream> Using namespace std; class myclass{ static int i; public: void seti(int n){i=n;} int geti() {return i;} }; int myclass::i; int main() { myclass o1,o2; o1.seti(10); cout<<“o1.i:”<<o1.geti()<<‘\n’; cout<<“o2.i:”<<o2.geti()<<‘\n’; return 0; } Static範例

  15. #include<iostream> Using namespace std; Class myclass{ Public: static int i; void seti(int n){i=n;} int geti() {return i;} }; int myclass::i; int main() { myclass o1,o2; myclass::i=100; cout<<“o1.i:”<<o1.geti()<<‘\n’; cout<<“o2.i:”<<o2.geti()<<‘\n’; return 0; } Static範例

  16. 練習 試撰寫一程式記錄該類別建立了多少個物件, 當物件產生時在建構子中將該static成員變數加一,反之當物件被消滅時在解建構子中將該static成員變數減一,請實際寫出該程式.

  17. Const成員函數與mutable 類別成員函數宣告成const, 則該函數便不得修改所引用的物件, 而且const成員函數也不得引用其他非const的成員函數. 然而const成員函數可以被const或非const的函數呼叫. 有時候您可能希望一類別中的某些特定成員變數被const成員函數所修改, 而大部份成員變數仍不允許被const成員函數修改, 您便可以使用mutable.

  18. #include<iostream> using namespace std; class demo{ int i; public: int geti() const {return i;} //ok void seti(int x) const {i=x;} //error }; int main() { demo ob; ob.seti(1900); cout<<ob.geti(); return 0; } Const範例

  19. #include<iostream> using namespace std; class demo{ mutable int i; int j; public: int geti() const {returni;}//ok void seti(int x) const {i=x;}//ok void setj(int x) const {j=x;}//error }; int main() { demo ob; ob.seti(1900); cout<<ob.geti(); return 0; } Const範例

  20. #include<iostream> using namespace std; class countdown{ int incr; int target; (mutable) int current; public: countdown(int delay,int i=1){ target=delay; incr=I; current=0; } bool counting() const { current += incr; if(current>= target){ cout<<“\a”; return false; } } }; int main() { countdown ob(100,2); while(ob.counting()); return 0; } 練習 此為一倒數器程式,當時間符合時發出警告聲,試修改錯誤

  21. 使用鏈結指定字和asm關鍵字 在C++中提供兩種重要機制, 使其能方便與其他的程式語言鏈結. 鏈結指定字(link specifier), 它可以讓您的C++程式語言中的function與其他語言鏈結. • 使用extern “language” function-prototype • 使用asm (“op-code”);

  22. example #include<iostream> using namespace std; extern “C++” int func(int x);//link c function int func(int x) { return x; }

  23. #include<iostream> using namespace std; extern “c” int func(int x);//link c function int func(int x) { return x3; } void func() { asm(“mov bp,sp”); asm(“push ax”); asm(“mov c1,4”); } example

More Related