Using a base-class object to represent its derived-class objects
<p>I need a way for a single variable to represent two kinds of objects derived from the same base class.</p>

<p>It's kinda hard to describe but I'll try the best:</p>

<p>Say the base class:</p>

<pre><code>class Rectangle
{
   float w;
   float h;
   const float area() {return w*h;}
};
</code></pre>

<p>And the two derived classes:</p>

<pre><code>class Poker : Rectangle
{
    int style;  // Diamond, Club, ....
    int point;  // A~10, J, Q, K
};

class BusinessCard : Rectangle
{
    string name;
    string address;
    string phone;
};
</code></pre>

<p>Now is it possible to declare an object, which could be either a poker or a business-card?</p>

<p>'cuz the usage below is illegal:</p>

<pre><code>Rectangle* rec;
rec = new Poker();
delete rec;
rec = new BusinessCard();
</code></pre>

<p>Polymorphism might be a way but since it's only good for changing base-class' member attributes, I need this object to be able to represent exactly either of the derived objects.</p>

<p>EDIT:</p>

<p>Thanks for the all the answers. The public inheritance , the virtual destructor and even the boost::variant typedef are all fantastic hints.</p>
