Inheritance/Single: Difference between revisions
added python |
added perl, hopefully it's right |
||
Line 97:
(*functions go here...*)
end</ocaml>
=={{header|Perl}}==
<perl>package Animal;
#functions go here...
1;</perl>
<perl>package Dog;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;</perl>
<perl>package Cat;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;</perl>
<perl>package Lab;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;</perl>
<perl>package Collie;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;</perl>
=={{header|Python}}==
|
Revision as of 08:39, 28 April 2008
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Show a tree of classes which inherit from each other. The top of the tree should be a class called Animal. The second level should have Dog and Cat. Under Dog should be Lab and Collie. None of the classes need to have any functions, the only thing they need to do is inherit from the specified superclasses (overriding functions should be shown in Polymorphism). The tree should look like this:
Animal /\ / \ / \ Dog Cat /\ / \ / \ Lab Collie
Ada
<ada> package Inheritance is
type Animal is tagged private; type Dog is new Animal with private; type Cat is new Animal with private; type Lab is new Dog with private; type Collie is new Dog with private;
private
type Animal is tagged null record; type Dog is new Animal with null record; type Cat is new Animal with null record; type Lab is new Dog with null record; type Collie is new Dog with null record;
end Inheritance; </ada>
C++
<cpp> class Animal {
// ...
};
class Dog: public Animal {
// ...
};
class Lab: public Dog {
// ...
};
class Collie: public Dog {
// ...
};
class Cat: public Animal {
// ...
}; </cpp>
Java
<java>public class Animal{
//functions go here...
}</java> <java>public class Dog extends Animal{
//functions go here...
}</java> <java>public class Cat extends Animal{
//functions go here...
}</java> <java>public class Lab extends Dog{
//functions go here...
}</java> <java>public class Collie extends Dog{
//functions go here...
}</java>
OCaml
<ocaml>class animal =
object (self) (*functions go here...*) end</ocaml>
<ocaml>class dog =
object (self) inherit animal (*functions go here...*) end</ocaml>
<ocaml>class cat =
object (self) inherit animal (*functions go here...*) end</ocaml>
<ocaml>class lab =
object (self) inherit dog (*functions go here...*) end</ocaml>
<ocaml>class collie =
object (self) inherit dog (*functions go here...*) end</ocaml>
Perl
<perl>package Animal;
- functions go here...
1;</perl>
<perl>package Dog; use Animal; @ISA = qw( Animal );
- functions go here...
1;</perl>
<perl>package Cat; use Animal; @ISA = qw( Animal );
- functions go here...
1;</perl>
<perl>package Lab; use Dog; @ISA = qw( Dog );
- functions go here...
1;</perl>
<perl>package Collie; use Dog; @ISA = qw( Dog );
- functions go here...
1;</perl>
Python
<python>class Animal:
pass #functions go here...</python>
<python>class Dog(Animal):
pass #functions go here...</python>
<python>class Cat(Animal):
pass #functions go here...</python>
<python>class Lab(Dog):
pass #functions go here...</python>
<python>class Collie(Dog):
pass #functions go here...</python>