1

update: I just got to know c++ supports multiple inheritance. I think multiple inheritance is the best way to solve this problem..

I have a question about multiple derived classes sharing method.. The code is as below:

class Base{
public:
    virtual double method1();
    virtual double method2();
}

class A::public Base{
public:
    virtual double method1();
    virtual double method2();
}

class B::public Base{
public:
    virtual double method1();
    virtual double method2();
}

class C::public Base{
public:
    virtual double method1();
    virtual double method2();
}

class D::public Base{
public:
    virtual double method1();
    virtual double method2();
}

For class A and B, method1() is the same; for class C and B method1() is the same. (it uses the base class members as arguments).

Instead of writing the same method1() for class A and class B, I think this can be simplified by:

class subBase1::public Base{
public:
    void double method1();
}
class A::public subBase1{}
class B::public subBase1{}


class subBase2::public Base{
public:
    void double method1();
}
class A::public subBase2{}
class B::public subBase2{}

But now for class B and C, method2() is also the same (it uses the base class members as arguments). What can I do? Is there anyway to have A&B share one method, and B&C share another method?

Thanks!

cgao
  • 155
  • 4
  • 15
  • Also check [`virtual` inheritance](http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class). – πάντα ῥεῖ Oct 06 '14 at 23:02
  • Thanks. I just got to know c++ supports multiple inheritance. I think multiple inheritance is the best way to solve this problem.. – cgao Oct 06 '14 at 23:35

1 Answers1

0

The following code will accomplish this.

class Base
{
    public:
        virtual double method1() { /* code shared by A and B */ }
        virtual double method2() { /* code shared by B and C */ }
};

class A : public Base
{
    public:
        virtual double method2() { /* code specific to A */ }
};

class B : public Base
{
};

class C : public Base
{
    public:
        virtual double method1() { /* code specific to C */ }
};

However, it is noteworthy that once these modifications have been made, B is identical to Base. It may be worth reassessing your inheritance hierarchy to determine whether A and C should inherit from B, as in the following:

class B
{
    public:
        virtual double method1() { /* code shared by A and B */ }
        virtual double method2() { /* code shared by B and C */ }
};

class A : public B
{
    public:
        virtual double method2() { /* code specific to A */ }
};

class C : public B
{
    public:
        virtual double method1() { /* code specific to C */ }
};
Brett Wolfington
  • 6,587
  • 4
  • 32
  • 51
  • Thanks! What if i have Class D and class C share the same method1(), do i need to specify method1 for C and D separately? – cgao Oct 06 '14 at 23:11
  • 1
    You could make `D` a subclass of `C`. `D::method1()` would inherit from `C`, and both `C::method2()` and `D::method2()` would inherit from `B` (or `Base`, in the first example). At that level of complexity I would start reassessing my inheritance hierarchy, but that is a subjective personal preference. Still, perhaps your problem could be better addressed by composition rather than inheritance. – Brett Wolfington Oct 06 '14 at 23:15