Should interfaces and abstract Classes be separated at C++



  • As is well known, separate language tools for the announcement of interfaces in C+++, and their role can be in abstract classes. But from an ideological perspective, the interfaces and abstract classes are quite different. Interfaces describe a contract of how to interact with the class. Abstract classes are the roots of class hierarchy, used for polymorphism in a spirit

    AbstractSuperClass* a = new SubClass;
    

    and can just perform a general function.

    Do you need to separate these concepts and write interfaces like Java/C#? Is it possible to mix and use only abstract classes?



  • C#/Java has no multiple class inheritance, but there is a multiple internship. Therefore, in these languages, the separation of interfaces and abstract classes is a non-voluntary option.

    There is no restriction in C++, so there is no particular need to separate the concept of the interface and to separate it from the abstract class. Any abstract class may serve an interface, but it does not even have to have public virtual functions (except for the destructor if polymorphous disposal is required). Example of such an interface - Pattern https://en.m.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface ♪

    If the PLO wishes to be more consistent with the C#/Java language paradigm, the names starting from the letter may be given to interfaith classes Ior even add

    #define interface struct
    

    In order to distinguish the Interface Class in the declaration/definition. Such approaches are used in MS COM.

    But in general, this is not necessary, and the problem of the rumboid hierarchy can be solved through virtual succession.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2