Đang tải... (xem toàn văn)
Separate Compilationand Namespaces
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyChapter 12Separate Compilationand Namespaces Slide 12- 3Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyOverview12.1 Separate Compilation 12.2 Namespaces Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley12.1Separate Compilation Slide 12- 5Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleySeparate 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 stored separately from a program.This allows you to use the class in multiple programs Slide 12- 6Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyADT 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 as theinterface of the ADTGroup the implementation of the operations togetherand make them unavailable to the programmer using the ADT Slide 12- 7Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyThe ADT InterfaceThe interface of the ADT includesThe class definitionThe declarations of the basic operations which can be one of the followingPublic member functions Friend functionsOrdinary functionsOverloaded operatorsThe function comments Slide 12- 8Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyThe ADT ImplementationThe implementation of the ADT includesThe function definitionsThe public member functionsThe private member functionsNon-member functionsPrivate helper functionsOverloaded operator definitionsMember variablesOther items required by the definitions Slide 12- 9Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleySeparate FilesIn C++ the ADT interface and implementation can be stored in separate filesThe interface file stores the ADT interfaceThe implementation file stores the ADT implementation Slide 12- 10Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyA Minor CompromiseThe public 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 usually in the interface file [...]... using a utility called a linker Linking is often done automatically Slide 12- 9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Separate Files In C++ the ADT interface and implementation can be stored in separate files The interface file stores the ADT interface The implementation file stores the ADT implementation Slide 12- 29 Copyright © 2007 Pearson Education,... using namespace std; (if you do not use the std namespace, you can define cin and cout to behave differently) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces Slide 12- 48 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Naming Namespaces To avoid choosing a name for a namespace that has already... ns1 and ns2, the two versions of my_function could be used in one program by using local using directives this way: Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 Separate Compilation Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Namespaces Slide 12- 28 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Namespaces A... ); } Using directive's scope Display 12.5 (1-2) Namespaces: Using a Function Slide 12- 19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Why Three Files? Using separate files permits The ADT to be used in other programs without rewriting the definition of the class for each Implementation file to be compiled once even if multiple programs use the ADT Changing... namespace The global namespace does not require the using directive Slide 12- 20 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Reusable Components An ADT coded in separate files can be used over and over The reusability of such an ADT class Saves effort since it does not need to be Redesigned Recoded Retested Is likely to result in more reliable components . partsEach part can be stored in a separate fileEach part can be compiled separatelyA class definition can be stored separately from a program.This allows. Pearson Addison-Wesley12. 1Separate Compilation Slide 12- 5Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleySeparate CompilationC++