/***************************************************************************** * File: oop.decaf * Author: Keith Schwarz (htiek@cs.stanford.edu) * * Sample Decaf code showing off its object-oriented features. */ class Animal { // Animals make great inheritance examples! :-) string getName() { return "Some animal"; } int getMass() { ///////////////////////////////////////////////////////////////////// // There's no good implementation for this method. Decaf does not // // support purely abstract methods, but it does allow you to have // // a method that just falls off the end. That's what we'll do // // here. // ///////////////////////////////////////////////////////////////////// } } class Dog extends Animal { // Inheritance! string getName() { return "Dog"; } int getMass() { return 20; } } class Cat extends Animal { string getName() { return "Cat"; } int getMass() { return 4; } } /* Utility function to print out a description of an animal. */ void PrintAnimal(Animal animal) { Print(animal.getName()); Print(" has mass "); Print(animal.getMass()); Print("kg\n"); } int main() { PrintAnimal(new Dog); PrintAnimal(new Cat); }