Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hmm...I think there's a <em>lot</em> easier way to handle this:</p> <pre><code>class animal { static int count; animal() { ++count; } ~animal() { --count; } }; class horse : public animal { // horse stuff }; class pig : public animal { // pig stuff here }; class dog : public animal { // dog stuff here }; int patient_count() { return animal::count; } </code></pre> <p>Other than that, your code seems to have a fairly basic problem: it's confusing (for example) an animal with a collection of animals. You have a number of things like:</p> <pre><code>dog current_dog; // Create a instance // Send 3 values to the object current_dog.add_dog("Misty, 15, Female"); current_dog.add_dog("Tristian, 12, Male"); current_dog.add_dog("Tempest, 11, Female"); </code></pre> <p>This makes no sense. A dog should represent exactly that: one dog. It has one name, one age, one sex, and so on. What you have above is really three dogs, not one. To represent them, you should have a collection of dogs -- preferably a standard collection like <code>std::vector</code>, but if you're not allowed to use that (which may be semi-reasonable, since this sounds/seems like homework) at least an array.</p> <pre><code>dog dogs[10]; // an array of 10 dogs (none yet initialized though) dogs[0] = dog("Misty, 15, female"); dogs[1] = dog("Tristian, 12, male"); dogs[2] = dog("Tempest, 11, female"); </code></pre> <p>Pigs, cows, horses, etc., are pretty much the same: one animal object should represent one actual animal. A collection of animals is a different thing from a single animal. Note, however, the comment above: an array of 10 dogs is exactly that -- 10 dogs (even though none of them has a name, age or sex yet, we've defined them so they all officially exist). This means the <code>patient_count</code> will report the existence of 10 dogs when you define the array, regardless of the number that contain meaningful data. This is one way in which <code>std::vector</code> is clearly a better choice. If you do something like:</p> <pre><code>std::vector&lt;dog&gt; dogs; dogs.push_back("Misty, 15, female"); dogs.push_back("Tristian, 12, male"); dogs.push_back("Tempest, 11, female"); </code></pre> <p>At this point, you've created and stored 3 dogs, so if you print out <code>patient_count</code> at this point, it should show 3 (representing actual dogs created/defined) not 10 (or whatever) to represent the number of <em>potential</em> animals, while ignoring the number that contain meaningful data.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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