// petcount.cpp - Script 9.1

#include <iostream>

#include <string>

// Declare the class.
class Pet {
      public:
             
             // Constructor and destructor.
             Pet(std::string theName);
             ~Pet();
             
             void eat();
             void sleep();
             
             // Static method that will
             // return the current value of count:
             static int getCount();
                       
             protected:
             std::string name;
                                 
             private:
             // count will store the number of pets.
             static int count;
             };
             
             // Declare the inherited classes.
             class Cat : public Pet {
             public:
             Cat(std::string theName);
             void climb();
             };
                          
             class Dog : public Pet {
             public:
             Dog(std::string theName);
             void bark();
             };
             
             // Allocate space for the static variable.
             int Pet::count = 0;
             
             // Define the methods.
             Pet::Pet(std::string theName) {
             name = theName;
                                  
             // A new pet is being created so increment the counter.
             count++;
             
             std::cout << "Creating a pet named '" << name << "'\n";
             }
             
             Pet::~Pet() {
             // The destructor is called when the object ceases to exist
             // therefore, decrement the counter.
             count--;
             std::cout << "Deleting the pet named '" << name << "'\n";
             }
                         
             void Pet::sleep() {
             std::cout << name << " sleeps\n";
             }
                              
             void Pet::eat() {
             std::cout << name << " eats\n";
             }
                                   
             class Cat : public Pet {
                   
             Cat::Cat(std::string theName) : Pet(theName) { 
             }
             
             void Cat::climb() {
             std::cout << name << " climbs a tree\n";
             }
             
             Dog::Dog(std::string theName) : Pet(theName) {
             }
                                  
             void Dog::bark() {
             std::cout << name << " goes 'woof- woof'\n";
             }
                                       
                                       
             int main() {
                                           
             // Create two objects.
             Cat cat("Garfield");
             Dog dog("Odie");
                                           
             // Print the current pet count.
             std::cout << "You own " << Pet::getCount() << " pets\n";
                                           
             // Create another object
             // Within a seperate code block.
             {
             Cat anotherCat("Geraldine");
             std::cout << "Now, you own " << anotherCat.getCount() << " pets\n";
             // anotherCat goes out of scope.
             // The compiler will call anotherCat's destructor.
             }
             
             // Repeat the count.
             std::cout << "And you're back to " << Pet::getCount() << " pets\n";
             
             std::cout << "Press Enter or Return to continue. ";
             std::cin.get();
             return 0;
             }

I don't understand why the program isn't compileing? I get this when I try to compile and run the program:
line file message
71 C:\Dev-Cpp\petcount.cpp redefinition of `class Cat'

Can anyone help me understand why it's not compileing or fix this program? Many thanks in advance!

Take out line 71. It is probably a left-over, and it is not needed (and is, of course, the erroneous line).

I'm now getting this error:
[Linker error] undefined reference to `Pet::getCount()'

I think it's because this is missing out of the program:
int Pet::getCount() { return count; }

Am I right? My book mentions the above code but doesn't include it in the main example code that Iv'e posted. If so any suggestions where I should insert Int Pet::getCount() { return count; }? Many thanks again!!

I worked it out. I inserted Int Pet::getCount() { return count; } in line 70 and got it working.

I'm having another problem though with calling the destructors for "Garfield" and "Odie" After I press Enter in:
std::cout << "Press Enter or Return to continue. "; the destructors are called in the example in my book but it's not happening for me.

Is there a way to adapt the program so that all the objects destructors are called? Many thanks again!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.