Search This Blog

Saturday, November 14, 2009

C++, Multiple Inheritance Vs C# Interface

Multiple Inheritance:
  • Suitable for large framework design
  • Encourage subclass to specialize functionality (is-a).
  • Each subclass is designed to be further extended
  • Need to worry about ambiguous implementation on the inheritance chain
  • Public, protected, private inheritance are very expressive (even though not very practical, but when you do need the expressiveness, you need the language like C++)
  • Polymorphic behavior is the foundation of large framework design (interface is stable in base class, but behavior can be extend in the derived class. The Open-Closed principle)

( Note that in C++, private inheritance is treated as implementation inheritance. You can inherit a base class, and all the public and protected base class methods become private in the subclass. So you can reuse the base class implementation, without worrying about its interfaces being used by others.) p.981 C++ primer for private inheritance guideline.

(Note that in C++, you can of course do object-composition without using inheritance. just put the objects in the private section as members. You can compose by value -- enjoy the auto memory management. You don't have to write copy constructor, assignment operator, destructor for your class. Or you can compose by a pointer or reference -- enjoy the full control of your members and possible polymorphic behavior of the new added objects. )

Multiple Interface:
  • Suitable for typical application
  • Encourage build with "composition" (has-a)
  • Worry less about being used as serving as a base class in the future
  • Worry less about ambiguous implementation because all interface are empty
  • Technically like a subset of inheritance: inheritance interface only (all members are pure virtual in C++)
  • Can still have polymorphic behavior

No comments: