Last active
January 7, 2023 02:10
-
-
Save EteimZ/359889c84ecacc4946772acf3b8e3944 to your computer and use it in GitHub Desktop.
An example demonstrating the use of utility functions within classes in OOP. Gotten from the book C How to Program.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Demonstrating utility function | |
| #include "SalesPerson.h" | |
| int main(){ | |
| SalesPerson s; // create a SalesPerson object | |
| s.getSalesFromUser(); | |
| s.printAnnualSales(); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Implementation for class SalesPerson | |
| #include <iostream> | |
| using std::cout; | |
| using std::cin; | |
| using std::endl; | |
| using std::fixed; | |
| #include <iomanip> | |
| using std::setprecision; | |
| #include "SalesPerson.h" // Include SalesPerson definition class | |
| // Initialize elements of array sales to 0.0 | |
| SalesPerson::SalesPerson(){ | |
| for ( int i = 0; i < 12; i++ ) | |
| sales[ i ] = 0.0; | |
| } | |
| // get sales figure from the user | |
| void SalesPerson::getSalesFromUser(){ | |
| double salesFigure; | |
| for ( int i = 1; i <= 12; i++ ){ | |
| cout << "Enter sales account for month " << i << ": "; | |
| cin >> salesFigure; | |
| setSales( i, salesFigure ); | |
| } | |
| } | |
| // set sales for a particular month in the array | |
| void SalesPerson::setSales( int month, double amount ){ | |
| // test for valid month and amount values | |
| if ( month >= 1 && month <= 12 && amount > 0 ) | |
| sales[month - 1] = amount; // adjust for subscription 0-11 | |
| else | |
| cout << "Invalid month or sales figure" << endl; | |
| } | |
| // print total annual sales using the private utility function | |
| void SalesPerson::printAnnualSales(){ | |
| cout << setprecision( 2 ) << fixed | |
| << "\nThe total anual sales are: " | |
| << totalAnnualSales() << endl; | |
| } | |
| // private utility function to total annual sales | |
| double SalesPerson::totalAnnualSales(){ | |
| double total = 0.0 ; // initialize total | |
| for ( int i = 0; i < 12; i++ ) // summarize sales result | |
| total += sales[ i ]; | |
| return total; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SalesPerson class definition | |
| #ifndef SALESP_H | |
| #define SALESP_H | |
| class SalesPerson{ | |
| public: | |
| SalesPerson(); // constructor | |
| void getSalesFromUser(); // input sales from keyboard | |
| void setSales( int, double ); // set sales for specific month | |
| void printAnnualSales(); // summarize and print sales | |
| private: | |
| double totalAnnualSales(); // prototype for utility function | |
| double sales[ 12 ]; // 12 monthly sales figures | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment