Spring 2006 - In-class exercise for 2/16/06
15 points
This exercise should be completed in pairs. There are two purposes to this exercise. The first is to provide experience in the mechanics of writing and using C++ classes. The second is to show how to put together a multi-file project in Visual C++.
0. Create a new project on your network drive. For this
exercise, you will need to download three files: account.h,
account.cpp, and inclass11.cpp from the course
website under today's date. Save these files to your project folder.
Add all three files given for this exercise (account.h,
account.cpp, and inclass11.cpp) to your project using
Project, then Add Existing Item.
1. A specification of a BankAccount class is given below as
well as the analysis and design of a driver program that can be used
to test the class.
Attributes
| Objects | Type | Name |
| Current balance | double | myBalance |
| Interest rate (in decimal form) | double | myRate |
Operations
| Objects | Type | Kind | Movement | Name |
| Initial balance | double | variable | received | initialBalance |
| Initial interest rate | double | variable | received | initialRate |
| Objects | Type | Kind | Movement | Name |
| Current balance | double | variable | returned | myBalance |
| Objects | Type | Kind | Movement | Name |
| Interest rate | double | variable | returned | myRate |
| Objects | Type | Kind | Movement | Name |
| Output stream | double | variable | received & passed back | outStream |
Analysis
| Objects | Type | Kind | Name |
| First bank account | BankAccount | variable | account1 |
| Second bank account | BankAccount | variable | account2 |
| User choice | char | variable | choice |
| First account balance | double | variable | balance1 |
| Second account balance | double | variable | balance2 |
| First account interest rate | double | variable | rate1 |
| Second account interest rate | double | variable | rate2 |
Design
2. It is useful to put class definitions and implementations
into multiple files so that we can reuse them easily. By convention,
each C++ class used in a program is stored in two files. The class
definition is stored in a header file which has the extension
``.h''. The implementations of the class member functions
are stored in a source file with the extension
``.cpp''. The main program is usually stored in a separate
source file. Today's program consists of the header file
account.h and the source file account.cpp containing
the code for the BankAccount class, and the main program source file
inclass11.cpp that contains a driver program that tests the
BankAccount class.
Because you have added all three files to your project, you
can build your project as before. The system will take care of
compiling and linking all the files that make up your project.
Run the program normally. Try several of the choices and
observe the output.
3. (5 points) Study the BankAccount class definition in
account.h. Answer the following questions.
4. (2 points) Study the BankAccount member function
implementations in source file account.cpp. Answer the
following questions.
5. (3 points) Study the main program in inclass11.cpp. This program tests the BankAccount class. Answer the following questions.
cout << "Balance: " << account1.myBalance
<< "Rate: " << account1.myRate << endl;
be allowed in place of the function call
account1.Write(cout)? Why or why not. (Note you can try
this for yourself and see what happens.)
6. (5 points) Add two member functions to the BankAccount
class, one to perform a deposit operation and another to perform a
withdrawal operation, and tests in the driver program to test these
new functions. Here are the analyses and designs for these functions.
| Objects | Type | Kind | Movement | Name |
| Amount to deposit | double | variable | received | amount |
| Objects | Type | Kind | Movement | Name |
| Amount to withdraw | double | variable | received | amount |
The following needs to be done to the project:
When you have completed this exercise, print out all three
files and turn them in with one copy of this exercise sheet with your
answers to the questions.