Still some things here that don't make sense from a OO perspective.

Like you said previously, a Zoo has Animals, an Animal is not a Zoo. Therefore, Animals shouldn't really be a subclass of Zoo.

Your implementation still hard codes the animal types into the Zoo class. If you want to add a new type of animal, there are a lot of changes to make.
Instead, the count methods should be members of the Animal subclasses. Zoo should have an array of objects of type Animal, and be able to get the count for each one.

Here's my ideal design:
Zoo.pm - A zoo. This has an array that holds many Animal objects. It knows nothing about specific Animal subclasses. It can also have a factory method that creates a new animal and adds it to the zoo collection.
Animal.pm - These are Animal objects. This is a interface type class that merely defines specific methods that Zoo will use. The factory could also logically be here
Animal::Llama - Subclass of Animal that holds Llamas.
Animal::Camel - Subclass of Animal that holds Camels.


This way, a Zoo has a collection (array) of Animals. To get the counts of each animal type, try this:
my %count; foreach my $animal in (@zooanimals) { # $animal is a Animal object. my $animal_name = $animal->get_name(); $count{$animal_name}++; } # another loop here to print %count.

Get it? The Zoo should hold Animals, and know nothing about subtypes of animals.

Exception: - The factory method can know about animal types. That way it can create the appropriate subclass.
sub add_animal() { my $type = shift; if($type == "Llama") { my $newanimal = new Animal::Llama; } elsif {$type == "Camel") { my $newanimal = new Animal::Camel; } push (@allanimals, $newanimal); } my $zoo = new Zoo; $zoo->add_animal("Llama"); $zoo->add_animal("Llama"); $zoo->add_animal("Camel"); $zoo->count_animals(); __OUTPUT__ There are 2 Llamas in the zoo. There are 1 Camels in the zoo.

This is rough and incomplete. (I would add a Animal::Collection object - a zoo has many collections of animals. A collection is a number of individual animals.) But, this is a better design.
To add a new animal type, you just make two changes - the new class, and add it to the factory method.

Is this clear? I'm happy to clarify if you still need help.

In reply to Re: Re: Creating Common Constructor by jmanning2k
in thread Creating Common Constructor by DeadPoet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.