In-Class Exercise

Basic Concepts


This exercise covers block scope and references. 


Given Information

The following code is extracted from the Example in the Chapter on Modular Programming.

Client Module

main.h

 // Modular Example
 // main.h

 #define NO_TRANSACTIONS 3

main.cpp

 // Modular Example
 // main.cpp

 #include "main.h"
 #include "Transaction.h"

 int main() {
     int i;
     struct Transaction tr;

     for (i = 0; i < NO_TRANSACTIONS; i++) { 
         enter(&tr);
         display(&tr);
     }
 }

Transaction Module

Transaction.h

 // Modular Example
 // Transaction.h

 struct Transaction {
     int acct;
     char type;
     double amount;
 };

 void enter(struct Transaction* tr);
 void display(const struct Transaction* tr); 

Transaction.cpp

 // Modular Example
 // Transaction.cpp

 #include <iostream>
 using namespace std;
 #include "Transaction.h"

 void enter(struct Transaction* tr) {

     cout << "Enter the account number : ";
     cin  >> tr->acct;
     cout << "Enter the account type (d for debit, c for credit) : ";
     cin  >> tr->type;
     cout << "Enter the account amount : ";
     cin  >> tr->amount;
 }

 void display(const struct Transaction* tr) {

     cout << "Account " << tr->acct;
     cout << ((tr->type == 'd') ? " Debit $" : " Credit $") << tr->amount; 
     cout << endl;
 }

Your Task

Incorporate into the above code the following simplifications:

  1. limit the use of the struct keyword wherever possible
  2. define the sentinel i within the for block's scope instead of outside its scope
  3. pass the Transactions by references instead of by address wherever possible

Client Module

In your updated implementation file for the client module fill in the missing code:

  1. define an instance of a Transaction
  2. define the sentinel
  3. insert the proper argument in each function call
 // Basic Concepts
 // h1.cpp

 #include "h1.h"
 #include "Transaction.h"

 int main() {



     for (    i = 0; i < NO_TRANSACTIONS; i++) { 
         enter(    );
         display(   );
     }
 }

Transaction Module

Transaction.h

In your updated header file for the Transaction module fill in the missing code:

  1. simplify the parameter list in each function prototype
  2. pass-by-reference rather than pass-by-address in each prototype
 // Basic Concepts
 // Transaction.h

 struct Transaction {
     int acct;
     char type;
     double amount;
 };

 void enter(                                               ); 
 void display(                                             );

Transaction.cpp

In your updated implementation file for the Transaction module fill in the missing code:

  1. simplify the parameter list in the function header of each function definition
  2. pass-by-reference rather than pass-by-address in each function header
  3. complete the syntax within the body of each function definition
 // Basic Concepts
 // Transaction.cpp

 #include <iostream>
 using namespace std;
 #include "Transaction.h"

 void enter(                                              ) {

     cout << "Enter the account number : ";
     cin  >>                              ;
     cout << "Enter the account type (d for debit, c for credit) : ";
     cin  >>                              ;
     cout << "Enter the account amount : ";
     cin  >>                              ;
 }

 void display(                                            ) {

     cout << "Account " <<                       ;
     cout << ((           == 'd') ? " Debit $" : " Credit $") <<           ; 
     cout << endl;
 }



  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License