why we need interface or pure virtual function in c++

后端 未结 3 2043
小蘑菇
小蘑菇 2021-01-15 06:09

why we need interface ( pure virtual function or abstract class) in c++? Instead of having abstract class, Can we have a base class with virtual function defined in it, and

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-15 06:24

    An interface give you the ability to specify a set of behaviors that all classes that implement the interface will share in common. Consequently, we can define variables and collections (such as arrays) that don't have to know in advance what kind of specific object they will hold, only that they'll hold objects that implement the interface.

    Here

    As others have said, an interface is a contractual obligation to implement certain methods, properties and events [...] That's a sufficiently awesome benefit to justify the feature.

    and here

    (please refer to these very good explanations)

提交回复
热议问题