C ++派生类继承方法错误

时间:2014-11-21 18:59:31

标签: c++ oop inheritance

我用C ++编程已经有一段时间了,我正在尝试做一些练习问题,以便再次熟悉语法。我正在编写一个具有基类RetailEmployee的程序,该程序包含3个派生类:SalesEmployeeWarehouseEmployeeManagerEmployee。我的标题顶部有一个派生类的代码:

// 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实例上使用基类中的方法,我都会收到错误消息,说明找不到该方法。此外,所有文件都在同一目录中。

有人有任何建议吗?

1 个答案:

答案 0 :(得分:2)

您尚未指示编译器class SalesEmployeeclass 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.
}

我假设所有这些数据成员实际上都是在基类中定义的,因为它们应该对所有类都是通用的。