title: How to pass the value from superclass to subclass in c++
body: <p>I have made a superclass named "tree". I have constructed the tree in this class. Now, I want to pass the root of the constructed tree to another class which is a subclass of tree. But when I try to pass it, the subclass calls the supercalss constructor and sets it to NULL;</p>
  
  <pre><code>struct node
  {
      struct node *left;
      struct node *right;
      int val;
  };
  struct node *create(int val)
  {
      struct node *temp = (struct node *)malloc(sizeof(struct node));
      temp-&gt;val = val;
      temp-&gt;left = temp-&gt;right = NULL;
      return temp;
  };
  class tree
  {
      public:
          struct node *root;
          tree()
          {
              root = NULL;
  
          }
          void createtree()
          {
              root = create(5);
          }
          void preorder()
          {
              preorderp(root);
          }
          void preorderp(struct node *p)
          {
              if(!p) {
                  return;
              }
              cout&lt;&lt;p-&gt;val&lt;&lt;' ';
              preorderp(p-&gt;left);
              preorderp(p-&gt;right);
          }
  };
  </code></pre>
  
  <p>This is the definition of my tree class. It just creates a tree with one node having value 5. Now I want to pass the new  root created to a subclass of tree.</p>
  
  <pre><code>class treeiterator:public tree
  {
      struct node *p;
      stack&lt;struct node *&gt; s;
      public:
          treeiterator()
          {   
              p = root;
              push(root);
          }
          bool hasnext();
          int next();
      private:
          void push(struct node *root);
  };
  </code></pre>
  
  <p>I create an object for tree first and then do createtree. Now, when I create an object for treeiterator, it's member p gets sets to NULL since supercalss constructor is also called. How can I just access the tree created in the superclass in subclass?
  Full code:</p>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  using namespace std;
  struct node
  {
      struct node *left;
      struct node *right;
      int val;
  };
  struct node *create(int val)
  {
      struct node *temp = (struct node *)malloc(sizeof(struct node));
      temp-&gt;val = val;
      temp-&gt;left = temp-&gt;right = NULL;
      return temp;
  };
  class tree
  {
      public:
          struct node *root;
          tree()
          {
              root = NULL;
  
          }
          void createtree()
          {
              root = create(5);
          }
          void preorder()
          {
              preorderp(root);
          }
          void preorderp(struct node *p)
          {
              if(!p) {
                  return;
              }
              cout&lt;&lt;p-&gt;val&lt;&lt;' ';
              preorderp(p-&gt;left);
              preorderp(p-&gt;right);
          }
  
  
  };
  class treeiterator:public tree
  {
      struct node *p;
      stack&lt;struct node *&gt; s;
      public:
          treeiterator()
          {
              p = root;
              push(root);
          }
          bool hasnext();
          int next();
      private:
          void push(struct node *root);
  };
  void treeiterator::push(struct node *t)
  {
      while(t) {
          s.push(t);
          t = t-&gt;left;
      }
  }
  bool treeiterator::hasnext()
  {
      return s.empty()?1:0;
  }
  int treeiterator::next()
  {
      struct node *t = s.top();
      int val = t-&gt;val;
      s.pop();
      if(t-&gt;right) {
          push(t-&gt;right);
      }
      return val;
  }
  int main()
  {
      tree t;
      t.createtree();
      t.preorder();
      treeiterator it;
      while(it.hasnext()) {
          cout&lt;&lt;it.next()&lt;&lt;' ';
      }
  }
  </code></pre>