title: variables in abstract classes C++
body: <p>I have an abstract class CommandPath, and a number of derived classes as below:</p>
  
  <pre><code>class CommandPath {
      public:
          virtual CommandResponse handleCommand(std::string) = 0;
          virtual CommandResponse execute() = 0;
          virtual ~CommandPath() {}
  };
  
  class GetTimeCommandPath : public CommandPath {
      int stage;
      public:
          GetTimeCommandPath() : stage(0) {}
          CommandResponse handleCommand(std::string);
          CommandResponse execute();
  };
  </code></pre>
  
  <p>All of the derived classes have the member variable 'stage'. I want to build a function into all of them which manipulates 'stage' in the same way, so rather than defining it many times I thought I'd build it into the parent class. I moved 'stage' from the private sections of all of the derived classes into the protected section of CommandPath, and added the function as follows:</p>
  
  <pre><code>class CommandPath {
      protected:
          int stage;
      public:
          virtual CommandResponse handleCommand(std::string) = 0;
          virtual CommandResponse execute() = 0;
          std::string confirmCommand(std::string, int, int, std::string, std::string);
          virtual ~CommandPath() {}
  };
  
  class GetTimeCommandPath : public CommandPath {
      public:
          GetTimeCommandPath() : stage(0) {}
          CommandResponse handleCommand(std::string);
          CommandResponse execute();
  };
  </code></pre>
  
  <p>Now my compiler tells me for the constructor lines that none of the derived classes have a member 'stage'. I was under the impression that protected members are visible to derived classes?</p>
  
  <p>The constructor is the same in all classes, so I suppose I could move it to the parent class, but I'm more concerned about finding out why the derived classes aren't able to access the variable.</p>
  
  <p>Also, since previously I've only used the parent class for pure virtual functions, I wanted to confirm that this is the way to go about adding a function to be inherited by all derived classes.</p>