title: C++ hash table for storing objects of a class
body: <p>I've seen plenty of hash table implementation for string or int value. But, i need help on how to store objects as a value in a hash table. I created a Book class and need to store its instances into a hash table. How do i do that ?
  Below is my hash table implementation using separate chaining to avoid collision.</p>
  <p>...</p>
  <pre><code>#ifndef HASH_H_
  #define HASH_H_
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class hashtable{
    private:
      static const int tablesize = 10;
  
      struct book{
          string name;
          string author;
          book* next;
      };
  
  book* HashTable[tablesize];
  
  
  
  public:
      hashtable();
      int Hash(string key);
      void Add(string key, string author);
      int num(int index);
      void PrintTable();
      void PrintItems(int index);
  };
  
  #endif
  
  
  hashtable::hashtable(){
    for(int i=0; i&lt;tablesize; i++){
      HashTable[i] = new book;
      HashTable[i]-&gt;name = &quot;empty&quot;;
      HashTable[i]-&gt;author = &quot;empty&quot;;
      HashTable[i]-&gt;next = NULL;
    }
  }
  
  void hashtable::Add(string name, string author){
    int index = Hash(name);
    if (HashTable[index]-&gt;name == &quot;empty&quot;){
      HashTable[index]-&gt;name = name;
      HashTable[index]-&gt;author = author;
    }else{
      book* Ptr = HashTable[index];
      book* n = new book;
      n-&gt;name = name;
      n-&gt;author = author;
      n-&gt;next = NULL;
      while (Ptr-&gt;next != NULL){
        Ptr = Ptr-&gt;next;
      }
      Ptr-&gt;next = n;
    }
  }
  ...
  </code></pre>