Question
Laboratory Seven Separate compilation OBJECTIVES _______________________________________________________ Introduce a class inside another one Use separate compilation Know how to use multiple classes in the programs Part
Laboratory Seven
Separate compilation
OBJECTIVES
_______________________________________________________
Introduce a class inside another one
Use separate compilation
Know how to use multiple classes in the programs
Part 1: A class inside another class
It is possible to use a class inside another one. For example, suppose ID has three integer parts; left, middle, and right parts. So, one can define a class ID as:
// This program is a driver written to demonstrate how we can use a // class inside another one. #include
class ID { public: ID( ); ID(int, int, int); void display(); private: int left; int middle; int right; };
class Loan // Loan is called structure tag { public: Loan( ); Loan(ID id, float amount, float rate, int term); void set( ); float payment( ); void display( ); private: ID id; // assume an unique integer between 1111-9999 float amount; // $ amount of the loan float rate; // annual interest rate int term; // number of months, length of the loan };
int main( ) { Loan loan1(ID(111,22,4444), 2300, 5.5, 48); // initialize to values given
Loan loan2;
cout << "Display loan1 "; loan1.display();
loan2.set( ); // set the values cout << "Display loan2 "; loan2.display();
return 0; }
ID::ID( ) { // use default values }
ID::ID(int l, int m, int r) { left = l; middle = m; right = r; }
void ID::display() { cout << right << "-" << middle << "-" << right << endl; }
Loan::Loan( ) { }
Loan::Loan(ID I, float am, float rt, int trm) { id = I; amount = am; rate = rt; term = trm; }
void Loan::set( ) { int l, m, r; ID temp_id; // Initialize the loan1 object cout << "Enter the left part of the loan ID "; cin >> l; cout << "Enter the middle part of the loan ID "; cin >> m; cout << "Enter the right part of the loan ID "; cin >> r;
id = ID(l, m, r);
cout << "Enter the amount of this loan "; cin >> amount;
cout << "Enter the annual interest rate of this loan (in %) "; cin >> rate;
cout << "Enter the term (number of months, length of the loan) "; cin >> term; }
void Loan::display() { id.display(); cout << amount << endl; cout << rate << endl; cout << term << endl; }
Part II: 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 understand how the separate compilation of different pieces is done, let's break the above program into several pieces.
As we mentioned before, the definition of a class will be placed in one file and the implementation of it in another file. In the above program, we have two classes, thus, we will have four files;
Two .h files that keep the class definition
Two .cpp files that keep the implementation.
All together we will have 5 files, 3 .cpp files and 2 .h files.
Here is the list of file names:
1) main_prog.cpp This is the main program
2) ID.cpp This file keeps the implementation of class ID 3) ID.h This file keeps the definition of class ID 4) Loan.cpp This file keeps the implementation of class Loan 5) Loan.h This file keeps the definition of class Loan
As you may expect, once you separate the files, you need to let the main function know where to get the information on each class. Also, when you use objects of a different class inside a class, you need to know the definition for the class that is being used. In order to do this we will use the include directive.
For example, now that we have two different files for two classes, at the top of main function we need to have:
#include "ID.h" #include "Loan.h"
Also, we need to include the ID.h in the Loan file, because Loan class uses the ID class. There is something you need to be aware of when you include a .h file at the top of a program, it is as if you have listed the contents of that file there. As you can imagine, if you include something twice, you will get an error. So if a .h file is already included in theLoan.h file, and then you include Loan.h and ID.h at the top of main, you seem to have the ID.h file listed twice.
To fix this problem, we will somehow let the compiler know that the ID.hhas been seen in Loan.h so it won't get listed again. So, for the .h file that may be listed more than once, we include a flag to avoid listing things twice. The flag has three parts, commented below with //1, //2, and //3. You do not need to include the comments in your version.
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started