Having issues with passing my values through different functions after \"Employe
ID: 3791041 • Letter: H
Question
Having issues with passing my values through different functions after "EmployeeRecord()".
Problem: This database must be capable of maintaining the employee ID, employee name, department, and annual salary of each sales rep. The first phase of development for this database will be to create the EmployeeRecord class.
Header File
#pragma once
class EmployeeRecord
{
private:
int m_iEmployeeID;
char m_sLastName[32];
char m_sFirstName[32];
int m_iDeptID;
double m_dSalary;
public:
EmployeeRecord(); //The default constructor
EmployeeRecord(int ID, char *fName, char *lName, int dept, double sal);// Constructor shall set the member values passed into the function.
~EmployeeRecord();// The destructor
int getID();// shall return the value stored in the member variable
void setID(int ID);// will set the member variable m_iEmployeeID to the value of its' argument.
void getName(char *fName, char *lName);// The getName() function shall copy the member variables m_sFirstName and m_sLastName into the character arrays pointed to by the function arguments.
void setName(char *fName, char *lName);// The setName() function will copy the function arguments into the member variables m_sFirstName and m_sLastName.
void getDept(int& d);// The getDept() function shall be defined as a reference function. That is, a call to this function will copy the member variable m_iDeptID into the int variable referenced by the function argument.
void setDept(int d);// The setDept() function will copy the function argument into the member variable m_iDeptID.
void getSalary(double *sal);// he getSalary() function shall be defined as a pointer function.
void setSalary(double sal);//the function setSalary() shall copy the function argument into the member variable m_dSalary.
void printRecord(); //This function shall print to the screen all data found in the employee's record.
};
Main File
#include <iostream>
#include "EmployeeRecord.h"
#include <string>
#include <cstdlib>
using namespace std;
//Default Constructor
EmployeeRecord::EmployeeRecord()
{
// The default constructor shall set the member variables to the following
m_iEmployeeID = 0;
m_sFirstName[0] = '';
m_sLastName[0] = '';
m_iDeptID = 0;
m_dSalary = 0.0;
}
EmployeeRecord::EmployeeRecord(int ID, char *fName, char *lName, int dept, double sal)
{
fName=NULL;
lName=NULL;
strcpy(m_sFirstName, fName);
strcpy(m_sLastName, lName);
}
// Default Desctrutor
EmployeeRecord::~EmployeeRecord()
{
// It was tested in sprint 1
}
int EmployeeRecord:: getID()
{
return m_iEmployeeID;
}
void EmployeeRecord::setID(int ID)
{
m_iEmployeeID = ID;
}
void EmployeeRecord::getName(char *fName, char *lName)
{
strcpy(m_sFirstName, fName);
strcpy(m_sLastName, lName);
/*char *newString = new char[strlen(theString) + 1];
strcpy(newString, theString);
return newString;*/
}
void EmployeeRecord::setName(char *fName, char *lName)
{
/*clearString(); // Clear the current string, if any
theString = new char[strlen(str) + 1]; // Allocate memory for new string
strcpy(theString, str); // Copy string argument into new memory space.*/
}
void EmployeeRecord::getDept(int& d)
{
d = m_iDeptID;
}
void EmployeeRecord::setDept(int d)
{
m_iDeptID = d;
}
void EmployeeRecord::getSalary(double *sal)
{
*sal = m_dSalary;
}
void EmployeeRecord::setSalary(double sal)
{
m_dSalary = sal;
}
void EmployeeRecord::printRecord()
{
cout << "ID: " << m_iEmployeeID << endl;
cout << "Last Name: " << m_sLastName << endl;
cout << "First Name: " << m_sFirstName << endl;
cout << "Dept: " << m_iDeptID << endl;
cout << "Salary: " << m_dSalary << endl;
}
int main(void)
{
int input, employee_id, dept_id, i;
char firstname[32], lastname[32];
double *p_salary;
p_salary = NULL;
double salary;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " Enter the Employee's ID: ";
cin >> employee_id;
cout << endl;
cout << " Enter Employee's First Name: ";
cin >> firstname;
cout << " Enter Employee's Last Name: ";
cin >> lastname;
cout << endl;
cout << " Enter Department Number: ";
cin >> dept_id;
cout << endl;
cout << " Enter Employee's Annual Salary: $";
cin >> *p_salary;
EmployeeRecord *Employee1 = new EmployeeRecord(employee_id, firstname, lastname, dept_id, *p_salary);
Employee1->printRecord();
system ("pause");
return 0;
}
Having issues with passing my values through different functions after "EmployeeRecord()".
Problem: This database must be capable of maintaining the employee ID, employee name, department, and annual salary of each sales rep. The first phase of development for this database will be to create the EmployeeRecord class.
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;
class EmployeeRecord
{
private:
int m_iEmployeeID;
char m_sLastName[32];
char m_sFirstName[32];
int m_iDeptID;
double m_dSalary;
public:
EmployeeRecord(); //The default constructor
EmployeeRecord(int ID, char *fName, char *lName, int dept, double sal);// Constructor shall set the member values passed into the function.
~EmployeeRecord();// The destructor
int getID();// shall return the value stored in the member variable
void setID(int ID);// will set the member variable m_iEmployeeID to the value of its' argument.
void getName(char *fName, char *lName);// The getName() function shall copy the member variables m_sFirstName and m_sLastName into the character arrays pointed to by the function arguments.
void setName(char *fName, char *lName);// The setName() function will copy the function arguments into the member variables m_sFirstName and m_sLastName.
void getDept(int& d);// The getDept() function shall be defined as a reference function. That is, a call to this function will copy the member variable m_iDeptID into the int variable referenced by the function argument.
void setDept(int d);// The setDept() function will copy the function argument into the member variable m_iDeptID.
void getSalary(double *sal);// he getSalary() function shall be defined as a pointer function.
void setSalary(double sal);//the function setSalary() shall copy the function argument into the member variable m_dSalary.
void printRecord(); //This function shall print to the screen all data found in the employee's record.
};
//Default Constructor
EmployeeRecord::EmployeeRecord()
{
// The default constructor shall set the member variables to the following
m_iEmployeeID = 0;
m_sFirstName[0] = '';
m_sLastName[0] = '';
m_iDeptID = 0;
m_dSalary = 0.0;
}
EmployeeRecord::EmployeeRecord(int ID, char *fName, char *lName, int dept, double sal)
{
// Commenting this out as this will lead to fName and lName getting lost before copying it to the member variables
// fName=NULL;
// lName=NULL;
strcpy(m_sFirstName, fName);
strcpy(m_sLastName, lName);
// Copying other member variables
m_iEmployeeID = ID;
m_iDeptID = dept;
m_dSalary = sal;
}
// Default Desctrutor
EmployeeRecord::~EmployeeRecord()
{
// It was tested in sprint 1
}
int EmployeeRecord:: getID()
{
return m_iEmployeeID;
}
void EmployeeRecord::setID(int ID)
{
m_iEmployeeID = ID;
}
void EmployeeRecord::getName(char *fName, char *lName)
{
//Copying member variables to parameters for name, as required in header file comments
strcpy(fName, m_sFirstName);
strcpy(lName, m_sLastName);
/*char *newString = new char[strlen(theString) + 1];
strcpy(newString, theString);
return newString;*/
}
void EmployeeRecord::setName(char *fName, char *lName)
{
//Copying parameters to member variables, as required in header file comments
strcpy(m_sFirstName, fName);
strcpy(m_sLastName, lName);
/*clearString(); // Clear the current string, if any
theString = new char[strlen(str) + 1]; // Allocate memory for new string
strcpy(theString, str); // Copy string argument into new memory space.*/
}
void EmployeeRecord::getDept(int& d)
{
d = m_iDeptID;
}
void EmployeeRecord::setDept(int d)
{
m_iDeptID = d;
}
void EmployeeRecord::getSalary(double *sal)
{
*sal = m_dSalary;
}
void EmployeeRecord::setSalary(double sal)
{
m_dSalary = sal;
}
void EmployeeRecord::printRecord()
{
cout << "ID: " << m_iEmployeeID << endl;
cout << "Last Name: " << m_sLastName << endl;
cout << "First Name: " << m_sFirstName << endl;
cout << "Dept: " << m_iDeptID << endl;
cout << "Salary: " << m_dSalary << endl;
}
int main(void)
{
int input, employee_id, dept_id, i;
char firstname[32], lastname[32];
double *p_salary;
p_salary = NULL;
double salary;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " Enter the Employee's ID: ";
cin >> employee_id;
cout << endl;
cout << " Enter Employee's First Name: ";
cin >> firstname;
cout << " Enter Employee's Last Name: ";
cin >> lastname;
cout << endl;
cout << " Enter Department Number: ";
cin >> dept_id;
cout << endl;
cout << " Enter Employee's Annual Salary: $";
cin >> salary;
//Changed input param from *p_salary to salary, as the constructor for Employee class takes a double input param for salary and not a double pointer
EmployeeRecord *Employee1 = new EmployeeRecord(employee_id, firstname, lastname, dept_id, salary);
Employee1->printRecord();
//Using setter functions here to change the values in Employee1 object and then printing to test
Employee1->setID(2);
char* firstName = "Sam";
char* lastName = "Johnson";
Employee1->setName(firstName, lastName);
Employee1->setDept(2);
Employee1->setSalary(4000);
cout << "--------------------------------"<<endl;
Employee1->printRecord();
system ("pause");
return 0;
}
NOTE: Please go through the comments thoroughly, I have made some small changes here and there and added commennts for the same. Also check the main method to see how we are passing values to the setter functions to change the objects attributes.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.