Create a class called Employee that includes
a) three instance variables—a first name (type String), a last name (type String) and
a monthly salary (double).
b) Provide a constructor that initializes the three instance variables.
c) Provide set and get functions for each instance variable. If the monthly salary is
not positive, do not set its value.
d) Write complete program. Create two Employee objects and display each object’s
yearly salary. Then give each Employee a 10% raise and display each Employee’s
yearly salary again.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee {
string firstName;
string lastName;
double monthSalary;
public:
Employee(string fName, string lName, double salary) :
firstName(fName), lastName(lName), monthSalary(salary)
{}
string getFirstName() const {
return firstName;
}
void setFirstName(string fName) {
firstName = fName;
}
string getLastName() const {
return lastName;
}
void setLastName(string lName) {
lastName = lName;
}
double getMonthSalary() const {
return monthSalary;
}
void setMonthSalary(double salary) {
if (salary > 0.0)
monthSalary = salary;
}
};
int main() {
Employee alice("Alice", "Smith", 10000);
Employee bob("Bob", "Brown", 12000);
cout << "First employee:" << endl;
cout << alice.getFirstName()
<< " " << alice.getLastName()
<< " $" << fixed << setprecision(2)
<< 12 * alice.getMonthSalary() << endl;
cout << "Second employee:" << endl;
cout << bob.getFirstName()
<< " " << bob.getLastName()
<< " $" << fixed << setprecision(2)
<< 12 * bob.getMonthSalary() << endl;
cout << endl << "Give 10% raise" << endl;
cout << "--------------" << endl;
alice.setMonthSalary(alice.getMonthSalary() * 1.1);
bob.setMonthSalary(bob.getMonthSalary() * 1.1);
cout << "First employee:" << endl;
cout << alice.getFirstName()
<< " " << alice.getLastName()
<< " $" << fixed << setprecision(2)
<< 12 * alice.getMonthSalary() << endl;
cout << "Second employee:" << endl;
cout << bob.getFirstName()
<< " " << bob.getLastName()
<< " $" << fixed << setprecision(2)
<< 12 * bob.getMonthSalary() << endl;
return 0;
}
Comments
Leave a comment