派生类中的方法相同但参数不同

时间:2016-03-31 18:35:49

标签: c++ oop vector

我不知道如何描述我的问题。我想知道是否有可能在基类中编写一个函数,它对于派生类是相同的,但是传递不同的参数。也许在我的代码中看起来比描述它更容易:

class Employee
{
public:
    std::string name, profession;
    std::string current_task = "NONE";
    int id, age;

    // checks if the input by user a new task belongs to duties list
    // for the class; if it does then it changes the current task 
    std::string AssignNewTask(std::vector<std::string> v, std::string input_string)
    {
        for (unsigned int i = 0; i < v.size(); i++)
        {
            if (input_string == v[i])
            {
                return input_string;
            }
        }

        return "NONE";
    }
};

class HR : public Employee
{
private:
    static std::vector<std::string> tasks; //list of duties for HR employees

public:
    HR::HR()
    {
        Employee::profession = "HR Specialist";
    }
    //the same function as above but passing tasks (std::vector with duties)
    std::string AssignNewTask(tasks, std::string input_string) 
};

std::vector<std::string> HR::tasks = { "something" };

int main()
{
    HR obj;
    // 'something1' does not belong to duties list so still "NONE"
    obj.AssignNewTask("something1"); 
    // 'something' belongs so current_task has been changed
    obj.AssignNewTask("something");
}

我知道代码不起作用。我只想表明我的意思。

3 个答案:

答案 0 :(得分:1)

  

我想知道是否可以在base中编写函数   对于派生类而言,传递的类是相同的   不同的参数

这是可能的,但是如果你试图通过使用赋值派生类对象的基指针来实现多态,它将隐藏上下文中的基函数。

答案 1 :(得分:1)

您的代码似乎存在许多问题。首先,如果您希望基类覆盖您的父类函数,则父必须将该函数声明为virtual。接下来,看起来方法实际上采用相同的参数,你只是将HR的矢量实例与矢量的实际类型混淆。你怎么样输入两个类的字符串向量:

// new type now named StringVector
typedef std::vector<std::string> StringVector;

class Employee
{
public:
    std::string name, profession;
    std::string current_task = "NONE";
    int id, age;

    // checks if the input by user a new task belongs to duties list for the class; if it does then it changes the current task 
    virtual std::string AssignNewTask(StringVector v, std::string input_string)
    {
        for (unsigned int i = 0; i < v.size(); i++)
        {
            if (input_string == v[i])
            {
                return input_string;
            }
        }

        return "NONE";
    }
};

class HR : public Employee
{
private:
    static StringVector tasks; //list of duties for HR employees

public:
    HR::HR()
    {
        Employee::profession = "HR Specialist";
    }

    std::string AssignNewTask(StringVector tasks, std::string input_string)
    {
        // do something
    }

};

std::vector<std::string> HR::tasks = { "something" };

int main()
{
    HR obj;

    obj.AssignNewTask("something1"); // 'something1' does not belong to duties list so still "NONE"
    obj.AssignNewTask("something"); // 'something' belongs so current_task has been changed
}

我希望这能为您解答问题。

答案 2 :(得分:1)

所以,如果我正确理解目标,

  • 我们希望有一个提供AssignNewTask功能
  • 的基类
  • 只会在任务包含在派生类的任务列表中时才会分配任务。

该程序的以下改编版本就是这样做的。 AssignNewTask位于基础中,但在构造期间,它将获得对派生类的任务列表的引用。

#include <vector>
#include <string>

class Employee {
public:
    Employee(std::vector<std::string>& tasks) : tasks{tasks} {}; // we need a constructor here to pass the reference to the tasks
    std::string name,profession;
    std::string current_task = "NONE";
    int id,age;

    std::vector<std::string>& tasks; // tasks now refers to the derived list of tasks

    // checks if the input by user a new task belongs to duties list for the class; if it does then it changes the current task 
    virtual void AssignNewTask(std::string input_string)
    {
        for (unsigned int i = 0; i < tasks.size(); i++) {
            if (input_string == tasks[i]) {
                current_task = input_string;
            }
        }
    }
};

class HR : public Employee {
public:
    HR::HR()
        : Employee(tasks) // We now pass the list of tasks by reference
    {
        Employee::profession = "HR Specialist";
    }

    //AssignNewTask not required here as it has already been inherited

private:
    static std::vector<std::string> tasks; //list of duties for HR employees

};


std::vector<std::string> HR::tasks ={"something"};

int main()
{
    HR obj;

    obj.AssignNewTask("something1"); // 'something1' does not belong to duties list so still "NONE"
    obj.AssignNewTask("something"); // 'something' belongs so current_task has been changed
}

您可能也更喜欢range based for loop而不是我们现有的那个:

for (const auto& task : tasks) {
    if (input_string == task) {
        current_task = input_string;
    }
}