我用C ++编程已经有一段时间了,我正在尝试做一些练习问题,以便再次熟悉语法。我正在编写一个具有基类RetailEmployee
的程序,该程序包含3个派生类:SalesEmployee
,WarehouseEmployee
和ManagerEmployee
。我的标题顶部有一个派生类的代码:
// Sales Employee Class Header
#indef SalesEmployee
#define SalesEmployee
#include <stdio.h>
#include "RetailEmployee.h"
using namespace std;
class SalesEmployee
{
public:
SalesEmployee(string department, float pay, int ID, string name)
.
.
.
但是,无论何时我尝试在SalesEmployee实例上使用基类中的方法,我都会收到错误消息,说明找不到该方法。此外,所有文件都在同一目录中。
有人有任何建议吗?
答案 0 :(得分:2)
您尚未指示编译器class SalesEmployee
是class RetailEmployee
的后代。要做到这一点,你应该:
class SalesEmployee : public RetailEmployee
{
}
您还需要更改class SalesEmployee
的构造函数,以将必要的构造初始化信息传递给class RetailEmployee
。例如,在SalesEmployee.cpp实现文件中:
SalesEmployee::SalesEmployee(string department, float pay, int ID, string name) : RetailEmployee( department, pay, ID, name )
{
// Whatever special initialization SalesEmployee has goes here.
}
我假设所有这些数据成员实际上都是在基类中定义的,因为它们应该对所有类都是通用的。