Advertisement
Guest User

A1

a guest
Sep 26th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. #include <iostream>
  2. using std::cout;
  3.  
  4. #include "Employee.h"
  5.  
  6.  
  7. Employee::Employee(string first, string last, int salary)
  8. {
  9.     setFirstName(first);
  10.     setLastName(last);
  11.     setMonthlySalary(salary);
  12.  
  13. }
  14.  
  15. void Employee::setFirstName(string name)
  16. {
  17.         firstName = name;
  18. }
  19.  
  20. string Employee::getFirstName()
  21. {
  22.     return firstName;
  23. }
  24.  
  25. void Employee::setLastName(string name)
  26. {
  27.     lastName = name;
  28. }
  29.  
  30.  
  31. string Employee::getLastName()
  32. {
  33.     return lastName;
  34. }
  35.  
  36.  
  37. void Employee::setMonthlySalary(int salary)
  38. {
  39.     if (salary > 0)
  40.         monthlySalary = salary;
  41.  
  42.     if (salary <= 0)
  43.         monthlySalary = 0;
  44. }
  45.  
  46. int Employee::getMonthlySalary()
  47. {
  48.     return monthlySalary;
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. #include <iostream>
  56. #include "Employee.h" // include definition of class Employee
  57.  
  58. using namespace std;
  59.  
  60. // function main begins program execution
  61. int main()
  62. {
  63.    // create two Employee objects
  64.    Employee employee1( "Lisa", "Roberts", 4500 );
  65.    Employee employee2( "Mark", "Stein", 4000 );
  66.  
  67.    // display each Employee's yearly salary
  68.    cout << "Employees' yearly salaries: " << endl;
  69.  
  70.    // retrieve and display employee1's monthly salary multiplied by 12
  71.    int monthlySalary1 = employee1.getMonthlySalary();
  72.    cout << employee1.getFirstName() << " " << employee1.getLastName()
  73.       << ": $" << monthlySalary1 * 12 << endl;
  74.  
  75.    // retrieve and display employee2's monthly salary multiplied by 12
  76.    int monthlySalary2 = employee2.getMonthlySalary();
  77.    cout << employee2.getFirstName() << " " << employee2.getLastName()
  78.       << ": $" << monthlySalary2 * 12 << endl;
  79.  
  80.    // give each Employee a 10% raise
  81.    employee1.setMonthlySalary( monthlySalary1 * 1.1 );
  82.    employee2.setMonthlySalary( monthlySalary2 * 1.1 );
  83.  
  84.    // display each Employee's yearly salary again
  85.    cout << "\nEmployees' yearly salaries after 10% raise: " << endl;
  86.  
  87.    // retrieve and display employee1's monthly salary multiplied by 12
  88.    monthlySalary1 = employee1.getMonthlySalary();
  89.    cout << employee1.getFirstName() << " " << employee1.getLastName()
  90.       << ": $" << monthlySalary1 * 12 << endl;
  91.    
  92.    monthlySalary2 = employee2.getMonthlySalary();
  93.    cout << employee2.getFirstName() << " " << employee2.getLastName()
  94.       << ": $" << monthlySalary2 * 12 << endl;
  95.  
  96.    // Some more test cases
  97.  
  98.    employee1.setMonthlySalary(0);
  99.    cout << "\nMonthly salary is: $" << employee1.getMonthlySalary() << endl;
  100.  
  101.    employee2.setMonthlySalary(-1);
  102.    cout << "\nMonthly salary is: $" << employee2.getMonthlySalary() << endl;
  103.  
  104.    employee1.setFirstName("R");
  105.    cout << "The first name is: " << employee1.getFirstName() << endl;
  106.  
  107.    employee2.setLastName("");
  108.    cout << "The last name is: " << employee2.getLastName() << endl;
  109.  
  110.  
  111.    // Create some extra Employee objects to test ctor a bit more
  112.  
  113.    Employee employee3("", "Z", 0);
  114.    cout << "\nThe first name is: " << employee3.getFirstName() << endl;
  115.    cout << "The last name is: " << employee3.getLastName() << endl;
  116.    cout << "Monthly salary is: $" << employee3.getMonthlySalary() << endl;
  117.  
  118.    Employee employee4("M", "", -1);
  119.    cout << "\nThe first name is: " << employee4.getFirstName() << endl;
  120.    cout << "The last name is: " << employee4.getLastName() << endl;
  121.    cout << "Monthly salary is: $" << employee4.getMonthlySalary() << endl;
  122.  
  123.    cout << "\n";
  124.  
  125.    system("pause");
  126. } // end main
  127.  
  128.  
  129.  
  130.  
  131. #include<string>
  132. using std::string;
  133.  
  134.  
  135. class Employee {
  136. public:
  137.     Employee(string, string, int);
  138.     void setFirstName(string);
  139.     string getFirstName();
  140.     void setLastName(string);
  141.     string getLastName();
  142.     void setMonthlySalary(int);
  143.     int getMonthlySalary();
  144.  
  145. private:
  146.     string firstName;
  147.     string lastName;
  148.     int monthlySalary;
  149. };
  150.  
  151.  
  152.  
  153. OUTPUT SHOULD BE LIKE THIS:
  154. Employees' yearly salaries:
  155. Lisa Roberts: $54000
  156. Mark Stein: $48000
  157.  
  158. Employees' yearly salaries after 10% raise:
  159. Lisa Roberts: $59400
  160. Mark Stein: $52800
  161.  
  162. Monthly salary is: $0
  163.  
  164. ERROR - Invalid salary amount specified: $-1
  165. Salary was left unchanged at: $4400
  166.  
  167. Monthly salary is: $4400
  168.  
  169. ERROR - invalid length string entered for first name in setFirstName(): R
  170. The first name was left unchanged: Lisa
  171. The first name is: Lisa
  172.  
  173. ERROR - invalid length string entered for last name in setLastName():
  174. The last name was left unchanged: Stein
  175. The last name is: Stein
  176.  
  177. Warning - invalid length string entered for first name in ctor:
  178. The string that was entered will be used for now.
  179.  
  180. Warning - invalid length string entered for last name in ctor: Z
  181. The string that was entered will be used for now.
  182.  
  183. The first name is:
  184. The last name is: Z
  185. Monthly salary is: $0
  186.  
  187. Warning - invalid length string entered for first name in ctor: M
  188. The string that was entered will be used for now.
  189.  
  190. Warning - invalid length string entered for last name in ctor:
  191. The string that was entered will be used for now.
  192.  
  193. Invalid salary amount specified in ctor: $-1
  194. Salary set to $0 instead.
  195.  
  196. The first name is: M
  197. The last name is:
  198. Monthly salary is: $0
  199.  
  200. Press any key to continue . . .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement