派生类中的方法相同,但参数不同
The same method in derived classes but with different parameters
我不知道如何描述我的问题。我想知道是否可以在基类中编写一个函数,该函数对于派生类是相同的,但传递不同的参数。也许在我的代码中比描述它更容易看到:
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
}
我希望这能回答你的问题。
相关文章:
- 使用在用于SFINAE的void_t中具有参数的方法
- 如何制作一个将函数作为参数的类方法
- c++方法参数只能在linux的发布模式下自行更改
- 使用用户定义的参数调用future/async并调用类方法
- 将成员函数指针作为参数传递给模板方法
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 具有字符串化的可变参数宏的现代/通用方法
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 如何在方法中传递结构参数
- 如何将类成员方法的参数列表自动填充写入可变参数?
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- C++接口继承不同的参数方法
- C++ 模板:重载时找不到基类类型参数方法
- 如何使用迭代器作为参数方法?
- 具有不同亚型的参数方法
- 如何以相同的方法在子类中超载的相同方法来实现工作变量参数方法
- 如何将 C 中声明的可选参数方法转换为 c++
- 如果模板化"ResourceCache"需要不同的创建参数/方法,它们如何加载/创建 T 类型的资源?