In: Computer Science
C++
Define a class PersonalRecord, which will be storing an employee’s information.
Internally, the class should have the following attributes:
SSN (string type), fullName (string type), homeAddress (string type), phoneNumber (string type), salaryRate (per year, float type), and vacationDays (accrued vacation, int type).
The constructor should take six parameters:
four of them are necessary to provide values to at the time of initialization (when a constructor will be called/invoked): SSN, fullName, homeAddress, and phoneNumber;
and two of them are optional parameters: salaryRate, vacationDays with values by default to be 0 for each.
Provide natural implementation for the member functions:
getName(), setName(name), getSalary(), and setSalary(salary).
Use the header file (.h) and the source-code file (.cpp) to separate the interface of the class from its implementation.
You don’t need to incorporate any checks on validity of data.
The test code given below creates two records for the following people (if you want to use it for testing, put in into a separate .cpp file):
John Hue; SSN: 111119911; Address: 234 Avenue B, Brooklyn, NY; phone number: (718) 222-3445; salary: $35000.
Mary Jones; SSN: 222334455; Address: 123 Kings Avenue, Apt. 10, Queens, NY; phone
number: (718) 555-3112.
Then calls some of the methods to see that they are working.
Feel free to use a C++ code editor. When putting the answer below, first put the content of the header file, followed with
//--------------- implementation
comment, then put the content of the source-code file.
Test code:
#include <iostream>
#include "PersonalRecord.h"
using namespace std;
int main() {
PersonalRecord p1("111119911", "John Hue", "234 Avenue B, Brooklyn, NY", "(718)222-3445", 35000);
cout << p1.getName() << " earns $" << p1.getSalary() << " per year\n";
PersonalRecord p2("222334455", "Mary Jones", "123 Kings Avenue, Apt. 10, Queens, NY", "(718)555-3112");
cout << p2.getName() << " earns $" << p2.getSalary() << " per year\n"; cout << " after the information is updated: " << endl;
p2.setName("Mary A Jones");
p2.setSalary(50000);
cout << p2.getName() << " earns $" << p2.getSalary() << " per year\n";
}
//PersonalRecord.h
//PersonalRecord header file
#include<iostream>
class PersonalRecord{
private:
//private data members
std::string SSN, fullName, homeAddress,phoneNumber;
float salaryRate;
int vacationDays;
public:
//constructors with four parameters
PersonalRecord(std::string SSN, std::string fullName,
std::string homeAddress, std::string phoneNumber);
//constructors with five parameters
PersonalRecord(std::string SSN, std::string fullName,
std::string homeAddress, std::string phoneNumber,
float salaryRate, int vacationDays);
//constructors with six parameters
PersonalRecord(std::string SSN, std::string fullName,
std::string homeAddress, std::string phoneNumber,
float salaryRate);
//method to set name
void setName(std::string name);
//method returns name
std::string getName ();
//method to set salary
void setSalary(float salary);
//method returns salary
float getSalary();
};
//PersonalRecord.cpp
#include"PersonalRecord.h"
//PersonalRecord.cpp
//implementation file
//constructors
PersonalRecord::PersonalRecord(std::string SSN,
std::string fullName,
std::string homeAddress, std::string phoneNumber){
this->fullName = fullName;
this->SSN = SSN;
this->homeAddress = homeAddress;
this->phoneNumber = phoneNumber;
this->salaryRate = 0;
this->vacationDays = 0;
}
PersonalRecord::PersonalRecord(std::string SSN,
std::string fullName,
std::string homeAddress, std::string phoneNumber,
float salaryRate){
this->fullName = fullName;
this->SSN = SSN;
this->homeAddress = homeAddress;
this->phoneNumber = phoneNumber;
this->salaryRate = salaryRate;
this->vacationDays = 0;
}
PersonalRecord::PersonalRecord(std::string SSN,
std::string fullName,
std::string homeAddress, std::string phoneNumber,
float salaryRate, int vacationDays){
this->fullName = fullName;
this->SSN = SSN;
this->homeAddress = homeAddress;
this->phoneNumber = phoneNumber;
this->salaryRate = salaryRate;
this->vacationDays = vacationDays;
}
//member functions
void PersonalRecord:: setName(std::string name){
this->fullName = name;
}
std::string PersonalRecord::getName (){
return this->fullName;
}
void PersonalRecord::setSalary(float salary){
this->salaryRate = salary;
}
float PersonalRecord::getSalary(){
return this->salaryRate;
}
//Main.cpp
#include <iostream>
#include "PersonalRecord.h"
using namespace std;
int main() {
PersonalRecord p1("111119911", "John Hue", "234 Avenue
B, Brooklyn, NY", "(718)222-3445", 35000);
cout << p1.getName() << " earns $" <<
p1.getSalary() << " per year\n";
PersonalRecord p2("222334455", "Mary Jones", "123 Kings
Avenue, Apt. 10, Queens, NY", "(718)555-3112");
cout << p2.getName() << " earns $" <<
p2.getSalary() << " per year\n";
cout << " after the information is updated: " <<
endl;
p2.setName("Mary A Jones");
p2.setSalary(50000);
cout << p2.getName() << " earns $" << p2.getSalary() << " per year\n";
}
/*
Output
John Hue earns $35000 per year
Mary Jones earns $0 per year
after the information is updated:
Mary A Jones earns $50000 per year
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
*/