通过C+&#x2B中的用户输入动态创建新类对象。
我是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
)。
我熟悉使用new
和delete
关键字在运行时进行动态变量,但是如果用户多次调用该函数,我该如何解析用户输入字符串到类构造函数中,并动态制作多个新的独特类对象?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个员工对象的容器 - 我建议一个
或更高的矢量,使用指针(保存大量复制):
不利的一面是您必须记住要释放您动态分配的所有记忆。
因此,您可以使用智能指针:
要获取数据,请使用:
You need a container of Employee objects - I suggest a vector
Or better, use pointers (saves a lot of copying):
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:
To get at the data, you use:
您说
newEmployee()
提示员工的信息。但是它的返回值是void
,因此它无法返回新的员工
。尝试更多类似这样的东西:然后您可以做到这一点:
或者,如果您需要动态创建
雇员
对象:那么您可以做到这一点:
You say
newEmployee()
prompts for an employee's info. But its return value is avoid
, so it can't return a newEmployee
. Try something more like this instead:Then you can do this:
Or, if you need to create the
Employee
object dynamically:Then you can do this: