版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、Chapter 12Separate Compilationand NamespacesOverview12.1 Separate Compilation 12.2 NamespacesSlide 12- 312.1Separate CompilationSeparate CompilationC+ allows you to divide a program into partsEach part can be stored in a separate fileEach part can be compiled separatelyA class definition can be stor
2、ed separately from a program.This allows you to use the class in multiple programsSlide 12- 5ADT ReviewAn ADT is a class defined to separate theinterface and the implementationAll member variables are privateThe class definition along with the function and operator declarations are grouped together
3、as theinterface of the ADTGroup the implementation of the operations togetherand make them unavailable to the programmer using the ADTSlide 12- 6The ADT InterfaceThe interface of the ADT includesThe class definitionThe declarations of the basic operations which can be one of the followingPublic memb
4、er functions Friend functionsOrdinary functionsOverloaded operatorsThe function commentsSlide 12- 7The ADT ImplementationThe implementation of the ADT includesThe function definitionsThe public member functionsThe private member functionsNon-member functionsPrivate helper functionsOverloaded operato
5、r definitionsMember variablesOther items required by the definitionsSlide 12- 8Separate FilesIn C+ the ADT interface and implementation can be stored in separate filesThe interface file stores the ADT interfaceThe implementation file stores the ADT implementationSlide 12- 9A Minor CompromiseThe publ
6、ic part of the class definition is part of the ADT interfaceThe private part of the class definition is part of the ADT implementation This would hide it from those using the ADTC+ does not allow splitting the public andprivate parts of the class definition across filesThe entire class definition is
7、 usually in the interface fileSlide 12- 10Case Study:DigitalTimeThe interface file of the DigitalTime ADT classcontains the class definitionThe values of the class are:Time of day, such as 9:30, in 24 hour notationThe public members are part of the interfaceThe private members are part of the implem
8、entationThe comments in the file should provide all the details needed to use the ADTSlide 12- 11Naming The Interface FileThe DigitalTime ADT interface is stored in a file named dtime.hThe .h suffix means this is a header fileInterface files are always header filesA program using dtime.h must includ
9、e it usingan include directive #include dtime.hSlide 12- 12Display 12.1#include or ?To include a predefined header file use #include tells the compiler to look where the systemstores predefined header filesTo include a header file you wrote, use and #include dtime.h and usually cause the compiler to
10、 look in the current directory for the header fileSlide 12- 13The Implementation FileContains the definitions of the ADT functionsUsually has the same name as the header file buta different suffixSince our header file is named dtime.h, the implementation file is named dtime.cppSuffix depends on your
11、 system (some use .cxx or .CPP)Slide 12- 14#include dtime.hThe implementation file requires an include directive to include the interface file: #include dtime.hSlide 12- 15Display 12.2 (1)Display 12.2 (2)Display 12.2 (3)The Application FileThe Application file is the file that contains the program t
12、hat uses the ADTIt is also called a driver fileMust use an include directive to include the interface file: #include dtime.hSlide 12- 16Display 12.3Running The ProgramBasic steps required to run a program:(Details vary from system to system!)Compile the implementation fileCompile the application fil
13、eLink the files to create an executable program using a utility called a linkerLinking is often done automaticallySlide 12- 17Compile dtime.h ?The interface file is not compiled separatelyThe preprocessor replaces any occurrence of #include dtime.h with the text of dtime.h before compiling Both the
14、implementation file and the application file contain #include dtime.hThe text of dtime.h is seen by the compiler in each of these filesThere is no need to compile dtime.h separatelySlide 12- 18Why Three Files?Using separate files permitsThe ADT to be used in other programs withoutrewriting the defin
15、ition of the class for eachImplementation file to be compiled once even if multiple programs use the ADTChanging the implementation file does not require changing the program using the ADTSlide 12- 19Reusable ComponentsAn ADT coded in separate files can be used over and overThe reusability of such a
16、n ADT class Saves effort since it does not need to be RedesignedRecodedRetestedIs likely to result in more reliable componentsSlide 12- 20Multiple ClassesA program may use several classesEach could be stored in its own interface and implementation filesSome files can include other files, that includ
17、e still othersIt is possible that the same interface file could be included in multiple filesC+ does not allow multiple declarations of a classThe #ifndef directive can be used to prevent multiple declarations of a classSlide 12- 21Introduction to #ifndefTo prevent multiple declarations of a class,w
18、e can use these directives:#define DTIME_H adds DTIME_H to a list indicating DTIME_H has been seen#ifndef DTIME_H checks to see if DTIME_H has been defined #endifIf DTIME_H has been defined, skip to #endifSlide 12- 22Using #ifndefConsider this code in the interface file #ifndef DTIME_H #define DTIME
19、_H #endifThe first time a #include dtime.h is found, DTIME_H and the class are definedThe next time a #include dtime.h is found, all lines between #ifndef and #endif are skippedSlide 12- 23truefalseWhy DTIME_H?DTIME_H is the normal convention for creating an identifier to use with ifndefIt is the fi
20、le name in all capsUse _ instead of . You may use any other identifier, but will makeyour code more difficult to readSlide 12- 24Display 12.4Defining LibrariesYou can create your own libraries of functionsYou do not have to define a class to use separatefilesIf you have a collection of functionsDecl
21、are them in a header file with their commentsDefine them in an implementation fileUse the library files just as you use your class interfaceand implementation filesSlide 12- 25Section 12.1 ConclusionCan youDetermine which belongs to the interface, implementation or application files?Class definition
22、Declaration of a non-member function used as an operation of the ADTDefinition of a member functionThe main part of the programDescribe the difference between a C+ class and an ADT?Slide 12- 2612.2NamespacesNamespacesA namespace is a collection of name definitions,such as class definitions and varia
23、ble declarationsIf a program uses classes and functions written by different programmers, it may be that the same nameis used for different thingsNamespaces help us deal with this problemSlide 12- 28The Using Directive#include places names such as cinand cout in the std namespaceThe program does not
24、 know about names in thestd namespace until you add using namespace std;(if you do not use the std namespace, you can define cin and cout to behave differently)Slide 12- 29The Global NamespaceCode you write is in a namespaceit is in the global namespace unless you specify a namespaceThe global names
25、pace does not require the using directiveSlide 12- 30Name ConflictsIf the same name is used in two namespacesThe namespaces cannot be used at the same timeExample: If myFunction is defined in namespaces ns1 and ns2, the two versions of myFunction could be used in one program by using local using dir
26、ectives this way:Slide 12- 31 using namespace ns1; myFunction( ); using namespace ns2; myFunction( );Scope Rules For usingA block is a list of statements enclosed in sThe scope of a using directive is the block in which it appearsA using directive placed at the beginning of a file, outside any block
27、, applies to the entire fileSlide 12- 32Creating a NamespaceTo place code in a namespaceUse a namespace groupingnamespace Name_Space_Name Some_Code To use the namespace createdUse the appropriate using directiveusing namespace Name_Space_Name;Slide 12- 33Namespaces:Declaring a FunctionTo add a funct
28、ion to a namespaceDeclare the function in a namespace groupingnamespace savitch1 void greeting( );Slide 12- 34Namespaces:Defining a FunctionTo define a function declared in a namespaceDefine the function in a namespace grouping namespace savitch1 void greeting( ) cout Hello from namespace savitch1.n
29、; Slide 12- 35Namespaces:Using a FunctionTo use a function defined in a namespaceInclude the using directive in the program where the namespace is to be usedCall the function as the function would normallybe called int main( ) using namespace savitch1; greeting( ); Slide 12- 36Using directives scope
30、Display 12.5 (1-2)A Namespace ProblemSuppose you have the namespaces below:Is there an easier way to use both namespacesconsidering that myFunction is in both?Slide 12- 37namespace ns1 fun1( ); myFunction( ); namespace ns2 fun2( ); myFunction( );Qualifying NamesUsing declarations (not directives) al
31、low us to select individual functions to use from namespacesusing ns1:fun1; /makes only fun1 in ns1 availThe scope resolution operator identifies a namespace hereMeans we are using only namespace ns1s version of fun1If you only want to use the function once, call it like this ns1:fun1( );Slide 12- 3
32、8Qualifiying Parameter NamesTo qualify the type of a parameter with a using declarationUse the namespace and the type name int getNumber (std:istream inputStream) istream is the istream defined in namespace stdIf istream is the only name needed from namespace std, then you do not need to use using n
33、amespace std;Slide 12- 39Directive/Declaration (Optional)A using declaration (using std:cout;) makes only one name available from the namespaceA using directive makes all the names in the namespace availableSlide 12- 40A Subtle Point (Optional)A using directive potentially introduces a nameIf ns1 an
34、d ns2 both define myFunction, using namespace ns1; using namespace ns2; is OK, provided myFunction is never used!Slide 12- 41A Subtle Point ContinuedA using declaration introduces a name into your code: no other use of the name can be made using ns1:myFunction; using ns2:myFunction;is illegal, even
35、if myFunction is never usedSlide 12- 42Unnamed NamespacesAs we have done helper functions so far, they are not really hidden (Display 12.2)We would like them to be local to the implementationfile to implement information hidingThe unnamed namespace can hide helper functionsNames defined in the unnam
36、ed namespace are local to the compilation unitA compilation unit is a file (such as an implementation file)plus any file(s) #included in the fileSlide 12- 43The unnamed groupingEvery compilation unit has an unnamed namespaceThe namespace grouping is written as any other namespace, but no name is giv
37、en: namespace void sample_function( ) /unnamed namespaceSlide 12- 44Names In The unnamed namespaceNames in the unnamed namespaceCan be reused outside the compilation unitCan be used in the compilation unit without a namespace qualifierThe rewritten version of the DigitalTimeinterface is found in whi
38、le the implementation file is shown in Slide 12- 45Display 12.6Display 12.7 (1)Display 12.7 (2)Namespaces In An ApplicationThe application file for the DigitalTime ADT isshown in Slide 12- 46Display 12.8 (1)Display 12.8 (2)Compilation Units OverlapA header file is #included in two filesIt is in two compilation unitsParticipates in two unnamed namespaces!This is OK as long as each of the compilationunits makes sense independent of the otherA name in the header files unnamed n
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年工業(yè)萘項目立項申請報告模板
- 2025年智能化配電與電控裝置項目規(guī)劃申請報告模板
- 函授畢業(yè)生登記表自我鑒定范文15篇
- 2025年汽車安全氣囊及裝置項目提案報告
- 2025年寵物水族項目立項申請報告
- 2025年無菌包裝用包裝材料項目立項申請報告
- 2025年汽車覆蓋件模具項目提案報告模式
- 2024年度水利工程行政合同行政優(yōu)益權(quán)實施要點分析3篇
- 資料員個人工作總結(jié)范文五篇
- 房屋租賃協(xié)議書六篇
- 小學(xué)師德考評細則
- 軟件定義網(wǎng)絡(luò)(SDN)實戰(zhàn)教程課件
- 2024版《大學(xué)生職業(yè)生涯規(guī)劃與就業(yè)指導(dǎo)》 課程教案
- 專題10閱讀理解、拓展探究-2022-2023學(xué)年八年級數(shù)學(xué)上冊期末選填解答壓軸題必刷專題訓(xùn)練(華師大版)(原卷版+解析)
- 西師大版五年級上冊小數(shù)混合運算題100道及答案
- 2024江蘇省鐵路集團限公司春季招聘24人高頻考題難、易錯點模擬試題(共500題)附帶答案詳解
- 2022年7月國家開放大學(xué)本科《中國法律史》期末紙質(zhì)考試試題及答案
- 2024-2025學(xué)年七年級數(shù)學(xué)上冊第一學(xué)期 期末模擬測試卷(湘教版)
- 2024年部門年終工作總結(jié)參考(四篇)
- 企業(yè)反恐專項經(jīng)費保障制度
- 二年級數(shù)學(xué)上冊口算天天練
評論
0/150
提交評論