// pgm38 // 12/7/16 class example // Payroll Program // Nested Classes and functions #include #include using namespace std; // Class Declaration -- like a blueprint NO MEMORY is allocated // this creates a new user defined Data Type class PersonName { public: string first; char middleinit; string last; }; class Address { public: int housenumber; string street; string city; string state; int zipcode; }; class Employee { public: PersonName name; Address a; double hours; double rate; int idnum; }; // prototypes void readEmployee(Employee&); void displayEmployee(Employee); double calcPay(Employee); int main() { // instantiate objects of type Employee Employee emp1, emp2; double pay; readEmployee(emp1); displayEmployee(emp1); pay = calcPay(emp1); cout << "Pay for emp1 is: " << pay << endl; readEmployee(emp2); displayEmployee(emp2); pay = calcPay(emp2); cout << "Pay for emp2 is: " << pay << endl; return 0; } // display all of the info of an Employee object void displayEmployee(Employee emp1) { cout << "Name: " << emp1.name.first << ' ' << emp1.name.last << "\nHours: " << emp1.hours << "\nRate: " << emp1.rate << "\nidnum: " << emp1.idnum << endl; cout << "Address: " << emp1.a.housenumber << ' ' << emp1.a.street << ' ' << emp1.a.city << endl; } // read in data members for an Employee object void readEmployee(Employee& emp1) { cout << "Enter an id number of an employee: "; cin >> emp1.idnum; cout << "Enter hours and rate: "; cin >> emp1.hours >> emp1.rate; cout << "enter first name: "; cin >> emp1.name.first; cout << "enter last name: "; cin >> emp1.name.last; cout << "enter middle initial: "; cin >> emp1.name.middleinit; // read in address also cout << "Enter house number: "; cin >> emp1.a.housenumber; cout << "Enter street: "; cin >> emp1.a.street; cout << "Enter city: "; cin >> emp1.a.city; // do this for all members } // this function calculates the pay for an Employee // and returns the pay double calcPay(Employee emp) { double pay; pay = emp.hours * emp.rate; return pay; }