Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using std::cout;
- #include "Employee.h"
- Employee::Employee(string first, string last, int salary)
- {
- setFirstName(first);
- setLastName(last);
- setMonthlySalary(salary);
- }
- void Employee::setFirstName(string name)
- {
- firstName = name;
- }
- string Employee::getFirstName()
- {
- return firstName;
- }
- void Employee::setLastName(string name)
- {
- lastName = name;
- }
- string Employee::getLastName()
- {
- return lastName;
- }
- void Employee::setMonthlySalary(int salary)
- {
- if (salary > 0)
- monthlySalary = salary;
- if (salary <= 0)
- monthlySalary = 0;
- }
- int Employee::getMonthlySalary()
- {
- return monthlySalary;
- }
- #include <iostream>
- #include "Employee.h" // include definition of class Employee
- using namespace std;
- // function main begins program execution
- int main()
- {
- // create two Employee objects
- Employee employee1( "Lisa", "Roberts", 4500 );
- Employee employee2( "Mark", "Stein", 4000 );
- // display each Employee's yearly salary
- cout << "Employees' yearly salaries: " << endl;
- // retrieve and display employee1's monthly salary multiplied by 12
- int monthlySalary1 = employee1.getMonthlySalary();
- cout << employee1.getFirstName() << " " << employee1.getLastName()
- << ": $" << monthlySalary1 * 12 << endl;
- // retrieve and display employee2's monthly salary multiplied by 12
- int monthlySalary2 = employee2.getMonthlySalary();
- cout << employee2.getFirstName() << " " << employee2.getLastName()
- << ": $" << monthlySalary2 * 12 << endl;
- // give each Employee a 10% raise
- employee1.setMonthlySalary( monthlySalary1 * 1.1 );
- employee2.setMonthlySalary( monthlySalary2 * 1.1 );
- // display each Employee's yearly salary again
- cout << "\nEmployees' yearly salaries after 10% raise: " << endl;
- // retrieve and display employee1's monthly salary multiplied by 12
- monthlySalary1 = employee1.getMonthlySalary();
- cout << employee1.getFirstName() << " " << employee1.getLastName()
- << ": $" << monthlySalary1 * 12 << endl;
- monthlySalary2 = employee2.getMonthlySalary();
- cout << employee2.getFirstName() << " " << employee2.getLastName()
- << ": $" << monthlySalary2 * 12 << endl;
- // Some more test cases
- employee1.setMonthlySalary(0);
- cout << "\nMonthly salary is: $" << employee1.getMonthlySalary() << endl;
- employee2.setMonthlySalary(-1);
- cout << "\nMonthly salary is: $" << employee2.getMonthlySalary() << endl;
- employee1.setFirstName("R");
- cout << "The first name is: " << employee1.getFirstName() << endl;
- employee2.setLastName("");
- cout << "The last name is: " << employee2.getLastName() << endl;
- // Create some extra Employee objects to test ctor a bit more
- Employee employee3("", "Z", 0);
- cout << "\nThe first name is: " << employee3.getFirstName() << endl;
- cout << "The last name is: " << employee3.getLastName() << endl;
- cout << "Monthly salary is: $" << employee3.getMonthlySalary() << endl;
- Employee employee4("M", "", -1);
- cout << "\nThe first name is: " << employee4.getFirstName() << endl;
- cout << "The last name is: " << employee4.getLastName() << endl;
- cout << "Monthly salary is: $" << employee4.getMonthlySalary() << endl;
- cout << "\n";
- system("pause");
- } // end main
- #include<string>
- using std::string;
- class Employee {
- public:
- Employee(string, string, int);
- void setFirstName(string);
- string getFirstName();
- void setLastName(string);
- string getLastName();
- void setMonthlySalary(int);
- int getMonthlySalary();
- private:
- string firstName;
- string lastName;
- int monthlySalary;
- };
- OUTPUT SHOULD BE LIKE THIS:
- Employees' yearly salaries:
- Lisa Roberts: $54000
- Mark Stein: $48000
- Employees' yearly salaries after 10% raise:
- Lisa Roberts: $59400
- Mark Stein: $52800
- Monthly salary is: $0
- ERROR - Invalid salary amount specified: $-1
- Salary was left unchanged at: $4400
- Monthly salary is: $4400
- ERROR - invalid length string entered for first name in setFirstName(): R
- The first name was left unchanged: Lisa
- The first name is: Lisa
- ERROR - invalid length string entered for last name in setLastName():
- The last name was left unchanged: Stein
- The last name is: Stein
- Warning - invalid length string entered for first name in ctor:
- The string that was entered will be used for now.
- Warning - invalid length string entered for last name in ctor: Z
- The string that was entered will be used for now.
- The first name is:
- The last name is: Z
- Monthly salary is: $0
- Warning - invalid length string entered for first name in ctor: M
- The string that was entered will be used for now.
- Warning - invalid length string entered for last name in ctor:
- The string that was entered will be used for now.
- Invalid salary amount specified in ctor: $-1
- Salary set to $0 instead.
- The first name is: M
- The last name is:
- Monthly salary is: $0
- Press any key to continue . . .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement