I am trying to change this code from STRUCTS to CLASSES, the members have to be private.
Well, I think I did a semi-ok job; the code doesn\'t run and I have no idea why. Can you please
help. Platform: C++
========== C++ CODE ============
#include
#include
#include
#include
#include //must have to use system (\"pause\" );
//#include \"personType.h\"
using namespace std;
const int MAX_EMPLOYEES = 50;
//----------------------------------
class employeeType{ //:public personType
private:
long empID;
string first;
string last;
char gender;
double payrate;
string jobRole;
int years;
public:
virtual void programmer_info() const=0;
//Function to output employee\'s data
virtual double cutBacks(employeeType let[], int listsize) const=0;
//Function to calculate and return the wages.
//Postcondition: Pay is calculated and returned
void yourFired(employeeType let[], int& listsize , long id); //int& cuz we are restando
cantidades de la lista.
//Function to set the salary. /Postcondition: personId = id
long seqSearch(employeeType let[], int listLength, int searchItem)const;
//Function to retrieve the id. /Postcondition: returns personID
employeeType (long id = 0, string first = \"\", string last = \"\", char gender = \"\",
double payrate = 0, string jobRole = \"\",int years = 0);
//Ibefore it was: userinput();
//Constructor with parameters //Sets the first name, last name, payRate, and
//hoursWorked according to the parameters. If no value is specified, the default
//values are assumed. //Postcondition: firstName = first;
///==============================================================
void getData(ifstream& inFile, class employeeType let[], int& listSize);
void printOne ( employeeType one);
void hireOne(employeeType let[], int& listsize); //int& cuz we are adding or restyando
cantidades de la lista.
void selectionSort( employeeType let[], int length);
void printList(employeeType let[], int listSize);
employeeType getOne ( ifstream& dataIn );
};
///===============================================================
void employeeType::yourFired(long id)
{
empID = id;
}
long employeeType::seqSearch() const
{
return empID;
}
employeeType::employeeType(long id, string first, string last, char gender,
double, string jobRole,int years)
: personType(first, last)
{
empID = id;
}
//----------------------------------
int main ()
{
int number; // number of employees in the file
int id;
char choice;
class employeeType [MAX_EMPLOYEES], newrecord;
ifstream dataFile;
dataFile.open ( \"newEmployees.txt\");
if (!dataFile){
cout << \"\ Error with input file!!\ \ \";
//system (\"pause\"); // must #include
return 1;
}
getData (dataFile, employeeType, number);
cout < to be used
/// TASK 3 =======================================================
for(int i =0; i<3 ; i++){ //calling hireOne 3 times, you can insert 3 new employees on a roll
hireOne(employeeType, number); // the list increases by up to 3 new records each time
}
cout << \"After hiring employees, new list: \ \"<> id;
your.
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
1. I am trying to change this code from STRUCTS to CLASSES, the members have to be private.
Well, I think I did a semi-ok job; the code doesn't run and I have no idea why. Can you please
help. Platform: C++
========== C++ CODE ============
#include
#include
#include
#include
#include //must have to use system ("pause" );
//#include "personType.h"
using namespace std;
const int MAX_EMPLOYEES = 50;
//----------------------------------
class employeeType{ //:public personType
private:
long empID;
string first;
string last;
char gender;
double payrate;
string jobRole;
int years;
public:
virtual void programmer_info() const=0;
//Function to output employee's data
virtual double cutBacks(employeeType let[], int listsize) const=0;
//Function to calculate and return the wages.
//Postcondition: Pay is calculated and returned
void yourFired(employeeType let[], int& listsize , long id); //int& cuz we are restando
cantidades de la lista.
//Function to set the salary. /Postcondition: personId = id
long seqSearch(employeeType let[], int listLength, int searchItem)const;
//Function to retrieve the id. /Postcondition: returns personID
employeeType (long id = 0, string first = "", string last = "", char gender = "",
2. double payrate = 0, string jobRole = "",int years = 0);
//Ibefore it was: userinput();
//Constructor with parameters //Sets the first name, last name, payRate, and
//hoursWorked according to the parameters. If no value is specified, the default
//values are assumed. //Postcondition: firstName = first;
///==============================================================
void getData(ifstream& inFile, class employeeType let[], int& listSize);
void printOne ( employeeType one);
void hireOne(employeeType let[], int& listsize); //int& cuz we are adding or restyando
cantidades de la lista.
void selectionSort( employeeType let[], int length);
void printList(employeeType let[], int listSize);
employeeType getOne ( ifstream& dataIn );
};
///===============================================================
void employeeType::yourFired(long id)
{
empID = id;
}
long employeeType::seqSearch() const
{
return empID;
}
employeeType::employeeType(long id, string first, string last, char gender,
double, string jobRole,int years)
: personType(first, last)
{
empID = id;
}
//----------------------------------
int main ()
{
int number; // number of employees in the file
int id;
char choice;
class employeeType [MAX_EMPLOYEES], newrecord;
3. ifstream dataFile;
dataFile.open ( "newEmployees.txt");
if (!dataFile){
cout << " Error with input file!! ";
//system ("pause"); // must #include
return 1;
}
getData (dataFile, employeeType, number);
cout < to be used
/// TASK 3 =======================================================
for(int i =0; i<3 ; i++){ //calling hireOne 3 times, you can insert 3 new employees on a roll
hireOne(employeeType, number); // the list increases by up to 3 new records each time
}
cout << "After hiring employees, new list: "<> id;
yourFired(employeeType, number, id);
cout << " The new employee list after firing employee ID: "
<> one.first >> one.last >> one.gender
>> one.id >> one.payrate >> one.jobRole >> one.years;
printOne(one);
return one;
}
//----------------------------------------------
void printOne ( employeeType one){ //printOne is keep on a loop, use in the structure on the
array printList.
//print out one menuItemType struct
cout << fixed << showpoint << setprecision (2);
cout << "Emp ID Num: " <> data.id;
cout<< " Enter employee First and Last Name: "<> data.first>>data.last;
cout << " Enter employee gender: "<> data.gender;
cout << " Enter employee Job role: "<> data.jobRole;/// TASK 3
=======================================================
cout << " Enter employee Pay rate: "<> data.payrate;
cout << " Enter employee years: "<> data.years;
cout << " ";
//data.blabla .... the diff variables after the 'dot' get consolidated into 'data'
4. return data; //consolidates all (name, last payrate, gender,etc) together into 'data' as a whole
pack of info.
}
/// TASK 3
void hireOne(employeeType let[], int& listsize){
int location;
employeeType newrecord;
cout <<" Enter the location of the new employee should be inserted"<>location;
if(location >= 0 && location < listsize){
newrecord = userinput(); //calling userinput to read/store the newrecord values
for( int i = listsize -1 ; i>= location ; i--)
let[i+1] = let [i];
let[location] = newrecord;
listsize++; //adding a new element to the list, the size of the array increases
} //end of if
else
cout <<" You entered a wrong location"<
Solution
Hii,
I am replying late because the correction took a long time. The major problem with your code
was accessing the private members of employee class by functions which were global. So I had
to modify some lines for the function and the structure of the program accordingly.
Just see if its serving your purpose of what you intended. One good thing it would be if the
employee data are also written to the file before closing the program, thats a good practice of
writing program. That implementation will make the program better, although not necessary
now.
Also make a new text file called "newEmployees.txt" under the same directory where the
program resides and paste these lines. Remember don't press enter after the last line:
Jason Bourne M 8088 81.30 Cyber-Security 2
Bob Beezer M 1234 12.91 ComputerScience 3
Ron Cook M 7841 30.23 Web-Design 4
Alex Musk M 4561 56.15 Electronics 3
The edited program is here(you can copy paste it directly). Please comment if there are any
issues.
#include
5. #include
#include
#include
#include //must have to use system ("pause" );
//#include "personType.h"
using namespace std;
const int MAX_EMPLOYEES = 50;
// class definition
// class employeeType is misleading, just employee is better
class employee{
private:
long emp_id;
string first_name;
string last_name;
char gender;
double payrate;
string jobrole;
int years;
public:
// constructor with default arguments
employee(long id = 0, string f_name = "", string l_name = "", char gen = ' ',double rate = 0,
string role = "",int yr = 0)
{
emp_id = id;
first_name = f_name;
last_name = l_name;
gender = gen;
payrate = rate;
jobrole = role;
years = yr;
}
// class methods that are required to access the private members
void print_emp();
void askinput();
double get_payrate();
void modify_payrate(double);
6. long get_emp_id();
};
// global function declaration i.e., these doesn't come under class employee as was in the given
program
// global functions can't access employee private members
void cutBacks(employee let[], int listsize);
void selectionSort( employee e[], int length);
void hireOne(employee e[], int& listsize);
employee userinput();
void printOne(employee one);
void printList(employee e[],int n);
employee getOne(ifstream& dataIn);
void getData(ifstream& inFile, employee e[], int* listSize);
void yourFired(employee e_list[], int& listsize , int id);
int seqSearch(employee e[], int listLength, int searchItem);
void programmer_info();
// class function definitions
double employee::get_payrate(){
return payrate;
}
void employee::modify_payrate(double m_factor){
payrate = payrate * m_factor;
return;
}
long employee::get_emp_id(){
return emp_id;
}
void employee::print_emp(){
// This will print the information of one employee data
cout << fixed << showpoint << setprecision (2);
cout << " Emp ID Num: " <> emp_id;
cout<< " Enter employee First and Last Name: "<> first_name>>last_name;
cout << " Enter employee gender (M or F): "<> gender;
cout << " Enter employee Job role: "<> jobrole;
cout << " Enter employee Pay rate: "<> payrate;
7. cout << " Enter employee years: "<> years;
cout << " ";
return;
}
int main ()
{
int number; // number of employees in the file
int id;
char choice;
employee emp_list[MAX_EMPLOYEES], newrecord; // declaring two variables of class
employee, one is array and other is a variable
ifstream dataFile;
dataFile.open("newEmployees.txt");
if (!dataFile){
cout << " Error with input file!! ";
return 1;
}
getData(dataFile, emp_list, &number);
printList(emp_list, number);
cout < to be used
// TASK 3 =======================================================
for(int i =0; i<1 ; i++){ //calling hireOne 3 times, you can insert 3 new employees on a roll
hireOne(emp_list, number); // the list increases by up to 3 new records each time
}
cout << "After hiring employees, new list: "<> id;
yourFired(emp_list, number, id);
cout << " The new employee list after firing employee ID "<>location;
if(location > 0 && location < listsize){
newrecord = userinput(); //calling userinput to read/store the newrecord values
for( int i = listsize-1 ; i>= location-1 ; i--)
e[i+1] = e[i];
e[location-1] = newrecord;
listsize++; //adding a new element to the list, the size of the array increases
} //end of if
else
8. cout <<" You entered a wrong location"<> f_name >> l_name >> gen >> id >> rate >> role
>> yr;
employee e(id, f_name, l_name, gen, rate, role, yr);
//e.print_emp();
return e;
}
void getData(ifstream& inFile, employee e[], int* listSize){
// Input employees from the input file
employee item ;
*listSize = 0;
while (!inFile.eof() && (*listSize < MAX_EMPLOYEES))
{
item = getOne(inFile);
e[*listSize] = item ;
(*listSize)++;
cout <<" Current Size = "<< *listSize <