Thread: Inheritance

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    231
    Rep Power
    16

    Inheritance


    Hi all,

    I am not sure why I cannot access the parent method from a child class. The problem is at the Hourly->write(). Please help. Thanks alot!


    Code:
     #!/usr/bin/perl
    # Implementation of class Employee.
    
    package Employee;
    
    use strict;
    use warnings;
    use Date;
    
    sub new
    {
       my $type = shift();
       my $class = ref( $type ) || $type;
    
       my $hireDay = new Date;
       my $self = { firstName => undef,
       		lastName  => undef,
       		hireDay   => $hireDay };
    
       bless( $self, $class );
       return $self;
    }
    
    sub firstName
    {
       my $self = shift();
    
       $self->{ firstName } = shift() if ( @_ );
    
       return $self->{ firstName };
    }
    
    sub lastName
    {
       my $self = shift();
    
       $self->{ lastName } = shift() if ( @_ );
    
       return $self->{ lastName };
    }
    
    sub hireDay
    {
       my $self = shift();
    
       if ( @_ )
       {
          $self->{ hireDay }->setDate( @_ );
       }
       else
       {
          $self->{ hireDay }->print();
       }
    }
    
    return 1;
    Code:
    #!/usr/bin/perl
    # Implementation of class Hourly.
    
    package Hourly;
    
    use strict;
    use warnings;
    use Employee;
    our @ISA = ( "Employee" );
    
    sub new
    {
       my $object = shift();
       my $class = ref( $object ) || $object;
    
       my $self = $class->SUPER::new();
       $self->{ rate } = undef;
    
       bless( $self, $class );
       return $self;
    }
    
    sub rate
    {
       my $self = shift();
    
       $self->{ rate } = shift() if ( @_ );
    
       return $self->{ rate };
    }
    
    sub write
    {
       my $self = shift();
    
       print( $self->firstname(), "\n" );
    }
    
    
    return 1;

    Code:
    #!/usr/bin/perl
    # Using classes Hourly and Employee
    
    use strict;
    use warnings;
    use Employee;
    use Hourly;
    
    my $hour = new Hourly;
    
    $hour->firstName( "John" );
    $hour->lastName( "White" );
    $hour->hireDay( 11, 30, 1999 );
    $hour->rate( 9.50 );
    print( $hour->firstName(), " ", $hour->lastName(),
           " was hired on " );
    $hour->hireDay();
    print( ".\n" );
    printf( "He makes \$%.2f per hour.\n", $hour->rate() );
    
    $hour->write();
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    231
    Rep Power
    16
    Never mind. I found the problem. It should be $self->firstName(). Thanks anyway!

    Comments on this post

    • icrf agrees : we love to see people answer their own questions
  4. #3
  5. Reinvent the Circle
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Jonesville, MI
    Posts
    218
    Rep Power
    19
    In case anyone shows up in this thread looking for inheretance information...

    Read http://www.emerson.emory.edu/service...ml#perlobj_a_0
    for @ISA information.

    Read this (Google-converted PDF) for Perl's Inheritence. Note the information about "use base PACKAGENAME"
    -Yanno

    "If it will have to be done more than once, don't do it. Make something that does it for you."

    "If you want to get out of the box, you must not think outside the box, you must think about the box. In fact, think about destroying the box."

IMN logo majestic logo threadwatch logo seochat tools logo