Answer to Question #314178 in C++ for Haider

Question #314178

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.

1
Expert's answer
2022-03-19T02:24:50-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog