//============================================================ // SOLUTIONS TO HOMEWORK 1 //============================================================ // HOMEWORK 1 //------------------------------------------------------------ // DUE DATE: Wednesday, 2/22/2006 //============================================================ // Modify the program below as follows // // 1) Add datafields to represent the address of an employee. // // a) Modify the constructors so they properly initialize // these new data fields // // b) Modify the print function to print out the new address // information // // c) Add a function to return the ZIP code of an employee // // d) Change the main program, so that is prints out a list // of all employees who live in an area with ZIP code 12345 // and gives them a $10000 raise // // e) Change the program such that the first employee who lives // in ZIP-code 06660 will be fired // // // 2) Answer the following questions in English: // // a) What is the difference between a class declaration // and a class implementation? // // b) What are constructors being used for and what do they need to do? // // c) Please explain what the term "encapsulation" means (we will discuss // this during the lecture on Tuesday) and why it is useful #include #include #include using namespace std; //************************************************************ // Employee class // class Employee { private: string name; string position; double salary; string ssn; string street; string city; string state; string zip; public: // Constructors Employee(); Employee(string n, string p, double s, string ssn_num, string str, string cty, string st, string zip_code); // Data Access string get_name(); string get_position(); double get_salary(); string get_ssn(); string get_zip(); // Change Data void promote(string p, double s); void fire(); void give_raise_absolute(double s); void give_raise_percent(double p); // Task Functions void print(); }; //---------------------------------------- // Employee - Constructors // Employee::Employee() { name = ""; position = ""; salary = 0; ssn = ""; street = ""; city = ""; state = ""; zip = ""; } Employee:: Employee(string n, string p, double s, string ssn_num, string str, string cty, string st, string zip_code) { name = n; position = p; if (s > 50000) salary = 50000; else salary = s; ssn = ssn_num; street = str; city = cty; state = st; zip = zip_code; }; //---------------------------------------- // Employee - Data Access // string Employee::get_name() { return name; } string Employee::get_position() { return position; } double Employee::get_salary() { return salary; } string Employee::get_ssn() { return ssn; } string Employee::get_zip() { return zip; } //---------------------------------------- // Employee - Change Data // void Employee::promote(string p, double s) { position = p; salary = s; } void Employee::fire() { position = "Former Employee"; salary = 0; } void Employee::give_raise_absolute(double s) { salary = salary + s; } void Employee::give_raise_percent(double p) { salary = salary * (1+p/100.0); } //---------------------------------------- // Employee - Task Functions // void Employee::print() { cout << "========================================\n"; cout << " Employee: " << name << "\n"; cout << "----------------------------------------\n"; cout << " Current Position: " << position << "\n"; cout << " Current Salary: " << salary << "\n"; cout << " SSN: " << ssn << "\n"; cout << " Address: " << street << "\n"; cout << " " << city << ", " << state << " " << zip << "\n"; cout << "========================================\n"; } //============================================================ int main() { //Employee a("Alice Accountant", "Payroll", 40000); //a.print(); //a.promote("Chief Payroll officer", 80000); vector employees(3);; int i; for (i = 0; i < 3; i = i+1) { string name; string position; double salary; string ssn; string street; string city; string state; string zip; cout << "Please enter name: "; cin >> name; cout << "Please enter position: "; cin >> position; cout << "Please enter salary: "; cin >> salary; cout << "Please enter ssn: "; cin >> ssn; cout << "Please enter street address: "; cin >> street; cout << "Please enter city: "; cin >> city; cout << "Please enter state: "; cin >> state; cout << "Please enter zip: "; cin >> zip; employees[i] = Employee(name, position, salary, ssn, street, city, state, zip); } cout << "Done entering data\n"; //print all employees for (i = 0; i < 3; i = i+1) employees[i].print(); //give everybody living in zip 12345 a raise for (i = 0; i < 3; i = i+1) if (employees[i].get_zip() == "12345") employees[i].give_raise_absolute(10000); //print all employees for (i = 0; i < 3; i = i+1) employees[i].print(); // fire everybody at zip 06660 for (i = 0; i < 3; i = i+1) if (employees[i].get_zip() == "06660") employees[i].fire(); //print all employees for (i = 0; i < 3; i = i+1) employees[i].print(); return 0; }