Real OOP with D

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 16 23:10:27 PDT 2015


On 17/08/2015 5:57 p.m., Ozan wrote:
> Hi
>
> Working with objectoriented concepts results often in large trees of
> related classes. Every instance of a class knows his methods and data.
> An example like following would work:
>
> import std.stdio;
> class Family { }
> class Dad : Family { void greeting() { writeln("I'm dad"); } }
> class Boy : Family { void greeting() { writeln("I'm daddy's boy"); } }
> void main() {
> writeln("Father and son");
> Dad father = new Dad;
> Family son = new Boy;
> father.greeting;
> son.greeting;
> }
>
> The critical point is using a variable of type Family for an instance of
> Boy. Class Family covers the greeting method of Boy. In real OOP that
> would not be a problem, because the access point of view starts with the
> object. In D, it starts with the class definition.
>
> Is there any way to get real OOP with D?
>
> Regards,  Ozan

import std.stdio;
abstract class Family { void greeting(); }
class Dad : Family { void greeting() { writeln("I'm dad"); } }
class Boy : Family { void greeting() { writeln("I'm daddy's boy"); } }

void main() {
	Family dad = new Dad;
	Family boy = new Boy;
	dad.greeting;
	boy.greeting;
}

I'm confused how this isn't real OOP?


More information about the Digitalmars-d-learn mailing list