14. Create a class called Employee that includes three pieces of information as data members a first name, a last name and a monthly salary. Provide a set and a get function for each data member. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10 percent raise and display each Employee’s yearly salary again.

C++ How to Program (8th Edition)

Chapter 3

Introduction to Classes, Objects and Strings

Exercise

Problem # 14:

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). 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 10 percent raise and display each Employee’s yearly salary again.

#include<iostream>
#include <string>
using namespace std;

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;
};
Employee::Employee( string first, string last, int salary)
{
 firstName=first;
 lastName=last;
 if(salary>0)
  monthlySalary=salary;
 else
 {
  monthlySalary=0;
  cout<<"Initial salary was invalid."<<endl;
 }
}
void Employee::setFirstName( string first)
{
 if ( first.length() <= 25 )
  firstName = first;
 if ( first.length() > 25 )
 {
  firstName = first.substr( 0, 25 );
  cout << "Name \"" << first <<"\" exceeds maximum length (25).\n"<< "Limiting firstName to first 25 characters.\n" << endl;
 }
}
void Employee::setLastName(string last )
{
 if ( last.length() <= 25 )
  lastName = last;
 if ( last.length() > 25 )
 {
  lastName = last.substr( 0, 25 );
  cout << "Name \"" << last <<"\" exceeds maximum length (25).\n"<< "Limiting lastName to first 25 characters.\n" << endl;
 }
}
void Employee::setMonthlySalary(int salary )
{
 if(salary>0)
  monthlySalary=salary;
 else
 {
  monthlySalary=0;
  cout<<"Initial salary was invalid."<<endl;
 }
}
string Employee::getFirstName()
{
 return firstName;
}
string Employee::getLastName()
{
 return lastName;
}
int Employee::getMonthlySalary()
{
 return monthlySalary;
}
int main()
{
 Employee Employee1("James","Smith",1500);
 Employee Employee2("Zhang","Wei",2000);
 cout << "Employee1's name is: "<< Employee1.getFirstName()<<" "<< Employee1.getLastName()<<endl;
 cout<< "and monthly salary is: "<< Employee1.getMonthlySalary()<< " and annual salary is: "<< Employee1.getMonthlySalary()*12<< endl;
 cout<<"After 10 percent increment in salary:"<<endl;
 int incrementedSalary1=Employee1.getMonthlySalary();
 incrementedSalary1=incrementedSalary1+(incrementedSalary1*0.1);
 Employee1.setMonthlySalary(incrementedSalary1);
 cout<< "Employee1's monthly salary is: "<< Employee1.getMonthlySalary()<< " and annual salary is: "<< Employee1.getMonthlySalary()*12<<endl<<endl;
 
 cout << "Employee2's name is: "<< Employee2.getFirstName()<<" "<< Employee2.getLastName()<<endl;
 cout<< "and monthly salary is: "<< Employee2.getMonthlySalary()<< " and annual salary is: "<< Employee2.getMonthlySalary()*12<< endl;
 cout<<"After 10 percent increment in salary:"<<endl;
 int incrementedSalary2=Employee1.getMonthlySalary();
 incrementedSalary2=incrementedSalary2+(incrementedSalary2*0.1);
 Employee2.setMonthlySalary(incrementedSalary2);
 cout<< "Employee2's monthly salary is: "<< Employee1.getMonthlySalary()<< " and annual salary is: "<< Employee1.getMonthlySalary()*12<< endl;
}

Let me know in the comment section if you have any question.

Similar Posts:

Chapter 3

  1. Modify class GradeBook, Include a string data member for course instructor’s name, Provide a set function and a get function to change and retrieve it, Modify display Message to output the welcome message and course name.
  2. Create an Account class that a bank might use to represent customers bank accounts. Include a data member to represent the account balance. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account. Member function get Balance should return the current balance.
  3. Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four data members a part number, a part description, a quantity of the item being purchased and a price per item.
  4. Create a class called Date that includes three pieces of information as data members a month, a day and a year. Provide a set and a get function for each data member. Provide a member function display Date that displays the month, day and year separated by forward slashes (/). 

 Chapter 2


Comments