Access Functions and Utility Functions

Access functions can read or display data. Another common use for access functions is to test the truth or falsity of conditionssuch functions are often called predicate functions. An example of a predicate function would be an isEmpty function for any container classa class capable of holding many objectssuch as a linked list, a stack or a queue. A program might test isEmpty before attempting to read another item from the container object. An isFull predicate function might test a container-class object to determine whether it has no additional room. Useful predicate functions for our Time class might be isAM and isPM.

The program of Figs. 9.59.7 demonstrates the notion of a utility function (also called a helper function). A utility function is not part of a class's public interface; rather, it is a private member function that supports the operation of the class's public member functions. Utility functions are not intended to be used by clients of a class (but can be used by friends of a class, as we will see in Chapter 10).

Figure 9.5. SalesPerson class definition.

1 // Fig. 9.5: SalesPerson.h 2 // SalesPerson class definition. 3 // Member functions defined in SalesPerson.cpp. 4 #ifndef SALESP_H 5 #define SALESP_H 6 7 class SalesPerson 8 { 9 public: 10 SalesPerson(); // constructor 11 void getSalesFromUser(); // input sales from keyboard 12 void setSales( int, double ); // set sales for a specific month 13 void printAnnualSales(); // summarize and print sales 14 private: 15 double totalAnnualSales(); // prototype for utility function 16 double sales[ 12 ]; // 12 monthly sales figures 17 }; // end class SalesPerson 18 19 #endif

Class SalesPerson (Fig. 9.5) declares an array of 12 monthly sales figures (line 16) and the prototypes for the class's constructor and member functions that manipulate the array.

In Fig. 9.6, the SalesPerson constructor (lines 1519) initializes array sales to zero. The public member function setSales (lines 3643) sets the sales figure for one month in array sales. The public member function printAnnualSales (lines 4651) prints the total sales for the last 12 months. The private utility function totalAnnualSales (lines 5462) totals the 12 monthly sales figures for the benefit of printAnnualSales. Member function printAnnualSales edits the sales figures into monetary format.


Figure 9.6. SalesPerson class member-function definitions.

(This item is displayed on pages 492 - 493 in the print version)

1 // Fig. 9.6: SalesPerson.cpp 2 // Member functions for class SalesPerson. 3 #include 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 using std::fixed; 8 9 #include 10 using std::setprecision; 11 12 #include "SalesPerson.h" // include SalesPerson class definition 13 14 // initialize elements of array sales to 0.0 15 SalesPerson::SalesPerson() 16 { 17 for ( int i = 0; i < 12; i++ ) 18 sales[ i ] = 0.0; 19 } // end SalesPerson constructor 20 21 // get 12 sales figures from the user at the keyboard 22 void SalesPerson::getSalesFromUser() 23 { 24 double salesFigure; 25 26 for ( int i = 1; i <= 12; i++ ) 27 { 28 cout << "Enter sales amount for month " << i << ": "; 29 cin >> salesFigure; 30 setSales( i, salesFigure ); 31 } // end for 32 } // end function getSalesFromUser 33 34 // set one of the 12 monthly sales figures; function subtracts 35 // one from month value for proper subscript in sales array 36 void SalesPerson::setSales( int month, double amount ) 37 { 38 // test for valid month and amount values 39 if ( month >= 1 && month <= 12 && amount > 0 ) 40 sales[ month - 1 ] = amount; // adjust for subscripts 0-11 41 else // invalid month or amount value 42 cout << "Invalid month or sales figure" << endl; 43 } // end function setSales 44 45 // print total annual sales (with the help of utility function) 46 void SalesPerson::printAnnualSales() 47 { 48 cout << setprecision( 2 ) << fixed 49 << " The total annual sales are: $" 50 << totalAnnualSales() << endl; // call utility function 51 } // end function printAnnualSales 52 53 // private utility function to total annual sales 54 double SalesPerson::totalAnnualSales() 55 { 56 double total = 0.0; // initialize total 57 58 for ( int i = 0; i < 12; i++ ) // summarize sales results 59 total += sales[ i ]; // add month i sales to total 60 61 return total; 62 } // end function totalAnnualSales

In Fig. 9.7, notice that the application's main function includes only a simple sequence of member-function callsthere are no control statements. The logic of manipulating the sales array is completely encapsulated in class SalesPerson's member functions.

Figure 9.7. Utility function demonstration.

(This item is displayed on pages 493 - 494 in the print version)

1 // Fig. 9.7: fig09_07.cpp 2 // Demonstrating a utility function. 3 // Compile this program with SalesPerson.cpp 4 5 // include SalesPerson class definition from SalesPerson.h 6 #include "SalesPerson.h" 7 8 int main() 9 { 10 SalesPerson s; // create SalesPerson object s 11 12 s.getSalesFromUser(); // note simple sequential code; 13 s.printAnnualSales(); // no control statements in main 14 return 0; 15 } // end main  

Enter sales amount for month 1: 5314.76 Enter sales amount for month 2: 4292.38 Enter sales amount for month 3: 4589.83 Enter sales amount for month 4: 5534.03 Enter sales amount for month 5: 4376.34 Enter sales amount for month 6: 5698.45 Enter sales amount for month 7: 4439.22 Enter sales amount for month 8: 5893.57 Enter sales amount for month 9: 4909.67 Enter sales amount for month 10: 5123.45 Enter sales amount for month 11: 4024.97 Enter sales amount for month 12: 5923.92 The total annual sales are: $60120.59  

Software Engineering Observation 9.8

A phenomenon of object-oriented programming is that once a class is defined, creating and manipulating objects of that class often involve issuing only a simple sequence of memberfunction callsfew, if any, control statements are needed. By contrast, it is common to have control statements in the implementation of a class's member functions.

Категории