100 likes | 198 Views
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.
E N D
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. • 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.
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.
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.
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
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; } …
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(); }
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
Linking • The header, implementation and application files are linked together in the Project.
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; }