Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Separate Compilation C++ has facilities for dividing a program into parts that can be kept in different files. These files can be compiled separately and

Separate Compilation C++ has facilities for dividing a program into parts that can be kept in different files. These files can be compiled separately and then linked together when the program is about to run. One thing that is commonly done in programs with classes is that the class definition is placed in one file and the implementation in another. The ADT (Abstract Data Type) is a class that is designed to separate the interface and the implementation of the class. All class definitions should be ADTs. If you wish to define a class as an ADT, then you should separate the specification of how the class is used from the details of how the class is implemented. The separation must be such that you can change the implementation and any program that uses the class can still use it without a need to make any change. In order to illustrate how the separate compilation of different pieces is done, let's look at program P84.cpp and see how we can break it in several pieces. Each piece is shown with a different color. 11. // This and the following 5 lines will go to the main program, main_prog.cpp 2. // Additional include statements should be added 3. // P91.cpp - This program is a driver written to demonstrate how we can use a 4. // class inside another one. 5. Winclude 6. Using namespace std; 7. 8. // This part will go to ID.h file 9 class ID 1e. 11. public: 12. ID: 13. ID(int, int, int); 14. void display(); 15. private 16. int left; 17 int middle; 18. int right; 19. }} 2e. 21. // This part will go to Loan.h file 22. class Loan // Loan class definition 23. 24 public 25. Loan(); 26. Loan (ID id, float amount, float rate, int term); 27 void set(); 28 float payment 29. void display(); 38 private: 31. ID id; // assune an unique integer in three integer parts 32. float amount // $ amount of the loan 33 float rate; // annual interest rate 34 int term; 1/ number of months, length of the loan 35. }; 37. // This part will go to the main program, main_prog.cpp 36. 38. int main() 39. { 40. Loan loani(ID(111, 22,4444), 23ee, 5.5, 48); // initialize to values given 41. Loan loan2; 42. 43. cout > 1; 93. cout > cout > 97 98. id - ID(1, m,r); 95. 99 100. cout > amount; 102. 103. cout > rate; 105. 106. cout > term; 108.) 109. 110. void Loan::display 111. 112. id.display(); 113. cout 5. 6. class ID 17. { 8 public: 9. ID(); 10. ID(int, int, int); 11. void display(); 12. private: 13. int left; 14. int middle; 15. int right; 16.): 17 18. Mendif / 3 What we do is test to see whether the IDH has been visited, or defined. If the .h file has not been defined (#ifndef ID_H), the definition following this statement is used to define the class (#define). If it has been defined, everything between the #ifndef ID_H and #endif is skipped. The format is the same for all other .h files, for example: In the Loan.h file 1. Mifndef Loan_H //1 2. #define Loan_H 1/2 3. 5. Mendif // 3 Assignment #1 Write the above program in five different files as described by the comments at the top of each section. Once you have created the 5 files, include the ID.h and Loan.h file in the main. Make the changes in the other .h files so they will be visited twice. Once you have made all these changes, compile and link all parts to be sure that is working

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions