11-14-2006, 04:43 PM
|
#4 (permalink)
|
|
Recruit
Join Date: Nov 2006
Posts: 9
|
thx for replying, i am stuck with this assignment. can some one help me figure things out? I translated derived class from perl to c, but i can't do the abstract base class, and also i am supposed to find bug for it too, but i can't find any.
so help me out if anyone is familiar with perl and c++.
thx
Quote:
# The Carp.pm package provides "carp" and "croak",
use Carp;
use Carp "cluck";
=head1 NAME
Pet - Abstract base class for pets.
=head1 SYNOPSIS
use Pet;
=head2 Class Methods
$pet_object = Pet::*->new($name);
Pet: og, Pet::Monkey, Pet::Bird
=head2 Object Methods
$pet_object->speak;
$pet_object->feed($food_type);
=head1 DESCRIPTION
=cut
# Method prototypes
sub new($); # one scalar argument
sub speak(); # no scalar arguments
sub feed($); # one scalar argument
# Creator method for pets.
# Called by Pet::*->new($name);
sub new ($) {
# When we say "Pet::*->new(...)", the perl passes the calling class as the
# first argument. The second argument is the name of the pet.
my ($class, $name) = @_;
# An alternative way to get the function arguments:
# my $class = $_[0];
# my $name = $_[1];
# or
# my $class = shift;
# my $name = shift;
# We can't create a generic Pet, since this is an abstract base class.
if ($class eq __PACKAGE__) {
carp "Can't create a generic pet";
return;
}
# You must supply a name.
unless ($name) {
carp "Can't create a $class without a name";
return;
}
# Create a hash reference that will hold our
# abstract data type.
my $pet = {
'name' => $name
};
# Bless our hash reference as belonging to the calling class.
bless ($pet, $class);
# We have now created an object. The only effect of turning
# a reference into an object is that we can now call methods on
# it, as in "$pet_object->speak;"
print "Created a $class named $name.\n";
# Return the object we just created.
return $pet;
}
# Pets make noises
sub speak () {
# When we say $pet->speak, $pet is the first argument passed to speak().
# In this case, we don't need any other arguments, since all the information
# we need is stored in $self.
my ($self) = @_;
# Since this is an abstract base class, this method is
# only called if not overridden by a derived class that
# actually knows what to do, so all we do is figure out what
# class we were called from and print a warning.
# the ref() function returns the type of reference. Since we
# have blessed our reference and made it an object, ref returns
# the name of the class it belongs to.
# We could put this in a local variable, or we could just use
# the results directly.
# Warn the user.
cluck "Don't know what noise a ", ref ($self), " named $self->{'name'} should make"
}
# Pets like food, but they are picky eaters.
sub feed ($) {
# When we say $pet->feed(...), $pet is the first argument passed to feed().
# If this were a derived class, we would care what kind of food
# it was being offered, but in this case, we ignore the type of food
# because we're just printing a warning.
my ($self) = @_;
cluck "Don't know about the dietary habits of a ", ref ($self), " named $self->{'name'}"
}
# Perl always returns a value from any subroutine call, as it is built
# as an expression-oriented (applicative, function, remember?) language.
# Therefore, the last statement in a package must be a true value to
# indicate it loaded correctly.
1;
|
|
|
|