Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.

Similar presentations


Presentation on theme: "Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that."— 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 … 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. AppleAAPL BoeingBA75.50 Intel INTC22.30 NokiaNOK3.35 RambusRMBS5.55 SiriusSIRI3.15 Xilinx XLNX36.80 Conpanies.txt

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 … 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; }


Download ppt "Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that."

Similar presentations


Ads by Google