CS 210 - Fundamentals of Programming I

Spring 2006 - In-class exercise for 2/16/06

15 points

Names:

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.

Specification for BankAccount class

Attributes


Objects Type Name
Current balance double myBalance
Interest rate (in decimal form) double myRate


Operations

Main Program

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

  1. Create account1 using default constructor
  2. Create account2 using explicit values 100.0 and 7.0
  3. Do
    3.1
    Display a menu of choices
    3.2
    Read in choice
    3.3
    If choice is 'b' or 'B'
    3.3.1 Compute balance1 = account1.GetBalance()
    3.3.2 Compute balance2 = account2.GetBalance()
    3.3.3 Display balance1, balance2
    3.4
    If choice is 'r' or 'R'
    3.4.1 Compute rate1 = account1.GetRate()*100
    3.4.2 Compute rate2 = account2.GetRate()*100
    3.4.3 Display rate1, rate2
    3.5
    If choice is 'u' or 'U'
    3.5.1 Update first account using account1.Update()
    3.5.2 Update second account using account2.Update()
    3.6
    If choice is 'p' or 'P'
    3.6.1 Display first account using account1.Write(cout)
    3.6.2 Display second account using account2.Write(cout)
  4. While choice is not 'q' or 'Q'


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.

a.
What is the purpose of the compilation guards #ifndef ACCOUNT_H_, #define ACCOUNT_H_, and #endif.






b.
What is the difference between the public: and private: sections of the class definition?









c.
What are the attributes of a BankAccount object? Give their names and types.



d.
How many constructors are there for the BankAccount class? Which one is the default constructor? Why?



e.
List the member functions (that are not constructors) of a BankAccount object.










4. (2 points) Study the BankAccount member function implementations in source file account.cpp. Answer the following questions.

a.
What is the purpose of the line #include "account.h"?






b.
How does the compiler know that the Write function defined in this file is a member function of the BankAccount class and not just some random function definition?






5. (3 points) Study the main program in inclass11.cpp. This program tests the BankAccount class. Answer the following questions.

a.
Which variables are BankAccount objects?



b.
How is the GetBalance member function of the account1 object called?



c.
Would the statement:
     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.

The following needs to be done to the project:

  1. Prototypes for the two functions are added to the BankAccount class definition in account.h
  2. Implementations for the two functions are added to account.cpp
  3. Menu items for the two functions are added to the main program menu display
  4. Additional selection statements are added to the main program to perform the tests of the two functions. Both statements should read in an amount, then call the appropriate function.


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.


Converted using latex2html on Wed Feb 15 21:59:20 CST 2006