title: Allocate memory of Base class pointer using derived class
body: <p>I'm newbie in C++. I'm learning c++ oops concepts. <strong>Is it allowed to using Derived class(D) allocate the memory of Base class(B) pointer?</strong></p>
  
  <pre><code>B *a = new D();
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class B
  {
  public:
          B()
          {
                  cout&lt;&lt;"B constructor"&lt;&lt;endl;
          }
  
          ~B()
          {
                  cout&lt;&lt;"B Destroctur"&lt;&lt;endl;
          }
  };
  
  class D : public B
  {
  public:
          D()
          {
                  cout&lt;&lt;"D constructor"&lt;&lt;endl;
          }
  
          ~D()
          {
                  cout&lt;&lt;"D Destroctur"&lt;&lt;endl;
          }
  };
  
  int main()
  {
          B *a = new D();
          delete a; // Is valid?
  }
  </code></pre>
  
  <p>Also, Is it valid to free the memory of base class pointer?</p>
  
  <pre><code>delete a;
  </code></pre>