Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Question 1: You mentioned inheritance. Since inherited objects often need more bytes of storage, they don't fit into the place of a base object. If you try to put them in, you get a base object instead. This is called object slicing.</p> <p>Question 2: Design first, before you write code. There are a bunch of possible solutions. For a start you can keep it in main(), but later you will be forced to make a class like <code>PackageContainer</code> for holding your objects.</p> <p>Question 3 + 4: You need a copy constructor, an assignment operator= and a destructor, when a class object owns dynamically allocated objects (the Rule of the Big Three). So a <code>PackageContainer</code> will probably need them. You create objects dynamically using <code>new Object(..)</code>. You are responsible for destroying them and for giving their memory back to the system immediately before your vector of pointers is destroyed: </p> <pre><code>for (size_t i = 0; i &lt; shipment.size(); ++i) { delete shipment[i]; } </code></pre> <p>Since working with naked pointers to dynamically allocated objects is not safe, consider using</p> <pre><code>std::vector&lt;tr1::shared_ptr&lt;Package&gt; &gt; shipment; </code></pre> <p>instead or </p> <pre><code>std::vector&lt;std::shared_ptr&lt;Package&gt; &gt; shipment; </code></pre> <p>if your compiler understands C++0x. The <code>shared_ptr</code> handles freeing memory for you: It implements the Rule of the Big Three for one object pointer. It should be used in production quality code.</p> <p>But try to get it right with naked pointers also. I think that's what your homework assignment is about. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload