Clunky syntax

Chris Cain clcain at uncg.edu
Mon May 7 11:03:43 PDT 2012


On Monday, 7 May 2012 at 17:52:01 UTC, ixid wrote:
> Thank you, could you explain what is happening in your example? 
> Bar is inheriting from Foo, what are you getting when you 
> create a parent of type sub-class compared  to Bar b = new Bar; 
> and Foo b = new Foo; ? Foo b = new Bar won't compile if you add 
> members to Bar and access them.

Hello,

Foo is the "interface" you'll have to bar.

So, a bit of a bigger example:


-=-=-=-
import std.stdio;
class Foo {
     void doStuff() {}
}
class Bar : Foo {
     void doStuff() {
         writeln("Hi");
     }
     void doThings() { }
}

void main() {
     Foo f = new Bar;
     f.doStuff(); // prints "Hi" to the screen
     f.doThings(); // doesn't compile
}
-=-=-=-

So, as you can see, if you have a Foo, you can't call "doThings" 
using it. However, if your Foo is actually a Bar underneath, then 
it'll use Bar's version of "doStuff".

OOP isn't terribly hard, but I suggest reading up on it some to 
grasp the concepts (and especially so you can see the benefits).

Here's a link that might help you get started on some of the 
fundamentals:
http://en.wikipedia.org/wiki/Object-oriented_programming#Fundamental_features_and_concepts


More information about the Digitalmars-d-learn mailing list