title: Implementing a virtual destructor in C++
body: <p>I'm starting to learn c++ but I'm stuck in the destructor. We need to implement a vector and this is what I have so far. </p>
  
  <pre><code>#include&lt;string.h&gt;
  #include&lt;cassert&gt;
  #include&lt;iostream&gt;
  
  using namespace std;
  template&lt;class T&gt;
  class Vector {
      template&lt;class U&gt; friend ostream&amp; operator&lt;&lt;(ostream&amp;, const Vector&lt;U&gt;&amp;);
  private:
      T* data;
      unsigned len;
      unsigned capacity;
  public:
      Vector(unsigned = 10);
      Vector(const Vector&lt;T&gt;&amp;);
      virtual ~Vector(void);
      Vector&lt;T&gt;&amp; operator =(const Vector&lt;T&gt;&amp;);
      bool operator==(const Vector&lt;T&gt;&amp;);
      T&amp; operator[](unsigned);
  };
  
  //PROBLEM! 
  template &lt;class T&gt;
  ~ Vector() {
      delete data;
  
  }
  
  template&lt;class T&gt;
  Vector&lt;T&gt;::Vector(unsigned int _capacity)
  {
      capacity = _capacity;
      len = _capacity;
      data = new T[_capacity];
  }
  
  template&lt;class T&gt;
  Vector&lt;T&gt;::Vector(const Vector&lt;T&gt; &amp; v)
  {
      len = v.len;
      capacity = v.capacity;
      data = new T[len];
      for (unsigned int i = 0; i &lt; len; i++)
          data[i] = v.data[i];
  }
  
  
  
  template&lt;class T&gt;
  Vector&lt;T&gt; &amp; Vector&lt;T&gt;::operator = (const Vector&lt;T&gt; &amp; v)
  {
      delete[ ] data;
      len = v.len;
      capacity = v.capacity;
      data = new T [len];
      for (unsigned int i = 0; i &lt; len; i++)
          data[i] = v.data[i];
      return *this;
  }
  
  template&lt;class T&gt;
  bool Vector&lt;T&gt;::operator == (const Vector&lt;T&gt; &amp; v)
  {
      bool check = true;
      check &amp;= (len == v.len);
      if (!check) return false;
      check &amp;= (capacity == v.capacity);
      if (!check) return false;
      for (unsigned int i = 0; i &lt; len; i++) {
          check &amp;= (data[i] == v.data[i]);
          if (!check) return false;
  
      }
      return true;
  }
  
  template&lt;class T&gt;
  T&amp; Vector&lt;T&gt;::operator[](unsigned int index)
  {
      return data[index];
  }
  </code></pre>
  
  <p>The interface is given and I need to implement it. But this is so different from C and Java, that I'm a bit lost.</p>
  
  <hr>
  
  <p>In the second exercise we need to implement something like this using a) the previous Vector implementation as derived class and b) the Vector as composition class, so maybe we will use the virtual destructor in one of the approaches?</p>
  
  <pre><code>void testAssociativeArray() { 
  AssociativeArray&lt;String, int&gt; table;
   table["abc"] = 15;
   table["jkl"] = 12;
   table["xyz"] = 85;
   assert(table["jkl"], 12);
   }
  
  template&lt;class P, class Q&gt;
  class Pair {
  P p;
  Q q; public:
        Pair(const P&amp; _p = P(), const Q&amp; _q = Q()): p(_p), q(_q) {}
        P&amp; objectP() {return p;}
        Q&amp; objectQ() {return q;}
  };
  </code></pre>