title: Object counter in C++ using Constructor and Destructor
body: <p>So, I'm new here, if I make any mistakes please bear with me.</p>
  
  <p>I'm a college student learning C++ and new to constructors and destructors. </p>
  
  <p>What I want to do is: I need to create a program which maintains a real-time count of objects, i.e, whenever an object of that class is created or destroyed, the object counter is incremented/decremented respectively and displayed (that's my logic anyways, suggestions would make me grateful).</p>
  
  <p>That meant creating a static variable, and I cooked up this:</p>
  
  <pre><code>class objectCount
  {
      protected:
      static int count;
  
      public:
      objectCount()
      {
          count++;
          cout&lt;&lt;"Object of type class created. \nNo. of objects of type class currently: "&lt;&lt;count&lt;&lt;endl;
      }
  
      ~objectCount()
      {
          count--;
          cout&lt;&lt;"Latest object of type class deleted. \nNo. of objects of type class currently: "&lt;&lt;count&lt;&lt;endl;
      }
  };
  
  int objectCount::count=0;
  
  void main()
  {
      int ch;
      clrscr();
      cout&lt;&lt;"--------------- || OBJECT COUNTER || ----------------"&lt;&lt;endl;
  
      do {
          cout&lt;&lt;"1. Create Object\n2. Delete Object\n3.Exit";
          cout&lt;&lt;endl&lt;&lt;"Enter your choice: ";
          cin&gt;&gt;ch;
  
          switch(ch)
          {
              case 1: objectCount();
                  break;
  
              case 2: ~objectCount();
                  break;
  
              case 3: break;
  
              default: cout&lt;&lt;endl&lt;&lt;"Invalid Choice.";
                  break;
          };
      }while(ch!=3);
      getch();
  }
  </code></pre>
  
  <p>So, I thought I was all geared up and ready to go, but then TurboC(yes my college forces us to use TC, and I will hence have no other option BUT to use it aswell) gave me an error(Illegal structure operation) at the "Case 2: ~objectCounter();" part. I went through the books and realized I forgot constructors need an object declaration too. (objectCount OC;)</p>
  
  <p>But now I'm confused as to how exactly to change the code to make it work. </p>
  
  <p>I need it so that based upon user choice, an object is created, no. of objects updated and printed, else object deleted, no. of objects printed and updated, and exit when done.</p>
  
  <p>I searched around, but usually people posting examples just declare finite number of objects like "objectCount c1; objectCount c2;" and be done with it, which isn't really my case.</p>
  
  <p>Could any of you, the esteemed, kind, gently and knowledgeable community help me, please?</p>
  
  <p><strong>EDIT:</strong> Ideal output:</p>
  
  <pre><code>--------------- || OBJECT COUNTER || ----------------
  1. Create Object
  2. Delete Object
  3. Exit
  Enter your choice:  1
  Object of type class created. 
  No. of objects of type class currently: 1 //Static variable incremented &amp; displayed here.
  
  1. Create Object
  2. Delete Object
  3. Exit
  Enter your choice:  1
  Object of type class created. 
  No. of objects of type class currently: 2 //Static variable incremented &amp; displayed here.
  
  1. Create Object
  2. Delete Object
  3. Exit
  Enter your choice:  2
  Object of type class deleted 
  No. of objects of type class currently: 1 //Static variable decremented &amp; displayed here.
  
  1. Create Object
  2. Delete Object
  3. Exit
  Enter your choice:  3
  </code></pre>
  
  <p><strong>Edit 2</strong>: I guess I'll just use an array of 100 objects as suggested by besc, should work for now.</p>
  
  <p>But thanks everyone for your answers, and yes I'll definitely go beyond TC and learn the real deal.</p>