(Employee Class) Create a class called Employee that includes three pieces of information as data members—a first name (type string), a last name (type string) and a monthly salary (type int). [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)— called floatingpoint values—to represent dollar amounts.]
Your class should have a constructor that initializes the three data members. Provide a set and a get function for each data member.
If the monthly salary is not positive, set it to 0. Write a test program that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 1 0 percent raise and display each Employee’s yearly salary again.
Solution:
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; #include "Employee. h" int main() { Employee employee1 ( "Jack" , "Miller", 1000) ; Employee employee2 ( "Bill" , "Clinton", 2000) ; cout << "The new employees: \n" << employee1. getFirstName() << " " << employee1. getLastName() << " " << employee1. getSalary() << endl; cout << employee2. getFirstName() << " " << employee2. getLastName() << " " << employee2. getSalary() << endl << endl; cout << "Yearly salary of employees: \n" << employee1. getFirstName() << " " << employee1. getLastName() << " " << (employee1. getSalary() ) *12 << endl; cout << employee2. getFirstName() << " " << employee2. getLastName() << " " << (employee2. getSalary() ) *12 << endl; cout << "Increased yearly salary of employees: \n" << employee1. getFirstName() << " " << employee1. getLastName() << " " << (employee1. getSalary() ) *12*1. 1 << endl; cout << employee2. getFirstName() << " " << employee2. getLastName() << " " << (employee2. getSalary() ) *12*1. 1 << endl; system("PAUSE") ; return 0; }
// Employee. h file #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; class Employee {p ublic: Employee(string initFirstName, string initLastName, int initSalary) ; void setFirstName( string ) ; string getFirstName() ; void setLastName( string ) ; string getLastName() ; void setSalary( int ) ; int getSalary() ; private: string firstName; string lastName; int salary; };
// Employee. cpp file #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; #include "Employee. h" //initialization with constructor Employee: : Employee(string initFirstName, string initLastName, int initSalary) { setFirstName( initFirstName) ; setLastName( initLastName) ; setSalary( initSalary) ; } //function for set first name void Employee: : setFirstName( string initFirstName) { firstName = initFirstName; }
//function for get first name
string Employee: : getFirstName()
{
return firstName;
}
//function for set last name void Employee: : setLastName( string initLastName) { lastName = initLastName; } string Employee: : getLastName() { return lastName; }
//function for set salary void Employee: : setSalary( int initSalary) { if (initSalary < 0) { cout << "The salary amount can' t be negative. Salary set as 0" << endl; salary = 0; } else { salary = initSalary; } }
//function for get salary int Employee: : getSalary() { return salary; }
Post A Comment:
0 comments: