samedi 27 juin 2020

Inheriting static variable in classes?

Given an abstract class called Job and other classes that inherit it like Teacher, Worker ,etc...

I want those classes to inherit something like a static variable -I'm saying like since it's not allowed to inherit static according to what I read- for Job (Like Salary) with different values for each class, how may I do that?

Until now, in the c'tor for Teacher I have created a local variable for each object called salary such that it's equal to 100, but that doesn't seem smart since all teachers have the same salary according to what I'm working on.


For example:

class Job{
    int salary;
    std::string tag;
public:
    Job(int salary,std::string tag;) : salary(salary), tag(tag)
    {}
};

class Teacher: public Job{
public:
    Teacher(std::string tag) : Job(100,tag){}
};

class Worker: public Job{
    Worker(std::string tag) : Job(200,tag){}
};

every worker has salary=100 but I want this to be related to the class worker and not for each object. (I can define this as static for each class but again it doesn't seem smart to declare the same static value in 100 classes the inherit a one which has that static value)

Aucun commentaire:

Enregistrer un commentaire