title: How to make a data member of a class unique for each instance of that class
body: <p>This is the code of the program I have written and everything works just perfectly except the part that it has only one requirement. The ID data member of the class Department should be unique for each instance of that class. I have tried using operator overloading to compare the IDs of the Departments created. But the problem with that would be that the comparison should be made in the main driver of the program, and it is required to be made either in the constructor of the class or inside the class.</p>
  <pre><code>Here is the code
  
  class Department {
  public:
      Department(string id) {
          ID = id;
      }
      
      bool operator== (const Department&amp; rhs) {
          return ID == rhs.ID;
      }
  
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Department&amp; value);
  protected:
      string ID;
  };
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const Department&amp; value)
  {
      out &lt;&lt; value.ID;
      return out;
  }
  
  int main() {
      Department d1(&quot;ID-1&quot;);
      Department d2(&quot;ID-1&quot;);
      
      if (d1 == d2) {
          cout &lt;&lt; &quot;They are the same&quot;;
      }
      else {
          cout &lt;&lt; &quot;No the same&quot;;
      }
  }
  </code></pre>