<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://wiki.lostsouls.org/w/skins/common/feed.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Man inheritance - Revision history</title>
		<link>http://wiki.lostsouls.org/w/index.php?title=Man_inheritance&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.8.2</generator>
		<lastBuildDate>Sun, 10 May 2026 10:34:30 GMT</lastBuildDate>
		<item>
			<title>Laine at 16:29, 11 June 2007</title>
			<link>http://wiki.lostsouls.org/w/index.php?title=Man_inheritance&amp;diff=3926&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Description==&lt;br /&gt;
Have you noticed how many objects in the system have the same&lt;br /&gt;
functionality in common? Let's look at rooms for instance, they&lt;br /&gt;
all have the ability to host people and provide commands. It's&lt;br /&gt;
not that every room is programmed with the same basic functions&lt;br /&gt;
again and again, rather it will use a model room and then make&lt;br /&gt;
some special changes to it. That doesn't work by copying the&lt;br /&gt;
file.. Ouch! Don't replicate code! But by putting a tiny inherit&lt;br /&gt;
declaration&lt;br /&gt;
        &lt;br /&gt;
    inherit &amp;quot;&amp;lt;model-class&amp;gt;&amp;quot;;&lt;br /&gt;
                &lt;br /&gt;
at the beginning of your new file. This must come before any local&lt;br /&gt;
variables or functions. Once inherited your class will behave just&lt;br /&gt;
like the model class, because all the public methods are available&lt;br /&gt;
to the outside world. Now it is in your hands to change such an&lt;br /&gt;
inherited behavior. You have the following tools to do so:&lt;br /&gt;
&lt;br /&gt;
* Access to variables - It is one of the best design decisions in LPC that variables&lt;br /&gt;
are not accessible from outside, but you can use inherited&lt;br /&gt;
variables just as if they were your own. Modifiers apply however.&lt;br /&gt;
&lt;br /&gt;
* Method overloading&lt;br /&gt;
                int method_that_also_exists_in_the_model() {&lt;br /&gt;
                        &amp;lt;your new code&amp;gt;&lt;br /&gt;
                }&lt;br /&gt;
&lt;br /&gt;
You can simply rewrite a method that is also defined in the model&lt;br /&gt;
class, and thus change how it behaves. Contrary to other languages&lt;br /&gt;
in LPC method overloading only matches the name of the method, so&lt;br /&gt;
even by changing the amount and type of parameters you will mask&lt;br /&gt;
out the original version of the method. You can even apply other&lt;br /&gt;
modifiers to it as the original.&lt;br /&gt;
&lt;br /&gt;
* Calling inherited methods&lt;br /&gt;
&lt;br /&gt;
    int method_that_also_exists_in_the_model() {&lt;br /&gt;
        &amp;lt;your new code&amp;gt;&lt;br /&gt;
        return ::method_that_also_exists_in_the_model();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
You can add to the behaviour of a method by redefining it,&lt;br /&gt;
then calling it from within your new version. You can actually&lt;br /&gt;
call inherited methods from anywhere in your code. The double&lt;br /&gt;
colon tells the compiler you are looking for the inherited&lt;br /&gt;
variant.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let's imagine very simple food in a file called &amp;quot;/the/food.c&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
                // unless &amp;quot;modified&amp;quot; variables are accessible by inheritors&lt;br /&gt;
                int vitamins = 10;&lt;br /&gt;
&lt;br /&gt;
                // please overload this function with your own description&lt;br /&gt;
                public short() { return &amp;quot;something edible&amp;quot;; }&lt;br /&gt;
&lt;br /&gt;
                // let's do some standard action for food&lt;br /&gt;
                public consume() {&lt;br /&gt;
                        this_player() -&amp;gt; nourish(vitamins);&lt;br /&gt;
                        destruct(this_object());&lt;br /&gt;
                }&lt;br /&gt;
&lt;br /&gt;
And now someone else decides to do some italian cooking in a&lt;br /&gt;
file called &amp;quot;/the/fusilli.c&amp;quot;&lt;br /&gt;
&lt;br /&gt;
                inherit &amp;quot;/the/food&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
                // we have our own variables.&lt;br /&gt;
                int gone_cold = 0;&lt;br /&gt;
&lt;br /&gt;
                // and we simply redefine the short() function to replace it&lt;br /&gt;
                public short() {&lt;br /&gt;
                    // description changes depending on gone_cold&lt;br /&gt;
                    return &amp;quot;a &amp;quot;+( gone_cold ? &amp;quot;stinking&amp;quot; : &amp;quot;steaming&amp;quot; )&lt;br /&gt;
                            +&amp;quot; plate of fusilli&amp;quot;;&lt;br /&gt;
                }&lt;br /&gt;
&lt;br /&gt;
                // we have a new function to make food go cold&lt;br /&gt;
                private deteriorate() {&lt;br /&gt;
                    gone_cold = 1;&lt;br /&gt;
                    write(&amp;quot;The fusilli have gone cold.\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
&lt;br /&gt;
                // assume this gets called at creation&lt;br /&gt;
                private create() {&lt;br /&gt;
                    // we can access the variable we inherited from food.c&lt;br /&gt;
                    vitamins = 44;           // tomato has plenty of vitamins&lt;br /&gt;
&lt;br /&gt;
                    // go cold in 5 minutes&lt;br /&gt;
                    call_out( #'deteriorate, 5 * 60 );&lt;br /&gt;
                }&lt;br /&gt;
&lt;br /&gt;
                // we can overload the function even with new parameters&lt;br /&gt;
                public consume(how) {&lt;br /&gt;
                    // fetch the name of the person, or use &amp;quot;Someone&amp;quot;&lt;br /&gt;
                    string name = this_player() -&amp;gt; name() || &amp;quot;Someone&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
                    if (!gone_cold) {&lt;br /&gt;
                        write(&amp;quot;You enjoy a delicious plate of fusilli.\n&amp;quot;);&lt;br /&gt;
                        say(name +&amp;quot; guzzles a plate of hot fusilli.\n&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    else if (how == &amp;quot;quickly&amp;quot;) {&lt;br /&gt;
                        write(&amp;quot;You eat the fusilli so quickly you &amp;quot;&lt;br /&gt;
                              &amp;quot;hardly notice they have gone cold.\n&amp;quot;);&lt;br /&gt;
                        say(name +&amp;quot; wolfs down a plate of cold fusilli.\n&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                    else {&lt;br /&gt;
                        write(&amp;quot;You eye the plate and wonder if you &amp;quot;&lt;br /&gt;
                              &amp;quot;really feel like eating cold fusilli.\n&amp;quot;);&lt;br /&gt;
                        return; // don't eat&lt;br /&gt;
                    }&lt;br /&gt;
&lt;br /&gt;
                    // and here comes the most important part:&lt;br /&gt;
                    // we execute consume() from food.c, so we&lt;br /&gt;
                    // actually inherit its behaviour.&lt;br /&gt;
                    ::consume();&lt;br /&gt;
                }&lt;br /&gt;
&lt;br /&gt;
==Advanced Usage==&lt;br /&gt;
&lt;br /&gt;
* Doing multiple inheritance&lt;br /&gt;
&lt;br /&gt;
While the Java(TM) language has so-called interfaces as a kludge,&lt;br /&gt;
LPC doesn't need them as it supports real multiple inheritance.&lt;br /&gt;
A very powerful feature, it lets you combine the behaviour of&lt;br /&gt;
several classes into a new one. Simply put several lines of&lt;br /&gt;
inherit declarations underneath each other. If you have name&lt;br /&gt;
collisions in the namespace of inherited methods, you will have&lt;br /&gt;
to address them explicitely with a &amp;quot;the/file&amp;quot;::method(args) syntax.&lt;br /&gt;
&lt;br /&gt;
* Wildcarded multiple inheritance&lt;br /&gt;
&lt;br /&gt;
LDMUD 3.2.1@117 introduces an advanced voodoo syntax which allows&lt;br /&gt;
you to call several methods in model classes at once, but for some&lt;br /&gt;
technical reasons it cannot pass any arguments. This works by&lt;br /&gt;
writing a glob type match ('*' and '?' wildcards) into the string&lt;br /&gt;
in front of the double colon, as in &amp;quot;*&amp;quot;::create(). I wouldn't&lt;br /&gt;
recommend you to use this, it's better to be clearly conscious of&lt;br /&gt;
what you inherit and do. But if you're desperate, there you go.&lt;br /&gt;
&lt;br /&gt;
==Advanced Example==&lt;br /&gt;
&lt;br /&gt;
          inherit &amp;quot;foo&amp;quot;;&lt;br /&gt;
          inherit &amp;quot;bar&amp;quot;;&lt;br /&gt;
          inherit &amp;quot;baz&amp;quot;;&lt;br /&gt;
          inherit &amp;quot;ball&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
          reset() {&lt;br /&gt;
                &amp;quot;ba?&amp;quot;::reset();&lt;br /&gt;
                // calls bar::reset() and baz::reset()&lt;br /&gt;
&lt;br /&gt;
                &amp;quot;ba*&amp;quot;::reset();&lt;br /&gt;
                // calls bar::reset(), baz::reset() and ball::reset()&lt;br /&gt;
&lt;br /&gt;
                &amp;quot;*&amp;quot;::reset();&lt;br /&gt;
                // calls every inherited reset() function.&lt;br /&gt;
&lt;br /&gt;
                &amp;quot;ball&amp;quot;::rejoice(&amp;quot;Listen to italectro today!&amp;quot;);&lt;br /&gt;
                // only explicit filename of model class allows&lt;br /&gt;
                // passing arguments to the inherited method&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[[man functions|functions(LPC)]], [[man initialisation|initialisation(LPC)]], [[man modifiers|modifiers(LPC)]], [[man pragma|pragma(LPC)]],&lt;br /&gt;
[[man function exists|function_exists(efun)]], [[man functionlist|functionlist(efun)]], [[man inherit list|inherit_list(efun)]], [[man symbol variable|symbol_variable(efun)]], [[man variable exists|variable_exists(efun)]], [[man variable list|variable_list(efun)]].&lt;/div&gt;</description>
			<pubDate>Mon, 11 Jun 2007 16:29:52 GMT</pubDate>			<dc:creator>Laine</dc:creator>			<comments>http://wiki.lostsouls.org/Talk:Man_inheritance</comments>		</item>
	</channel>
</rss>