第十三章 第十三章
名稱空間 名稱空間 (Namespace) (Namespace) 與 與 轉換函數與其他主題 轉換函數與其他主題
Namespace 關鍵字, 它會將所定義的名稱 區域化, 只有在該區域時方能看到在該區 域中所定義的名稱, 因此其許可同樣的名 稱在不同區域中並存.
Namespace
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;}
};
}
Namespace
Namespace 的使用 的使用
在名稱空間中所宣告的識別字在該名稱
空間內可以直接被使用. 例如MyNamespace
中的return i即是直接使用變數I. 但若您是在名 稱空間外要使用該名稱空間所定義的識別字時, 您便需使用作用範圍運算子(scope operator) 以 指定您所要使用的名稱空間為何.
MyNamespace::I =10;
Namespace
Namespace 的使用 的使用
若在程式中常要使用到某一名稱空間的 成員時, 若每次要存取該成員時便需指定 名稱空間及作用圍範運算子, 然後才跟著 所所要使用的成員, 如此便顯得非常麻煩.
因此using敘述便是用來減輕此一負擔, using敘述有以下兩種形式:
using namespace name;
using name::member;
Namespace
Namespace 的使用 的使用
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
Namespace 的使用 的使用
#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;
}
練習 練習
改寫成不需使用
改寫成不需使用 using using namespace std
namespace 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
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;
}
建立轉換函數 建立轉換函數
轉換函數將一物件的值轉換成另一與其 相容之資料型態
宣告:
operator type() { return value;}
轉換函數必需是屬於要做型態轉換的類 別中之成員
轉換函數範例 轉換函數範例
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;
}
練習 練習
class pwr{
int base;
int exp;
public:
pwr(int b,int e){ base=b;exp=e;}
};
建立一轉換函數將pwr型態的物件轉換成整數型態,並 使得傳回值為Baseexp
Class
Class pwr 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)<<e ndl;}
};
int main() {
pwr o1(2,3);
o1.show();
int i=o1;
cout<<i<<endl;
return 0;
}
Static
Static 類別成員 類別成員
當一成員被宣告成為static時, 則無論您建 立了多少該類別的物件, 該static成員變數 只有一份而已, 每一份物件皆會共亨此一 份static變數.事實上static成員變數在任何 物件被建立之前就已經存在了
Static
Static 類別成員 類別成員
當您在一類別中宣告static成員變數時, 您 並未定義它.所以您必須使用作用範圍決 定運算子(scope resolution operator)指定它 所屬的變數,再對該static成員變數定義.
static成員函數並不能使用this指標, 而且 不允許有虛擬的成員函數.而且static成員 函數也不能宣告成const或volatile.
Static
Static 範例 範例
#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
Static 範例 範例
#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 成員變數加一,反之當物件被消滅時在解 建構子中將該static成員變數減一,請實際 寫出該程式.
Const
Const 成員函數與 成員函數與 mutable mutable
類別成員函數宣告成const, 則該函數便不得修改所引用 的物件, 而且const成員函數也不得引用其他非const的成 員函數. 然而const成員函數可以被const或非const的函 數呼叫.
有時候您可能希望一類別中的某些特定成員變數被 const成員函數所修改, 而大部份成員變數仍不允許被 const成員函數修改, 您便可以使用mutable.
Const
Const 範例 範例
#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
Const 範例 範例
#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;
}
練習 練習 此為一倒數器程式 此為一倒數器程式 , , 當時 當時 間符合時發出警告聲
間符合時發出警告聲 , , 試修改 試修改 錯誤 錯誤
#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;
}
使用鏈結指定字和
使用鏈結指定字和 asm asm 關鍵字 關鍵字
在C++中提供兩種重要機制, 使其能方便與其他的程式 語言鏈結. 鏈結指定字(link specifier), 它可以讓 您的C++程式語言中的function與其他語言鏈 結.
1. 使用extern “language” function-prototype
2. 使用asm (“op-code”);
example example
#include<iostream>
using namespace std;
extern “C++” int func(int x);//link c function int func(int x)
{
return x;
}
example example
#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”);
}