07_多文件工程和編譯預處理命令.ppt_第1頁
07_多文件工程和編譯預處理命令.ppt_第2頁
07_多文件工程和編譯預處理命令.ppt_第3頁
07_多文件工程和編譯預處理命令.ppt_第4頁
07_多文件工程和編譯預處理命令.ppt_第5頁
已閱讀5頁,還剩47頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、第七章 多文件工程和編譯預處理命令,學習目標,掌握多文件工程的組織和應用方法; 掌握外部變量和外部函數(shù)的定義與使用; 了解標準C+庫和命名空間的概念; 了解各種預編譯指令的功能。,2,目錄,7.1 多文件工程 7.2 外部變量與外部函數(shù) 7.3 標準C+庫和命名空間 7.3.1 標準C+庫 7.3.2 什么是命名空間 7.3.3 命名空間的定義 7.3.4 命名空間的使用 7.3.5 命名空間的嵌套,3,目錄(續(xù)),7.4 編譯預處理 7.4.1 在程序中包含頭文件 7.4.2 程序中的置換 7.4.3 條件預編譯,4,7.1 多文件工程,將類和函數(shù)的定義與對它們的使用分離 將類的聲明和類的實

2、現(xiàn)分離 便于分工合作 便于軟件的維護。,5,例7-1:設計一個圓類,并計算圓的面積和周長,/文件1,類的定義,Circle.h #include using namespace std; class Circle private: float radius; /圓半徑 public: Circle(float r); /構(gòu)造函數(shù) float Circumference(); /計算圓周長 float Area(); /計算圓面積 ;,6,例7-1(續(xù)),/文件2,類的實現(xiàn), Circle.cpp #include”Circle.h” Circle:Circle(float r): radius

3、(r) float Circle:Circumference() return 2.0 * 3.14 * radius; float Circle:Area() return 3.14 * radius * radius; ,7,例7-1(續(xù)),/文件3, 主函數(shù),7_1.cpp #include Circle.h int main() Circle cr(5.0); coutCircumference = cr.Circumference()endl; coutArea = cr.Area()endl; return 0; ,8,例7-1(續(xù)),運行結(jié)果: Circumference = 3

4、1.4 Area = 78.5,7.2 外部變量與外部函數(shù),對于大型復雜程序一般采用多文件工程,也就是一個工程由多個文件構(gòu)成,但有時候我們在一個文件中定義的變量或函數(shù),希望能夠被其他文件共享, 這時候我們就要把該變量或函數(shù)定義為外部變量或外部函數(shù)。 聲明外部變量和外部函數(shù) 具體格式如下: 外部變量:extern 變量名; 外部函數(shù):extern 函數(shù)名;,10,例7-2:外部變量和外部函數(shù)的聲明和使用,/A.h class A private: double nValue; public: A(); double GetValue() return nValue; ;,11,例7-2(續(xù)),/

5、A.cpp #include A.h #include using namespace std; double nInitValue = 10.0; void ShowMessage() cout The ShowMessage function has been calledendl; ,12,例7-2(續(xù)),A:A() nValue = nInitValue; ShowMessage(); /B.h class B private: double nValue; public: B(); double GetValue () return nValue; ;,13,例7-2(續(xù)),/B.c

6、pp #include B.h extern double nInitValue ; extern void ShowMessage(); B:B() nValue = nInitValue; ShowMessage(); ,14,例7-2(續(xù)),/7-2.cpp #include A.h #include B.h #include using namespace std; void main() A a; B b; cout nValue of A is: a.GetValue()endl; cout nValue of B is: b.GetValue()endl; ,15,例7-2(續(xù))

7、,運行結(jié)果: The ShowMessage function has been called The ShowMessage function has been called nValue of Test1 is: 10 nValue of Test2 is: 10,7.3.1 標準C+庫,標準C+類與組件在邏輯上分為6種類型 1)輸入/輸出類; 2)容器類與ADT(抽象容器類型); 3)存儲管理類; 4)算法; 5)錯誤管理; 6)運行環(huán)境支持。,17,7.3.2 什么是命名空間,命名空間(Namespace)是C+用來避免命名沖突的機制。 命名空間相當于一個作用域,在這個作用域中,所有標

8、識符都是唯一的。不同命名空間的名字作用域也不相同,這樣保證了不同的命名空間相互不影響,即便在兩個命名空間的標志符是相同的,也不會產(chǎn)生沖突。 例如:using namespace std; std就是一個C+標準庫的命名空間。,18,7.3.3 命名空間的定義,可以定義自己的命名空間,其格式如下: namespace namespace _name /變量聲明; /函數(shù)聲明; /類型定義; 這里namespace是關鍵字,namespace _name是表示命名空間的標識符,它唯一標識了命名空間,這個名稱會附加于在該命名空間中聲明的所有實體。大括號限定了命名空間namespace _name的作

9、用域。,19,例7-3:定義一個自己的命名空間,#include using namespace std;/標準c+命名空間 namespace myown /定義自己的命名空間 int max(int x,int y)return xy?x:y; int nval1,nval2;/聲明兩個變量 double multi(double x, double y)/定義求積函數(shù) return x*y; /無名的命名空間的成員 ;,20,例7-3(續(xù)),int main() coutmyown:nval1myown:nval2; coutthe maxnum is myown:max(myown:n

10、val1,myown:nval2)endl; coutthe accumulation is : multi(myown:nval1,myown:nval2)endl; ,21,例7-3(續(xù)),運行結(jié)果: please input two integer:4 7 the maxnum is7 the accumulation is :28,例7-4:在多個文件中實現(xiàn)同一個命名空間,/7_4.h namespace myown double multi(double x, double y)/定義求積函數(shù) return x*y; ; /7_4.cpp #include using namespa

11、ce std; #include 7_4.h namespace myown /定義自己的命名空間 int max(int x,int y)return xy?x:y; int nval1,nval2;/聲明兩個變量 ,23,例7-4(續(xù)),int main() coutmyown:nval1myown:nval2; coutthe maxnum is myown:max(myown:nval1,myown:nval2)endl; coutthe accumulation is : myown:multi(myown:nval1,myown:nval2)endl; ,24,例7-4(續(xù)),運行

12、結(jié)果: please input two integer:4 7 the maxnum is7 the accumulation is :28,7.3.4 命名空間的使用,一個命名空間將不同的標識符集合在一個命名作用域(name scope)內(nèi)。這樣,在不同的命名空間中,即使使用同樣的標識符來表示不同的事物,也不會引起命名沖突。 為了說明標識符是屬于哪一個命名空間,需要在標識符前加上命名空間名字和“:”。其中“:”稱為域作用符。,26,例7-5:多種方法使用命名空間,/7-5.h namespace myspace1 int max(int x,int y)return xy?x:y; int

13、 nval1,nval2; namespace myspace2 double multi(double x, double y) /定義求積函數(shù) return x*y; ; double division(double x,double y) return x/y; ,27,例7-5(續(xù)),/7-5.cpp #include using namespace std;/using指示符 #include 7-5.h namespace my=myspace1; /別名 double division(double x,double y); int main() coutmy:nval1my:n

14、val2; cout“the maxnum is”myspace1:max(my:nval1,my:nval2)endl; using myspace2:multi; using myspace2:division; cout“the accumulation is :”multi(my:nval1,my:nval2)endl; cout“the quotient is:”division(my:nval1,my:nval2)endl; coutanother quotient is:”:division(my:nval1,my:nval2)endl; double division(doub

15、le x,double y) return y/x; ,28,例7-5(續(xù)),運行結(jié)果: please input two integer:4 7 the maxnum is7 the accumulation is :28 the quotient is:0.571429 another quotient is:1.75,例7-6:編寫簡單倒計時的計數(shù)器,#include using namespace std; namespace NewNameSpace int upperbound, lowerbound; class counter int count; public: counte

16、r(int n) if (n lowerbound) return count-; else return lowerbound; ; ,30,例7-6(續(xù)),int main() using NewNameSpace:upperbound; upperbound = 100; NewNameSpace:lowerbound = 0; NewNameSpace:counter ob1(10); int i; do i = ob1.run(); cout NewNameSpace:lowerbound); cout endl; using namespace NewNameSpace; coun

17、ter ob2(20);,31,例7-6(續(xù)),do i = ob2.run(); cout lowerbound); cout lowerbound); return 0; ,32,例7-6(續(xù)),運行結(jié)果: 10 9 8 7 6 5 4 3 2 1 0 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 100 99 98 97 96 95 94 93 92 91 90,7.3.5 命名空間的嵌套,命名空間也可以嵌套定義,也就是說可以在命名空間中定義命名空間。 在命名空間之外定義函數(shù),函數(shù)返回類型如果是自定義類型的,必須使用命名空間的限

18、定修飾;同時,函數(shù)名同樣需要這種限定修飾。但是對于函數(shù)參數(shù)表和函數(shù)體內(nèi)部的命名空間成員名字都可以使用簡短形式。,34,例7-7:編寫簡單倒計時的計數(shù)器,#include using namespace std; namespace myspace1 double const PI=3.14; double nval1(1.2),nval2(1.3); double max(double x, double y); namespace myspace2 /在命名空間myspace1內(nèi)定義命名空間myspace2 double multi(double x); double division(do

19、uble x) return PI/x; ,35,例7-7(續(xù)),int main() coutmyspace1:nval1myspace1:nval2; couty?x:y; double myspace1:myspace2:multi(double x) return PI*x; ,36,例7-7(續(xù)),運行結(jié)果: please input two integer:4 7 the maxnum is7 the accumulation is :12.56 the quotient is:0.448571,7.4.0 C+預處理指令集,38,7.4.1 在程序中包含頭文件,頭文件是外部文件,

20、通常存儲在磁盤上,其內(nèi)容可以通過#include預處理指令包含的程序中。 例如: #include 文件包含一般形式如下 #include 把自己的源文件包含在程序中,此時文件名要放在雙引號中 例如:#include ”Circle.h”,39,7.4.2 程序中的置換,程序中的置換包括符號置換和宏置換。C語言中,常用符號置換定義符號常量,宏定義常用來實現(xiàn)簡單的函數(shù)計算。 符號常量定義格式為: #define 標識符 字符序列 如:#define PI 3.14159265 C+中,定義符號常量更好的辦法是用const來定義, 如:const double pi = 3.14159265;,4

21、0,7.4.2 程序中的置換(續(xù)),宏定義格式 #define 標示符(參數(shù)列表) 替換字符串 如:#define MAX(a,b) (ab?a:b) 當程序中出現(xiàn)如下語句: c = MAX(a,b); 預編譯過程會把它替換為: c = (ab?a:b); C+中這一功能已被內(nèi)聯(lián)函數(shù)取代。 #undef 用于取消對標識符和宏的定義。,41,例7-8:簡單宏替換預處理指令(#define),#include using namespace std; #define YES 1 #define PI 3.1415926 #define RAD PI/180 #define MESG “This i

22、s a string” #define PRINT(k) cout b?a:b) void main() cout ”YES = ”YESendl; if (YES) cout”PI = ”PIendl; cout ”RAD = ”RADendl; cout MESGendl; int a=0, b=2, c; c = MAX(a,b); PRINT(c); ,42,例7-8(續(xù)),運行結(jié)果: YES = 1 PI = 3.14159 RAD = 0.0174533 This is a string 2,7.4.3 條件預編譯,使用條件編譯指令可以限定程序中的某些內(nèi)容要在滿足一定條件的情況下才

23、參與編譯。因此,利用條件編譯可以使同一個源程序在不同的編譯條件下產(chǎn)生不同的目標代碼。 #if的使用方式有兩種: 第一,可以測試某個常量表達式是否為真。 例如: #if 1 第二,可以測試某個符號是否以前用#define指令定義過。 例如: #ifdef PI #ifndef PI #if defined(標識符) 如果指定的標識符已經(jīng)定義,#if后面的語句組就包含在要編譯的源文件中。,44,例7-9:條件預編譯預處理指令,#include using namespace std; #define PI 3.1415926 void main() int i = 100; #if 1 cout

24、”i = ”iendl; #endif #ifdef PI cout ”1 PI= ”PIendl; #endif #ifndef PI cout ”2 PI=”PIendl; #endif ,45,例7-9(續(xù)),運行結(jié)果: i = 100 1 PI= 3.14159,例7-10:預編譯指令的應用,防止類的重復定義,/Point.h #ifndef POINTCLASS #define POINTCLASS class Point private: double X, Y, Z; public: Point (double _X=0, double _Y=0, double _Z=0); Point (Point #endif,47,例7-10(續(xù)),/Point.cpp #include Point.h #include Using namespace std; Poi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論