Answer the question
In order to leave comments, you need to log in
What's the difference between an interface in C# and pure virtual functions in C++ classes?
Recently switched from plus to sharp. I want to understand the difference between interface in C# and pure virtual classes in C++. Or is it basically the same thing?
Answer the question
In order to leave comments, you need to log in
" Purely virtual " are functions, not classes. A class that has at least one pure virtual function is called abstract . Sometimes " purely abstract " is a class that has only pure virtual functions + a virtual destructor. Such a class can indeed be used as an interface, and is designed to work like interfaces in other languages. The differences are minimal, if any.
Same thing, pure abstract classes in C++ are a way to get functionality similar to the concept of "interface" in those languages where it exists (C#, Java, etc.).
Thanks to multiple inheritance in C++, base classes can include both classes with real behavior (i.e., with implementations of some methods) and "interface" classes, i.e. classes with only pure-virtual member functions.
Alas, the Achilles heel of OOP is inheritance. You cannot inherit from more than one class. It doesn't matter if you inherit a real class or an abstract one. But with the help of interfaces, it is possible to bypass this limitation and still build the desired object with the necessary properties, and with a bunch of interfaces, and not just one.
An interface is a convention for working with an object.
What
pure virtual classes in C++I didn't understand.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question