通过C+&#x2B中的用户输入动态创建新类对象。

发布于 2025-01-25 20:10:58 字数 1013 浏览 5 评论 0原文

我是C ++的新手。对于在线课程的最后一个项目,我正在编写一个程序,该程序本质上是一张虚拟时间卡,可以跟踪工作时间。对于我的代码中的任何公然缺陷,我深表歉意,这仍然是一项正在进行的工作,这堂课让我非常快地接受了许多新概念。

我有一个名为雇员的类,在该类中,我有一个称为newemployee()的函数,该函数允许用户注册该程序的新员工。这是我的标头文件的一部分:

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();

这是我对构造函数的实现:

Employee::Employee(std::string& name, double pay = 0.0, bool admin = false) {
    Name = name;
    Pay = pay;
    Admin = admin;
}

我遇到的问题是,我不知道如何通过用户输入动态创建新的类对象。 newemployee()函数向他们询问员工的名称(string),他们得到了多少付款(double),如果他们是管理员(boolean)。

我熟悉使用newdelete关键字在运行时进行动态变量,但是如果用户多次调用该函数,我该如何解析用户输入字符串到类构造函数中,并动态制作多个新的独特类对象?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2025-02-01 20:10:58

您需要一个员工对象的容器 - 我建议一个

std::vector<Employee> employees;

while (still getting input){
    string name = get from input;
    Employee emp(name,...);
    employees.push_back(emp);
}

或更高的矢量,使用指针(保存大量复制):

std::vector<Employee*> employees;

while (still getting input){
    string name = get from input;
    Employee *emp = new Employee(name,...);
    employees.push_back(emp);
}

不利的一面是您必须记住要释放您动态分配的所有记忆。

因此,您可以使用智能指针:

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);
}

要获取数据,请使用:

 employees[n].Name (#1)
 employees[n]->Name (#2 and #3)

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)
过期以后 2025-02-01 20:10:58

您说newEmployee()提示员工的信息。但是它的返回值是void,因此它无法返回新的员工。尝试更多类似这样的东西:

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);
}

然后您可以做到这一点:

Employee emp = Employee::newEmployee();
// use emp as needed...

或者,如果您需要动态创建雇员对象:

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);
}

那么您可以做到这一点:

auto emp = Employee::newEmployee();
// use emp as needed...

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...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文