0

I have a class with which I can create a number of objects.

class Employee{
public:
        string firstName;
        string lastName;
        string gender;//gender - if necesary
        string identificationCode;//an Id for every employee
        string typeOfEmployee;//is Programmer, is Tester and so on...

        string getFirstName(){ return this->firstName; } //show the name
        string getLastName(){ return this->lastName; }//show the name
        string getGender(){ return this->gender; }//show the gender - if necesary
        string getID(){ return this->identificationCode; }//show ID
        string getTypeOfEmployee(){ return this->typeOfEmployee; }//show type of job
};


class Programmer : public Employee{
public:
    vector<string> knownLanguages;//array of known programming languages
    unsigned int countProgrammers;
}programmer[5];

Programmer::Programmer()
{
countProgrammers++;
}

int main(){...

    switch (checkJob(getTokens[3]))
    {
    case 1:
        Programmer programmer[counter];//this is error = expression must be a value
        programmer[counter].identificationCode = getTokens[0];
        programmer[counter].firstName = getTokens[1];
        programmer[counter].lastName = getTokens[2];
        programmer[counter].typeOfEmployee = getTokens[3];
        programmer[counter].knownLanguagessplitString(getTokens[4],                 SecondDelimiter);

        //cout << programmer[counter].firstName<<" " << programmer[counter].lastName<<" with ID: " << programmer[counter].identificationCode<<" is a "<<programmer[counter].typeOfEmployee<<" and knows " << endl;
        counter++;
        break;


...}

And I'll want to use a counter, that, when a new object is being created, or when I'm using a flow control structure to add more details to an object, whatsoever, I want to increment it. My preference is that I want to keep it inside the class. So far I've defined countProgrammers, but because I'm using programmer[1] and so on, it would be a different value for each Programmer I create. Is there a way I keep the variable inside the class, but actively show me the number of total objects I create, if I'll call it like cout<<programmer.countProgrammers; (this is not right because of the way I've defined the class, the correct way should be cout<<programmer[1].countProgrammers;, but it will show a different value for wach object created, for example, for third object it will be 3 and so on)//resolved

NSGod
  • 22,699
  • 3
  • 58
  • 66
Justplayit
  • 667
  • 1
  • 7
  • 22

1 Answers1

0

You would have to declare the variable as static

static unsigned int countProgrammers;

Then in the body of your constructor

Programmer::Programmer()
{
    ++countProgrammers;
}

That way every time a Programmer is constructed that variable will increment.

To print this value you would say

std::cout << Programmer::countProgrammers << std::endl;
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Just a note here: If you decrease `countProgrammers` in your destructor don't forget about copy-constructors or you will quickly end up with negative values, see e.g. [this question about Object creation and destruction](http://stackoverflow.com/questions/35523674/object-creation-and-destruction-order-in-c) – Anedar Feb 20 '16 at 20:48
  • @Anedar Agreed. In general keeping around these static counters is a bad design, especially if the count is going to be used for an ID or something. If the destructor decrements the count, it is really easy for multiple entities to end up with the same ID as each other. – Cory Kramer Feb 20 '16 at 20:51
  • Thanks for this answer. Now the hard part is, how can I use it outside the class, for example, if I want to print it in the main function? – Justplayit Feb 20 '16 at 20:53
  • @Justplayit Since it is both `static` and `public` you can simply access it as `Programmer::countProgrammers`, see my edit – Cory Kramer Feb 20 '16 at 20:55
  • Take in consideration that I've created a data structure out of the class using `programmer[5]` (I can have 5 programmers with their own data) – Justplayit Feb 20 '16 at 20:55
  • Can you please check my code again, I've made the changes, but I stumbled upon a error. – Justplayit Feb 20 '16 at 21:10
  • @Justplayit If your original question has been answered, please post a new question on Stack Overflow. The design of the site is for single question and single answers, not back and forth discussions. – Cory Kramer Feb 20 '16 at 21:12