www.digitalmars.com         C & C++   DMDScript  

D - Abstract Classes

reply "Don Stewart" <donald.m.stewart btinternet.com> writes:
I have a base class Foo that I have defined as abstract. Derived from this
is class Bar. I get an error compiling Foo as it's construcotr has a method
body. This is what I am trying to do :-

abstract class Foo {
    protected int a;

    this( int a ) {
        this.a = a;
    }
}

class Bar : Foo {
    this ( int a ) {
        super ( a );
    }
}

Is this supposed to fail in D ?
May 21 2002
next sibling parent "Don Stewart" <donald.m.stewart btinternet.com> writes:
Okay so I should make the variable private in Foo :o)

However, the question remains.

"Don Stewart" <donald.m.stewart btinternet.com> wrote in message
news:acgvf6$1hou$1 digitaldaemon.com...
 I have a base class Foo that I have defined as abstract. Derived from this
 is class Bar. I get an error compiling Foo as it's construcotr has a
method
 body. This is what I am trying to do :-

 abstract class Foo {
     protected int a;

     this( int a ) {
         this.a = a;
     }
 }

 class Bar : Foo {
     this ( int a ) {
         super ( a );
     }
 }

 Is this supposed to fail in D ?
May 21 2002
prev sibling parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Don Stewart" <donald.m.stewart btinternet.com> wrote in message
news:acgvf6$1hou$1 digitaldaemon.com...
 I have a base class Foo that I have defined as abstract. Derived from this
 is class Bar. I get an error compiling Foo as it's construcotr has a
method
 body. This is what I am trying to do :-

 abstract class Foo {
     protected int a;

     this( int a ) {
         this.a = a;
     }
 }

 class Bar : Foo {
     this ( int a ) {
         super ( a );
     }
 }

 Is this supposed to fail in D ?
Mmm... I thought abstract classes should have no implementation? I know it is possible to do this in C++, but why it should be done I don't understand... Do you want to force the programmer deriving a class from Foo to implement certain functions? Then why not make just the functions abstract? On another note you might want to look into interfaces as a substitute for Abstract Base Classes in D... -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
May 23 2002
parent reply "Don Stewart" <donald.m.stewart btinternet.com> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:acjm92$137a$2 digitaldaemon.com...
 "Don Stewart" <donald.m.stewart btinternet.com> wrote in message
 news:acgvf6$1hou$1 digitaldaemon.com...
 I have a base class Foo that I have defined as abstract. Derived from
this
 is class Bar. I get an error compiling Foo as it's construcotr has a
method
 body. This is what I am trying to do :-

 abstract class Foo {
     protected int a;

     this( int a ) {
         this.a = a;
     }
 }

 class Bar : Foo {
     this ( int a ) {
         super ( a );
     }
 }

 Is this supposed to fail in D ?
Mmm... I thought abstract classes should have no implementation? I know it is possible to do this in C++, but why it should be done I don't understand... Do you want to force the programmer deriving a class from Foo to implement certain functions? Then why not make just the functions abstract?
Because I want to create a class that has fields and methods that can manipulate the fields BUT not the class to be new'ed. Only derived classes can be newed. It's in C++, it's in Java, I wondered why it was not in D. An interface would mean I have to add the fields to all implementing classes rather than using the base class.
 On another note you might want to look
 into interfaces as a substitute for
 Abstract Base Classes in D...


 --
 Stijn
 OddesE_XYZ hotmail.com
 http://OddesE.cjb.net
 _________________________________________________
 Remove _XYZ from my address when replying by mail
May 23 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Don Stewart" <donald.m.stewart btinternet.com> wrote in message
news:ackskg$2b2m$1 digitaldaemon.com...

 Because I want to create a class that has fields and methods that can
 manipulate the fields BUT not the class to be new'ed.
Just declare its constructor as private. It is the way it was done in C++, and I think it should be done so in D as well. Abstract class is a class with all members abstract. If you only want to define some of the members abstract, just use the "abstract" attributes on them. You still can't create such class, but you can define bodies for non-abstract methods.
May 24 2002
parent "Don Stewart" <donald genient.com> writes:
Yes of course. I seen the years of Java have killed off the C++ brain cells
:o)

My brain hasn't managed to context switch yet LOL

Don
"Pavel Minayev" <evilone omen.ru> wrote in message
news:acl7ni$4ed$1 digitaldaemon.com...
 "Don Stewart" <donald.m.stewart btinternet.com> wrote in message
 news:ackskg$2b2m$1 digitaldaemon.com...

 Because I want to create a class that has fields and methods that can
 manipulate the fields BUT not the class to be new'ed.
Just declare its constructor as private. It is the way it was done in C++, and I think it should be done so in D as well. Abstract class is a class with all members abstract. If you only want to define some of the members abstract, just use the "abstract" attributes on them. You still can't create such class, but you can define bodies for non-abstract methods.
May 24 2002