In-Class Exercise

Member Functions and Privacy


This exercise hides the data members and provides access to them through public member functions.


Given Information

The following code is a solution to the Handout on Basic Concepts.

Client Module

h1.h

 // Basic Concepts
 // h1.h

 #define NO_TRANSACTIONS 3

h1.cpp

 // Basic Concepts
 // h1.cpp

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

 int main() {
     Transaction tr;

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

Transaction Module

Transaction.h

 // Basic Concepts
 // Transaction.h

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

 void enter(Transaction& tr);
 void display(const Transaction& tr); 

Transaction.cpp

 // Basic Concepts
 // Transaction.cpp

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

 void enter(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 Transaction& tr) {

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

Your Task

Convert the global functions into public member functions and the global calls into calls on Transaction objects.

Transaction Module

Transaction.h

The header file for your Transaction module identifies the enter() and display() functions as public member functions and the data members as private.

Update your definition of a Transaction:

  1. convert the global enter() and display() functions into public member functions
  2. identify the data members as private
 // Member Functions and Privacy
 // Transaction.h

 struct Transaction {


     int acct;
     char type;
     double amount;






 };

Transaction.cpp

The implementation file for the Transaction module contains the definitions of the two member functions. 

  1. complete the function headers
  2. refer to the data members directly
 // Member Functions and Privacy
 // Transaction.cpp

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

 void                                {

     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                                             {

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

Client Module

The client program now calls member functions on the Transaction object:

  1. replace the global function calls with calls to the member functions
 // Member Functions and Privacy
 // h2.cpp

 #include "h2.h"
 #include "Transaction.h"

 int main( ) {
     Transaction tr;

     for (int i = 0; i < NO_TRANSACTIONS; i++) { 



     }
 }

Note on Encapsulation

In this example the calls to the member functions have no arguments.  The internal aspects of each Transaction are completely hidden from the client module.  This is one aspect of encapsulation.  We can change the names and the order of the data members within the Transaction module without having to modify any code within any client module.







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