Separate Compilation and Namespaces

65 354 0
Separate Compilationand Namespaces

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Separate Compilation and 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 CompilationC++ allows you to divide a program into partsEach part can be stored in a separate fileEach part can be compiled separatelyA 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 ReviewAn ADT is a class defined to separate theinterface and the implementationAll member variables are privateThe class definition along with the function and operator declarations are grouped together as theinterface of the ADTGroup 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 InterfaceThe interface of the ADT includesThe class definitionThe declarations of the basic operations which can be one of the followingPublic member functions Friend functionsOrdinary functionsOverloaded operatorsThe function comments Slide 12- 8Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyThe ADT ImplementationThe implementation of the ADT includesThe function definitionsThe public member functionsThe private member functionsNon-member functionsPrivate helper functionsOverloaded operator definitionsMember variablesOther items required by the definitions Slide 12- 9Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleySeparate 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- 10Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyA Minor CompromiseThe public part of the class definition is part of the ADT interfaceThe private part of the class definition is part of the ADT implementation This would hide it from those using the ADTC++ does not allow splitting the public andprivate parts of the class definition across filesThe 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 . partsEach part can be stored in a separate fileEach part can be compiled separatelyA 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 CompilationC++

Ngày đăng: 12/09/2012, 22:53

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan