CIS 7124 Lecture 1

CISC 7124
Object-Oriented Programming
Payroll Sample Program in C++
payroll.cpp

 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <iomanip>
 5 
 6 #include "employee.h"
 7 #include "name.h"
 8 #include "salary_info.h"
 9           
10 using namespace std;
11 
12 const int MAX_EMPLOYEES = 100;
13 
14 Employee *find(Employee *employees[], int numEmployees, int empId);
15 string asMoney(int cents);
16 
17 int main() {
18      try {
19           Employee *employees[MAX_EMPLOYEES];
20           int numEmployees = 0;
21 
22           ifstream masterFile("../master.text");
23           if (!masterFile) throw "Unable to open master.text for reading";
24           
25           int empId;
26           masterFile >> empId;
27           while (masterFile) {
28                string last, first;
29                int rate, ytdPay, ytdOvertime;
30                masterFile >> last >> first >> rate >> ytdPay >> ytdOvertime;
31                
32                employees[numEmployees] = new Employee(empId, Name(last, first), SalaryInfo(rate, ytdPay, ytdOvertime));
33                numEmployees++;
34                masterFile >> empId;
35           }    
36           masterFile.close();
37                
38           ifstream hoursFile("../hours.text");
39           if (!hoursFile) throw "Unable to open hours.text for reading"; 
40                
41           hoursFile >> empId; 
42           while (hoursFile) {
43                int hours; 
44                hoursFile >> hours; 
45           
46                Employee *employee = find(employees, numEmployees, empId);
47                if (!employee) 
48                     cerr << "Employee " << empId << " not found in master" << endl;
49                else {                   
50                     int pay = employee->calcPay(hours);
51                     cout << employee->getEmpId() << " " + employee->getName().getLast() << " " << employee->getName().getFirst() <<
52                          " - " << hours << " hours @" << asMoney(employee->getSalaryInfo().getRate()) << "/hr: " << asMoney(pay) << endl;
53                }
54                hoursFile >> empId;
55           }
56           hoursFile.close();
57 
58           ofstream updatedMasterFile("../master.text");
59           if (!updatedMasterFile) throw "Unable to open master.text for writing";
60 
61           for (int i = 0; i < numEmployees; i++)
62                updatedMasterFile <<  *employees[i] << endl;
63           updatedMasterFile.close();
64      } catch (const char *e) {
65           cerr << "*** Exception *** " << e << endl;
66      }
67 }
68 
69 Employee *find(Employee *employees[], int numEmployees, int empId) {
70      for (int i = 0; i < numEmployees; i++)
71           if (empId == employees[i]->getEmpId()) return employees[i];
72      return NULL;
73 }
74 
75 string asMoney(int cents) {
76      ostringstream oss;
77      oss << "$" << cents / 100 << "." << setw(2) << setfill('0') << cents % 100;
78      return oss.str();
79 }
Lines Terms Notes In Java
2-4 Various libraries
  • fstream is the library containing file-related classes
  • sstream is the library containing string-based stream classes
  • iomanip is the library containing types related to manipulating the format of streams
19
  • To declare an array of class objects, the class must have a default constructor
    • Without a default constructor, how would all the lements be initiliazed?
    • The common solution is to use an array of pointers to the objects and create them as necessary
      • This also proves to be a space saving tactic, as one does not declare a slew of objects, only pointers
In Java, this is the standard approach (or at least substutuing 'reference' for 'pointer'
22-23, 38-39, 58-59 File open
  • No runtime error results from Failure to open a file
    • An internal status flag is set iwithin the stream object
    • An implicit conversion operation to bool is defined within the stream — used as a boolean the stream object returns true/false depending upon whether the stream is ready for I/O
Java throws an exception when a file cannot be opened
75-79
  • numeric to string conversion
  • ostringstream
  • To maintain accuracy in our money calculations, we shun double (which generates round-off errors) and instead represent our values as integers — in particular, as the monetary value expressed in cents.
  • In this program, we have no need to convert our money values to string (the only thing we do with these values is output them, and the << operator performs the conversion for us.
  • However, for illustrative purposes, we provide code to show how one would take a numeric value (or any other value for that matter), and transform it into a string using a ostringstream
  • An ostringstream is a stream just like any other (cout or ofstream), except that the output is 'written' to a string, rather than a file or the screen.
    • The resulting 'output' can then be retrieved using the str() member function of the ostringstream object.
The toString methid is used in Java to perform such conversions
77 manipulators C++'s I/O streams have default formats for values sent to them-- e.g. the number of digits of precision for doublesr; however there are times these formats are inappropriate, e.g. one might want to specfiy a minimum number of characters for the diaply of a value (for purposes of tabular output). Manipulators are objects that provide the programmer with such facilities. They are sent to an I/O stream to override the default behavior. In some case the manipulator operates only for the next item read/written; in others, the effect of the manipulator persists until changed. Java provides formatted I/O methods (printf, format) similar to C's scanf/printf functions.