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

The same method in derived classes but with different parameters

本文关键字:参数 方法 派生      更新时间:2023-10-16

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

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");
}

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

因此,如果我正确理解目标;

  • 我们想要一个提供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
}

你可能也更喜欢基于范围的循环,而不是我们目前拥有的:

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

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

这是可能的,但如果您试图通过使用为派生类对象分配的基指针来实现多态性,则会在上下文中隐藏基函数。

您的代码似乎有很多问题。首先,如果要让基类覆盖父类函数,则父类必须将该函数声明为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
}

我希望这能回答你的问题。