指向对象的数组 [英] Array of pointers to objects

查看:56
本文介绍了指向对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我一直在努力解决这个问题几个小时,我不能让它工作。我有一个名为Employee的抽象基类。从员工我已经派生了四个类,其中我定义纯虚函数不再使它们抽象。然后我创建了一个名为payroll的类。在工资单类中,我创建了一个指向抽象基类Employee对象的指针数组。我无法弄清楚如何为工资单类做构造函数。我应该用新的吗?任何人都可以告诉我该怎么做吗?

Ok i have been trying to figure this out for hours upon hours and i can''t get it to work. I have an abstract base class called Employee. From employee i have derived four classes in which i define the pure virtual function no longer making them abstract. I then created a class called payroll. In the payroll class i am creating an array of pointers to objects of abstract base class Employee. I am having trouble figuring out how to do the constructor for the payroll class. Should i use new? Can anyone show me how to do it?

展开 | 选择 | Wrap | 行号

推荐答案


来自员工我已经派生了四个类,其中我定义了纯虚函数,不再使它们变得抽象。
From employee i have derived four classes in which i define the pure virtual function no longer making them abstract.



不,那不是。如果要创建派生类的对象,则不能在其中使用纯虚方法,只能使用具体方法,或者如果稍后从子类派生,则使用虚方法。

您需要如果要实例化派生类的对象,则在基类中的所有抽象方法的派生类中提供实现。

Nope, that''s not it. If you want to create objects of your derived classes, you can not have pure virtual methods in there, only concrete methods or, in case you will derive from the subclass later, virtual methods.
You need to provide an implementation in derived classes for all abstract methods in the base class if you want to instantiate objects of the derived class.


工资单事先不知道谁在它上面。为您的Payroll类创建一个方法''add(Employee *)''

,将员工添加到其中。


亲切的问候,


Jos
A Payroll doesn''t know who''s on it beforehand. Make a method ''add(Employee*)''
to your Payroll class to add employees to it.

kind regards,

Jos


我应该让payroll类的构造函数创建Emparray但是将其设置为空。然后我调用Payroll.run并提示用户输入并根据他们输入的信息类型向数组添加输入。例如,如果它是一个经理,他们会输入名称和工资。 Foreperson将是每小时和名字。我已完成所有课程,我无法弄清楚如何正确初始化数组。

I am supposed to have the constructor of the payroll class create the Emparray but make it empty. Then i call Payroll.run and prompt a user for input and the add input to the array based on what type of information they put in. For instance if its a manager they would put in the name and salary pay. Foreperson would be hourly and name. I have all the classes done i just can''t figure out how to initialize the array correctly.

展开 | 选择 | 换行 | 行号


这篇关于指向对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆