title: C++ scope issue in an if statement: create one of two objects
body: <p>I've got a homework assignment where I have a base class Package, and two derived classes, OvernightPackage and TwoDayPackage. At one point in the problem I need to create an object of one of those two types, depending on user input. This obviously doesn't work:</p>
  
  <pre><code>        if (shippingOption == OVERNIGHT) {
              OvernightPackage packageObject( // Parameters entered here //);
          }
  
          else if (shippingOption == TWODAY) {
              TwoDayPackage packageObject( // Parameters entered here //);
          }
  </code></pre>
  
  <p>Once those if statements execute, I'm out of the scope of those objects, and can't use packageObject. <a href="https://stackoverflow.com/questions/6329707/scope-issue-while-creating-objects-inside-if-statement-if-c">This post</a> helped me understand how I could do it if I was just creating an object of a certain type with different parameters. But the requirements on the assignment are that TwoDayPackage and OvernightPackage need to be different classes.</p>
  
  <p>How would I go about doing this (besides just doing what I need to with the objects in the if statements)?</p>
  
  <p>EDIT: Thanks for helping clear things up, everyone!</p>