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

23
第第第第 第第第第 第第第第 第第第第 (Namespace) (Namespace) 第第第第第第第第第 第第第第第第第第第 Namespace 關關關 , 關關關關關關關關關關關關 , 關關關關關關關關關關關關關關關關關關關關關關 , 關關關關 關關關關關關關關關關關關關關 .

Upload: isi

Post on 18-Jan-2016

119 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

第十三章 第十三章 名稱空間名稱空間 (Namespace) (Namespace) 與 與

轉換函數與其他主題轉換函數與其他主題

Namespace 關鍵字 , 它會將所定義的名稱區域化 , 只有在該區域時方能看到在該區域中所定義的名稱 , 因此其許可同樣的名稱在不同區域中並存 .

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

NamespaceNamespace 的宣告的宣告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;} };}

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

NamespaceNamespace 的使用的使用

在名稱空間中所宣告的識別字在該名稱 空間內可以直接被使用 . 例如 MyNamespace

中的 return i 即是直接使用變數 I. 但若您是在名稱空間外要使用該名稱空間所定義的識別字時 , 您便需使用作用範圍運算子 (scope operator) 以指定您所要使用的名稱空間為何 .

MyNamespace::I =10;

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

NamespaceNamespace 的使用的使用 若在程式中常要使用到某一名稱空間的成

員時 , 若每次要存取該成員時便需指定名稱空間及作用圍範運算子 , 然後才跟著所所要使用的成員 , 如此便顯得非常麻煩 .

因此 using 敘述便是用來減輕此一負擔 , using 敘述有以下兩種形式 :

using namespace name; using name::member;

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

NamespaceNamespace 的使用的使用 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;

}

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

NamespaceNamespace 的使用的使用

#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;

}

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

練習練習改寫成不需使用改寫成不需使用 using namespusing namesp

ace stdace std 敘述敘述 #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 fileif(!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; }

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

建立轉換函數建立轉換函數

轉換函數將一物件的值轉換成另一與其相容之資料型態

宣告 : operator type() { return value;}

轉換函數必需是屬於要做型態轉換的類別中之成員

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

轉換函數範例轉換函數範例

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 inte

ger cout<<i<<‘\n’; return 0;}

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

練習練習

class pwr{

int base;

int exp;

public:

pwr(int b,int e){ base=b;exp=e;}

}; 建立一轉換函數將 pwr 型態的物件轉換成整數型態 ,

並使得傳回值為 Baseexp

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

Class pwrClass 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;}

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

Static Static 類別成員類別成員

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

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

Static Static 類別成員類別成員

當您在一類別中宣告 static 成員變數時 , 您並未定義它 . 所以您必須使用作用範圍決定運算子 (scope resolution operator) 指定它所屬的變數 , 再對該 static 成員變數定義 .

static 成員函數並不能使用 this 指標 , 而且不允許有虛擬的成員函數 . 而且 static成員函數也不能宣告成 const 或 volatile.

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

StaticStatic 範例範例

#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;}

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

StaticStatic 範例範例

#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;}

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

練習練習

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

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

ConstConst 成員函數與成員函數與 mutablemutable

類別成員函數宣告成 const, 則該函數便不得修改所引用的物件 , 而且 const 成員函數也不得引用其他非 const 的成員函數 . 然而 const 成員函數可以被 const 或非 const 的函數呼叫 .

有時候您可能希望一類別中的某些特定成員變數被 const 成員函數所修改 , 而大部份成員變數仍不允許被 const 成員函數修改 , 您便可以使用 mutable.

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

ConstConst 範例範例#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;

}

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

ConstConst 範例範例#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;}//err

or

};

int main()

{

demo ob;

ob.seti(1900);

cout<<ob.geti();

return 0;

}

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

練習 此為一倒數器程式練習 此為一倒數器程式 ,, 當時當時間符合時發出警告聲間符合時發出警告聲 ,, 試修改試修改

錯誤錯誤 #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;}

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

使用鏈結指定字和使用鏈結指定字和 asmasm 關鍵關鍵字字

在 C++ 中提供兩種重要機制 , 使其能方便與其他的程式語言鏈結 . 鏈結指定字 (link specifier), 它可以讓您的 C++ 程式語言中的 function 與其他語言鏈結 .

1. 使用 extern “language” function-prototype

2. 使用 asm (“op-code”);

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

exampleexample

#include<iostream>

using namespace std;

extern “C++” int func(int x);//link c function

int func(int x)

{

return x;

}

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

exampleexample

#include<iostream> using namespace std; extern “c” int func(int x);//li

nk c function

int func(int x) { return x3;}

void func()

{

asm(“mov bp,sp”);

asm(“push ax”);

asm(“mov c1,4”);

}