"Sputnik" help  
Sputnik Help
Language Reference - Classes - Class Inheritance

Class Inheritance

In this example we make a base class and inherit it and the new class gains the old classes functions etc.

class Cat
{
    my $name;
    my $lives;
    Function Meow()
    {
        say "$name says Meow!";
    }
}

class SuperCat inherits "Cat"
{
    my $speed;
    Function __Construct($n, $l, $s)
    {
        $name = $n;
        $lives = $l;
        $speed = $s;
    }
}

my $felix = new SuperCat("Felix", 9, 4);
my $fluffy = new SuperCat("Fluffy", 4, 6);

say "Felix lives: " . $felix->$lives;
say "Felix speed: " . $felix->$speed;
say "Fluffy lives: " . $fluffy->$lives;
say "Fluffy speed: " . $fluffy->$speed;
$felix->Meow();
$fluffy->Meow();

// PRINTS
// Felix lives: 9
// Felix speed: 4
// Fluffy lives: 4
// Fluffy speed: 6
// Felix says Meow!
// Fluffy says Meow!

Notice the class "SuperCat" has access to the "Meow" function from the class "Cat"? That is one the benefits of inheritance.

Sputnik supports multiple inhertance (use commas , to name them all) so you can even do this:

class Cat
{
    my $name;
    my $lives;
    Function Meow()
    {
        say "$name says Meow!";
    }
}

class Collar
{
    my $name;
    Function Collar()
    {
        say "$name's collar rattles";
    }
}

class SuperCat inherits "Cat,Collar"
{
    my $speed;
    Function __Construct($n, $l, $s)
    {
        $name = $n;
        $lives = $l;
        $speed = $s;
    }
}

my $felix = new SuperCat("Felix", 9, 4);
my $fluffy = new SuperCat("Fluffy", 4, 6);

say "Felix lives: " . $felix->$lives;
say "Felix speed: " . $felix->$speed;
say "Fluffy lives: " . $fluffy->$lives;
say "Fluffy speed: " . $fluffy->$speed;
$felix->Meow();
$fluffy->Meow();
$felix->Collar();
$fluffy->Collar();

// PRINTS
// Felix lives: 9
// Felix speed: 4
// Fluffy lives: 4
// Fluffy speed: 6
// Felix says Meow!
// Fluffy says Meow!
// Felix's collar rattles
// Fluffy's collar rattles

To the nature of multiple inheritance there is no "base" or "parent" class since it could literally be anything at all. So you might expect do something like "base:functionName()" to call a base function however that does not work instead you must create a referance and call that for example:

class Cat
{
    my $name;
    my $lives;
    Function __Construct($n, $l)
    {
        $name = $n;
        $lives = $l;
    }
    Function Meow()
    {
        say "$name says Meow!";
    }
}

class SuperCat inherits "Cat"
{
    my $speed;
    Function __Construct($n, $l, $s)
    {
        (&Cat:__Construct)($n, $l);
        $speed = $s;
    }
}

my $felix = new SuperCat("Felix", 9, 4);
my $fluffy = new SuperCat("Fluffy", 4, 6);

say "Felix lives: " . $felix->$lives;
say "Felix speed: " . $felix->$speed;
say "Fluffy lives: " . $fluffy->$lives;
say "Fluffy speed: " . $fluffy->$speed;
$felix->Meow();
$fluffy->Meow();

// PRINTS
// Felix lives: 9
// Felix speed: 4
// Fluffy lives: 4
// Fluffy speed: 6
// Felix says Meow!
// Fluffy says Meow!
Notice the "(&Cat:__Construct)($n, $l);" ? This first creates a reference to the function "__Construct" within the class "Cat" then calls that function using the "($n, $l)" parameters.

For example here we use it as a variable:

class Cat
{
    my $name;
    my $lives;
    Function __Construct($n, $l)
    {
        $name = $n;
        $lives = $l;
    }
    Function Meow()
    {
        say "$name says Meow!";
    }
}

class SuperCat inherits "Cat"
{
    my $speed;
    Function __Construct($n, $l, $s)
    {
        my $parentConstructor = &Cat:__Construct;
        $parentConstructor($n, $l);
        $speed = $s;
    }
}

my $felix = new SuperCat("Felix", 9, 4);
my $fluffy = new SuperCat("Fluffy", 4, 6);

say "Felix lives: " . $felix->$lives;
say "Felix speed: " . $felix->$speed;
say "Fluffy lives: " . $fluffy->$lives;
say "Fluffy speed: " . $fluffy->$speed;
$felix->Meow();
$fluffy->Meow();

// PRINTS
// Felix lives: 9
// Felix speed: 4
// Fluffy lives: 4
// Fluffy speed: 6
// Felix says Meow!
// Fluffy says Meow!

Notice "$parentConstructor" contains the function we need so we can call the right function.

Of course we could just call the function directly instead of making a ref then calling it like so:

class Cat
{
    my $name;
    my $lives;
    Function __Construct($n, $l)
    {
        $name = $n;
        $lives = $l;
    }
    Function Meow()
    {
        say "$name says Meow!";
    }
}

class SuperCat inherits "Cat"
{
    my $speed;
    Function __Construct($n, $l, $s)
    {
        Cat:__Construct($n, $l);
        $speed = $s;
    }
}

my $felix = new SuperCat("Felix", 9, 4);
my $fluffy = new SuperCat("Fluffy", 4, 6);

say "Felix lives: " . $felix->$lives;
say "Felix speed: " . $felix->$speed;
say "Fluffy lives: " . $fluffy->$lives;
say "Fluffy speed: " . $fluffy->$speed;
$felix->Meow();
$fluffy->Meow();

// PRINTS
// Felix lives: 9
// Felix speed: 4
// Fluffy lives: 4
// Fluffy speed: 6
// Felix says Meow!
// Fluffy says Meow!

Here another interesting way to do it using the Rule Prefix:

class Cat
{
    my $name;
    my $lives;
    "^" Function __Construct($n, $l)
    {
        $name = $n;
        $lives = $l;
    }
    Function Meow()
    {
        say "$name says Meow!";
    }
}

class SuperCat inherits "Cat"
{
    my $speed;
    Function __Construct($n, $l, $s)
    {
        $speed = $s;
    }
}

my $felix = new SuperCat("Felix", 9, 4);
my $fluffy = new SuperCat("Fluffy", 4, 6);

say "Felix lives: " . $felix->$lives;
say "Felix speed: " . $felix->$speed;
say "Fluffy lives: " . $fluffy->$lives;
say "Fluffy speed: " . $fluffy->$speed;
$felix->Meow();
$fluffy->Meow();

// PRINTS
// Felix lives: 9
// Felix speed: 4
// Fluffy lives: 4
// Fluffy speed: 6
// Felix says Meow!
// Fluffy says Meow!

Basically the "^" on the original "__Construct" function causes it to get inserted at the start of the inherting "__Construct" function.


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org