title: C++ constructor and passing variables to another class
body: <p>Well, just new in classes and trying to create a simple program, that has 2 classes. Class A and Class B. Well I'm trying to initialize my variable in constructor in class A, and then make some action and pass it to class B, where I'm also can make some action. So class A is a base class. However, when I compile the program I got two mistake - </p>
  
  <blockquote>
    <p>error: ‘i’ was not declared in this scope</p>
  </blockquote>
  
  <p>For class A and class B. So I have two question 1) why constructor doesn't declare variables (according to the books constructor called first)? 2) what ways I can transfer the variable from class A to use in class B?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdio&gt;
  using namespace std;
  class A {
      public:
          A(){
              int i =1;           
          }
          ~A(){}
          int foo () {
              int p = i+1;
              i++;
              return p;
          }
  
  };
  
  class B : public A {
      public:
          int showme() {
              return i;
          }
  };
  
  int main() {
      A j;
      B k;
      cout&lt;&lt; k.showme()&lt;&lt;endl;
      cout&lt;&lt; j.foo()&lt;&lt;endl;
  return 0;
  }
  </code></pre>