Difference between abstract class and interface in C++

Hello,
I encountered the following advice in "The C++ programming language" 3-th special edition, p325 12.6 .

"Use abstract classes to focus on the provision of clean interfaces"
"Use abstract classes to minimize interfaces"
"Use abstract classes to keep implementation details out of interfaces"

Can someone explain me what's the difference between an abstract class and an interface ?
I know that an abstract class is called a class which have at least on pure virtual function.

Regards
I believe it is that an abstract class can have function bodies, meaning you can *know* exactly how a function is defined, whereas if you use an interface, it just says "have a function named this", which means someone could have the "add()" do nothing, or delete a variable, or whatever.
Yeah, the intersection of the terms is fairly, well, indefinite. Like the word "window". In computers "window" can mean a good ten or fifteen different specific things that I know of. (I'm not actually counting...)


A B S T R A C T

An abstract thing is something that doesn't have any concrete value or application.

http://dictionary.reference.com/browse/abstract (clauses 1, 2, and 3) Yes, there are other dictionaries... but the Brits had the most straight-forward reading, methinks.

This is how the word abstract is meant in the C++ literature: a class (that is, a type) that cannot be instantiated into specific objects (that is, it cannot be made into actual data).


I N T E R F A C E

An interface is itself an abstract concept, but for now let us put that beside us. In simplest terms, it is the commonality between two (or more) things.

In computers, that translates to: the specification of how two or more things communicate.

http://dictionary.reference.com/browse/interface
http://www.merriam-webster.com/dictionary/interface
http://en.wikipedia.org/wiki/Interface_(computer_science)
Again, the Brits have that nice usage note about the history of the word and its relationship to the computer sciences.

In software, an interface is typically taken as a special type of object class that lists members that other objects must have to conform to a specific way of communicating, or use. Some languages (like Java and Delphi) specify it as a special type of declaration that modifies a class definition. C++ doesn't. Part of the reason is that C++ allows for multiple-inheritance, where Delphi and Java don't...


Same Difference

In practice, both things translate to much the same idea: a class declaration that cannot be instantiated unless specific methods, fields, etc. are provided with definition in a conforming (Java and Delphi) or derived (C++) class.

That is, in C++, an interface is implemented simply as inheriting from one or more abstract classes that specify the required methods, etc.

Hope this helps.
Last edited on
Hm, I see.
Anyway, I know that in JAVA the interface have only method declarations. The abstract class can have method declarations AND method definitions... both can't be instantiated. Both have keywords.
Here in C++, there is no interface keyword.
And if there is only one pure virtual function in a class, it's abstract, but we can mix definitions and declarations, as far as I know.

But what Stroustrup is meant to say with this, it's confusing.
Also note that "interface" may refer to a function used to access one of the private members of a class, and this applies to C++, Java, and C#.
Last edited on
"Use abstract classes to focus on the provision of clean interfaces"


The primary purpose of the base abstract class should be to define a common interface to all it's derived classes. If you have a lot of derived classes that are defining their own interfaces, then that's a strong indication that there may be something wrong with your design. The ideal goal of your class hierarchy is that you can fully interact with ALL your derived classes through JUST a base class pointer. This isn't always possible, and that's ok, but when designing your application, that is what you should strive for.

"Use abstract classes to minimize interfaces"


As mentioned above, create one common interface in the base class that all the derived classes use. Each derived class should not be creating it's own specific interface.

"Use abstract classes to keep implementation details out of interfaces"

As you said, C++ gives you the flexibility to mix interface and implementation. There's nothing explicitly wrong with that, because in some cases that's desirable, but in most it isn't. But that's just the nature of C++. It gives you all the flexibility in the world essentially, but you have to be careful with it. In some cases, it may make sense to provide a default implementation as part of the interface (one that will typically still be declared virtual and can be overridden by the derived classes).
Topic archived. No new replies allowed.