版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年工业萘项目立项申请报告模板
- 2025年智能化配电与电控装置项目规划申请报告模板
- 函授毕业生登记表自我鉴定范文15篇
- 2025年汽车安全气囊及装置项目提案报告
- 2025年宠物水族项目立项申请报告
- 2025年无菌包装用包装材料项目立项申请报告
- 2025年汽车覆盖件模具项目提案报告模式
- 2024年度水利工程行政合同行政优益权实施要点分析3篇
- 资料员个人工作总结范文五篇
- 房屋租赁协议书六篇
- 小学师德考评细则
- 软件定义网络(SDN)实战教程课件
- 2024版《大学生职业生涯规划与就业指导》 课程教案
- 专题10阅读理解、拓展探究-2022-2023学年八年级数学上册期末选填解答压轴题必刷专题训练(华师大版)(原卷版+解析)
- 西师大版五年级上册小数混合运算题100道及答案
- 2024江苏省铁路集团限公司春季招聘24人高频考题难、易错点模拟试题(共500题)附带答案详解
- 2022年7月国家开放大学本科《中国法律史》期末纸质考试试题及答案
- 2024-2025学年七年级数学上册第一学期 期末模拟测试卷(湘教版)
- 2024年部门年终工作总结参考(四篇)
- 企业反恐专项经费保障制度
- 二年级数学上册口算天天练
评论
0/150
提交评论