1 / 10

Chapter 12

Chapter 12. Separate Compilation and Namespaces. Abstract Data Type (ADT). ADT: A data type consisting of data and their behavior. The abstraction is that the users of the class do not have access to the implementation.

efrem
Télécharger la présentation

Chapter 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 12 Separate Compilation and Namespaces

  2. Abstract Data Type (ADT) • ADT: A data type consisting of data and their behavior. • The abstraction is that the users of the class do not have access to the implementation. • The separation of what it does (interface) from how it does it (implementation) is enforced by the use of separate files. These two files compose the class library for that ADT.

  3. Separate Compilation • The separation of what an ADT does (interface) from how it does (implementation) is enforced by the use of separate files. • These two files compose the class library for that ADT. • ADT’s are implemented as separate libraries so that they can be used in multiple applications.

  4. Why separate compilation? • To create libraries that can be used with different applications: Reusability • You can modify your class and not have to modify the programs that use it. • The library is only compiled once and can be used by many applications.

  5. Header file (.h) • Contains the class definition and any other user defined types used by the class. • This is called the class interface. • Requires extensive documentation (pre and post conditions) to give users understandings of what to use. // Date.h #ifndef _DATE_H_ #define _DATE_H_ #include <iostream> … using namespace std; class CDate { … }; #endif

  6. Implementation file (.cpp) • Contains the implementation of all the class functions. • This is called the class implementation of the purpose of each function. // Date.cpp #include “Date.h” CDate::CDate () { month = 1; day = 1; year = 1900; } CDate::CDate (int m, int d, int y) { month = m; day = d; year = y; } …

  7. Application file (.cpp) • contains the program (denoted by “main”) that is using the class. #include “Date.h” using namespace std; void main () { CDate yourBD; cout << "Enter your birthday as month, day and year, separate by spaces: ”; int m, d; cin >> m >> d >> yourBD.year; yourBD.setMonth(m); yourBD.setDay(d); cout << “Your birthday is: " << yourBD.display() << endl; CDate LincohnBD(2, 12, 1809); cout << “Abraham Lincoln's birthday is: " << LincohnBD.display() << endl; CDate twinBrotherBD(yourBD); cout << “Your twin brother’s birthday is: " << twinBrotherBD.display(); }

  8. Data files • usually have a .dat and .txt extension • are supplemental files containing data to support the Project. Conpanies.txt Apple AAPL 450.00Boeing BA 75.50Intel               INTC 22.30Nokia NOK 3.35Rambus RMBS 5.55Sirius SIRI 3.15Xilinx            XLNX 36.80

  9. Linking • The header, implementation and application files are linked together in the Project.

  10. Using #ifndef • Used to ensure that a header file has not already been included resulting in duplicate copies of the class. • States that the header file is not defined previously then use this definition--otherwise, do not include the class again. // Date.h #ifndef _DATE_H_ #define _DATE_H_ #include <iostream> … using namespace std; class CDate { … }; #endif // Date.cpp #include “Date.h” CDate::CDate () { month = 1; day = 1; year = 1900; } CDate::CDate (int m, int d, int y) { month = m; day = d; year = y; }

More Related