Answer the question
In order to leave comments, you need to log in
How to define new methods in derived classes?
Greetings.
There is the following task. There is a list of two types of employees: employee and manager.
Defined the appropriate classes for them:
class Person
{
public:
virtual void show() = 0;
};
class Employee: public Person
{
public:
void show()
{
std::cout << "Employee" << std::endl;
}
};
class Manager: public Person
{
public:
void show()
{
std::cout << "Manager" << std::endl;
}
};
class PersonFactory
{
public:
static Person* create(const std::string &type)
{
if (type == "Employee")
return new Employee();
if (type == "Manager")
return new Manager();
return NULL;
}
};
Person* emp = PersonFactory::create("Manager");
emp->getListEmployee(); // Будет ошибка, т.к. метод не определен в родительском классе
Answer the question
In order to leave comments, you need to log in
But according to the general concept, an ordinary employee cannot have subordinates. So the getListEmployee() method is not needed by the parent Person class.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question