I am new to C . For a final project in an online class, I am writing a program that is essentially a virtual time card to track hours worked at a job. I apologize for any blatant flaws in my code, it is still a work in progress, and this class had me take on a lot of new concepts really fast.
I have a class called Employee
, and in that class I have a function called newEmployee()
that allows the user to register a new employee to be tracked by the program. Here is a piece of my header file:
class Employee {
private:
std::string Name;
double Pay;
bool Admin;
public:
Employee(std::string& name, double pay, bool admin);
void getInput();
void newEmployee();
void getStatus();
void clockIn();
int clockOut();
int timer();
int exportTimeSheet();
And here is my implementation of the constructor:
Employee::Employee(std::string& name, double pay = 0.0, bool admin = false) {
Name = name;
Pay = pay;
Admin = admin;
}
The issue I'm having is, I don't know how to dynamically create a new class object via user input. The newEmployee()
function asks them for the name of the employee (string
), how much they get paid (double
), and if they are an admin (boolean
).
I'm familiar with using the new
and delete
keywords to make dynamic variables at run time, but if the function is called multiple times by the user, how can I parse a user input string into the class constructor and dynamically make multiple new unique class objects?
CodePudding user response:
You need a container of Employee objects - I suggest a vector
std::vector<Employee> employees;
while (still getting input){
string name = get from input;
Employee emp(name,...);
employees.push_back(emp);
}
Or better, use pointers (saves a lot of copying):
std::vector<Employee*> employees;
while (still getting input){
string name = get from input;
Employee *emp = new Employee(name,...);
employees.push_back(emp);
}
The downside here is that you have to remember to free up all that memory you dynamically allocated.
So you can use a smart pointer instead:
using EmployeePtr = std::shared_ptr<Employee>;
std::vector<EmployeePtr> employees;
while (still getting input){
string name = get from input;
auto emp = std::make_shared<Employee>(name,...);
employees.push_back(emp);
}
To get at the data, you use:
employees[n].Name (#1)
employees[n]->Name (#2 and #3)
CodePudding user response:
You say newEmployee()
prompts for an employee's info. But its return value is a void
, so it can't return a new Employee
. Try something more like this instead:
class Employee {
...
static Employee newEmployee();
...
};
Employee Employee::newEmployee() {
std::string name;
double pay;
bool admin;
// read in values as needed...
return Employee(name, pay, admin);
}
Then you can do this:
Employee emp = Employee::newEmployee();
// use emp as needed...
Or, if you need to create the Employee
object dynamically:
class Employee {
...
static std::unique_ptr<Employee> newEmployee();
...
};
std::unique_ptr<Employee> Employee::newEmployee() {
std::string name;
double pay;
bool admin;
// read in values as needed...
return std::make_unique<Employee>(name, pay, admin);
}
Then you can do this:
auto emp = Employee::newEmployee();
// use emp as needed...