Java makes a clear distinction between class and interface. (I believe C# does a
ID: 652455 • Letter: J
Question
Java makes a clear distinction between class and interface. (I believe C# does also, but I have no experience with it). When writing C++ however there is no language enforced distinction between class and interface.
Consequently I've always viewed interface as a workaround for the lack of multiple inheritance in Java. Making such a distinction feels arbitrary and meaningless in C++.
I've always tended to go with the "write things in the most obvious way" approach, so if in C++ I've got what might be called an interface in Java, e.g.:
class Foo {
public:
virtual void doStuff() = 0;
~Foo() = 0;
};
and I then decided that most implementers of Foo wanted to share some common functionality I would probably write:
class Foo {
public:
virtual void doStuff() = 0;
~Foo() {}
protected:
// If it needs this to do its thing:
int internalHelperThing(int);
// Or if it doesn't need the this pointer:
static int someOtherHelper(int);
};
Which then makes this not an interface in the Java sense anymore.
Instead C++ has two important concepts, related to the same underlying inheritance problem:
virtual inhertiance
Classes with no member variables can occupy no extra space when used as a base
"Base class subobjects may have zero size"
Reference
Of those I try to avoid #1 wherever possible - it's rare to encounter a scenario where that genuinely is the "cleanest" design. #2 is however a subtle, but important difference between my understanding of the term "interface" and the C++ language features. As a result of this I currently (almost) never refer to things as "interfaces" in C++ and talk in terms of base classes and their sizes. I would say that in the context of C++ "interface" is a misnomer.
It has come to my attention though that not many people make such a distinction.
Do I stand to lose anything by allowing (e.g. protected) non-virtual functions to exist within an "interface" in C++? (My feeling is the exactly the opposite - a more natural location for shared code)
Is the term "interface" meaningful in C++ - does it imply only pure virtual or would it be fair to call C++ classes with no member variables an interface still?
Explanation / Answer
In C++, the term "interface" has not just one widely accepted definition - so whenever you are going to use it, you should say what you mean exactly - a virtual base class with or without default implementations, a header file, the public members of an arbitrary class and so on.
Concerning your example: in Java (and similar in C#), your code probably would imply a separation of concerns:
interface IFoo {/* ... */} // here is your interface
class FooBase implements IFoo
{
// make default implementations for interface methods
}
class Foo extends FooBase
{
}
In C++, you can do this but you are not forced to. And if you like to call a class an interface if it has no member variables, but contains default implementations for some methods, just do so, but make sure everyone you are talking to knows what you mean.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.