docs.roxen.comView this page in a printer friendly mode
DocsPike7.0TutorialObject-Oriented Programming
Copyright © 2012, Roxen Internet Software
Suggestions, comments & compliments
manuals@roxen.com
 DEMO  DOCS  PIKE
 COMMUNITY  DOWNLOAD
www.roxen.com

   

Object Orientation in General
Object Orientation in Pike
Creating and Using Objects
How to Create a Class
Classes as Record Types
Programs are Classes and Vice Versa
Inheritance
Multiple Inheritance
Access control

Multiple Inheritance

Sometimes we want to inherit from two or more classes. This works in Pike (and in C++, but not in Java). You just write several inherits.

Lets say we have a class friend, that represents a friend:

class friend
{
  void cuddle()
  {
    write("Cuddle, cuddle, cuddle!\n");
  }
}

A hamster, as we all know, is both an animal and a friend, and it can also dance:

class hamster
{
  inherit animal;
  inherit friend;

  void dance()
  {
    write(name + " dances.\n");
  }
}

So, try it out:

hamster h = hamster("Blue Lightning", 0.12);
h->cuddle();      // Cuddle as a friend
h->eat("grain");  // Eat as an animal
h->dance();       // Dance as a hamster