Topic: Multiple Inheritance and Abstract base class


Author: Marc Girod <girod@stybba.ntc.nokia.com>
Date: 1998/04/17
Raw View
>>>>> "JK" == jkanze  <jkanze@otelo.ibmmail.com> writes:

JK> In article <3535F767.77F912A5@erols.com>,

JK> As a general rule, derivation should be virtual.  Here, for example, if
JK> all of the derivations from A where virtual, the code would work
JK> under the dominance rule.

All this is correct and I agree :-) apart for one detail: there is no
"dominance rule" anymore (unless it has been reinserted since CD2).

Best Regards!

--
Marc Girod                Valimo 1/2         Voice:  +358-9-511 63331
Nokia Telecommunications  P.O. Box 315       Mobile: +358-40-569 7954
NWS/NMS/NMS for Data      00045 NOKIA Group  Fax:    +358-9-511 63310
                          Finland            marc.girod@ntc.nokia.com



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Susan D. McClain" <dmcclain@erols.com>
Date: 1998/04/16
Raw View
I'm trying to use multiple inheritance with an abstract base class and
don't understand why this isn't working.  I have something like,

class A
{
     virtual void DoSomething() = 0;
}

class B : public A
{
     B() {;}
     virtual void DoSomething() {;}
}

class C : public A, public B
{
      C() {;}
}

The compiler won't let me instantiate a class C object saying that class
C is abstract
because it doesn't implement the DoSomething method.  Why doesn't it get
the
implementation from class B?

Thanks for any enlightenment you can offer,

Sue McClain
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jkanze@otelo.ibmmail.com
Date: 1998/04/16
Raw View
In article <3535F767.77F912A5@erols.com>,
  dmcclain@erols.com wrote:
>
> I'm trying to use multiple inheritance with an abstract base class and
> don't understand why this isn't working.  I have something like,
>
> class A
> {
>      virtual void DoSomething() = 0;
> }
>
> class B : public A
> {
>      B() {;}
>      virtual void DoSomething() {;}
> }
>
> class C : public A, public B
> {
>       C() {;}
> }
>
> The compiler won't let me instantiate a class C object saying that class
> C is abstract
> because it doesn't implement the DoSomething method.  Why doesn't it get
> the
> implementation from class B?

It does, but you have two instances of class A; the implementation in
class B only defines one of them.

As a general rule, derivation should be virtual.  Here, for example, if
all of the derivations from A where virtual, the code would work
under the dominance rule.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
        +49 (0)69 66 45 33 10    mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient   e objet --
              -- Beratung in objektorientierter Datenverarbeitung

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: mh43@acf5.nyu.edu (Mark Halvin)
Date: 1998/04/16
Raw View
"Susan D. McClain" <dmcclain@erols.com> writes:

>I'm trying to use multiple inheritance with an abstract base class and
>don't understand why this isn't working.  I have something like,

>class A
>{
>     virtual void DoSomething() = 0;
>}

>class B : public A
>{
>     B() {;}
>     virtual void DoSomething() {;}
>}

>class C : public A, public B
>{
>      C() {;}
>}

>The compiler won't let me instantiate a class C object saying that class
>C is abstract

The compiler is correct -- class C inherits two of class A, and only the
one that is a base class of class B has an implemented DoSomething()
function.

>because it doesn't implement the DoSomething method.  Why doesn't it get
>the
>implementation from class B?

It does! You're just forgetting the one directly inherited from class A.

>Thanks for any enlightenment you can offer,

If you really only want one class A in your class C, then use virtual
inheritance instead of public inheritance:

class A {
protected:
    virtual void DoSomething() = 0;
};
class B : virtual A {
protected:
    B() {;}
    virtual void DoSomething() {;}
};
class C : virtual A, virtual B {  //class C has ONE class A in it
public:
    C() {;}
};

Note that this assumes you don't want to instantiate any class A or class
B objects -- just class C objects. If this is not the case, you should
make the ctors and DoSomething() members public instead of protected.

--
---
Mark.Halvin@nyu.edu
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: spamme@rocketmail.com
Date: 1998/04/16
Raw View
In article <3535F767.77F912A5@erols.com>,
  dmcclain@erols.com wrote:
>
> I'm trying to use multiple inheritance with an abstract base class and
> don't understand why this isn't working.  I have something like,
>
> class A
> {
>      virtual void DoSomething() = 0;
> }
>
> class B : public A
> {
>      B() {;}
>      virtual void DoSomething() {;}
> }
>
> class C : public A, public B
> {
>       C() {;}
> }
>
> The compiler won't let me instantiate a class C object saying that class
> C is abstract
> because it doesn't implement the DoSomething method.  Why doesn't it get
> the
> implementation from class B?
>
> Thanks for any enlightenment you can offer,
>
> Sue McClain

Hi Sue,

First of all, declare your members as "public".  They are "private" by
default.  Secondly, since classes C and B both derive from A, you should use
virtual inheritance:

class B : virtual public A { ...
class C : virtual public A, public B { ...

HTH,

spamme

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: rsailors@wyoming.com (Skip Sailors)
Date: 1998/04/17
Raw View
>On 16 Apr 98 13:37:29 GMT, "Susan D. McClain" <dmcclain@erols.com> wrote:

>I'm trying to use multiple inheritance with an abstract base class and
>don't understand why this isn't working.  I have something like,
>
>class A
>{
>     virtual void DoSomething() = 0;
>}
>
>class B : public A
>{
>     B() {;}
>     virtual void DoSomething() {;}
>}
>
>class C : public A, public B
>{
>      C() {;}
>}

Class C IS-A A (and IS-A B.)  It may need to provide A-like behavior
(as it may need to provide B-like behavior.)  But an A has a pure
virtual function DoSomething() which C has chosen not to implement.  C
must be abstract. QED

Notice that I don't have to consider the B-ness of C to determine how
it speaks to the A-ness of C.

Later, if I were to decide for instance that C Is-not-a B, but rather
implemented-in-terms-of B, I could make the derivation private, or
make B a data member, and the A-ness of C would survive.

Hence the language encourages us to loosen our dependence on what B is
for any of the set of As, including those that happen to be Cs.

Thus it is written thusly.

HTH
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]