// 5/8/14 class example // Payroll Program // This version adds: NESTED classes, objects passed as parameters (both by value and reference) // and an object returned from a function #include #include using namespace std; // Class Declaration -- like a blueprint NO MEMORY is allocated class Address { public: int housenumber; string street; string city; string state; string country; string zipcode; }; class Name { public: string first; string middle; string last; }; class Employee { public: Name name; Address a; double hours; double rate; int idnum; }; //prototypes void printEmployee(Employee); // pass by value void readinEmployee(Employee&); // pass by reference Employee readinEmployee2(); int main() { // instantiate an object of type Employee Employee emp1, emp2; double pay; // read in using the function that accepts an object by reference readinEmployee(emp1); printEmployee(emp1); pay = emp1.hours * emp1.rate; cout << "Pay is: " << pay << endl; // NOT allowed: cout << emp1; // read in using the function with the return value emp2 = readinEmployee2(); printEmployee(emp2); pay = emp2.hours * emp2.rate; cout << "Pay is: " << pay << endl; // NOW, set the pay rate for emp1 to be that of emp2 // emp1.rate = emp2.rate; Employee newemp; // copy everything from emp1 to newemp // ASSIGNMENT OF OBJECTS IS LEGAL newemp=emp1; // Display all the info cout << "now newemp is identical to emp1...\n"; printEmployee(newemp); return 0; } // object are by default PASS BY VALUE void printEmployee(Employee e) { cout << "in the function printEmplyee: " << endl; // Display all the info for an employee object emp cout << "Name: " << e.name.first << ' ' << e.name.last << endl << "Address: " << e.a.housenumber << ' ' << e.a.street << ' ' << e.a.city << endl << "\nHours: " << e.hours << "\nRate: " << e.rate << "\nidnum: " << e.idnum << endl; } // read in one employee object and put into reference parameter void readinEmployee(Employee &e) { cout << "Enter an id number of an employee: "; cin >> e.idnum; cout << "Enter hours and rate: "; cin >> e.hours >> e.rate; cout << "enter first name: "; cin >> e.name.first; cout << "enter last name: " ; cin >> e.name.last; // read in address cout << "Enter house number: "; cin >> e.a.housenumber; cout << "Enter street: "; cin >> e.a.street; cout << "Enter city: "; cin >> e.a.city; // do this for all members } Employee readinEmployee2() { Employee e; cout << "Enter an id number of an employee: "; cin >> e.idnum; cout << "Enter hours and rate: "; cin >> e.hours >> e.rate; cout << "enter first name: "; cin >> e.name.first; cout << "enter last name: " ; cin >> e.name.last; // read in address cout << "Enter house number: "; cin >> e.a.housenumber; cout << "Enter street: "; cin >> e.a.street; cout << "Enter city: "; cin >> e.a.city; // do this for all members return e; } /* LAB 1 for today: Declare 2 classes: Brand: make and model Car: Brand b, year, mileage, price In main, instantiate 2 objects of type Car. Read in values for both objects and print them. Then, print the one with the cheaper price. */