First: why should we avoid multiple inheritance in C ++? I have never seen a fairly large application that has not used it extensively. Inheriting from multiple interfaces is a good example of where it is used.
Note that the Java interface does not work, as soon as you want to use contract programming, you are stuck using abstract classes and they do not allow multiple inheritance. However, in C ++, this is easy:
class One : boost::noncopyable { virtual void doFunctionOne( int i ) = 0; public: virtual ~One() {} void functionOne( int i ) {
Alternatively, you can have a composite interface:
class OneAndTwo : public One, public Two { }; class ImplementsOneAndTwo : public OneAndTwo { virtual void doFunctionOne( int i ); virtual double doFunctionTwo(); public: };
and inherits from him what ever makes the most sense.
This is a more or less standard idiom; in cases where it is possible, any preconditions or post-conditions in the interface (usually call inversion), virtual functions may be publicly available, but in general they will be closed, so that you can enforce the pre- and post-conditions.
Finally, note that in many cases (especially if the class represents a value), you simply implement it directly, without an interface. Unlike Java, you do not need a separate interface for implementing in another file from a class a definition of how C ++ works by default (with a class definition in the header, but the implementation code in the source file).
source share