I want to know about the way of defining interface class in C++ by this approach. Is it possible? I don't want to know about interface creation but want to know about the abstract keyword use in C++.
Asked
Active
Viewed 372 times
-6
-
5There is no keyword `abstract` in C++. And if you want an abstract class, that is a class that cannot be initialized, just use a protected constructor. – Bartek Banachewicz Dec 11 '13 at 14:41
-
1There are pure virtual functions though: `= 0`, `abstract` is C# – yizzlez Dec 11 '13 at 14:42
-
@BartekBanachewicz: No, an abstract class is one with pure virtual functions that must be overridden, not one with a protected constructor. – Mike Seymour Dec 11 '13 at 15:52
-
@MikeSeymour MSDN : `They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented.`, Wikipedia: `an abstract type is a type in a nominative type system which cannot be instantiated directly. `. Whether you implement some functionality or just derive from it doesn't really matter. – Bartek Banachewicz Dec 11 '13 at 15:54
-
1@BartekBanachewicz: C++11: "A class is abstract if it has at least one pure virtual function." A class with a protected constructor (and no pure virtual functions) *can* be instantiated directly, by its members, friends, and subclasses, so isn't abstract even by those rather inaccurate descriptions. – Mike Seymour Dec 11 '13 at 15:58
-
@MikeSeymour Heh, I smiled too much on "can be instantiated by its subclass" to disagree now. You're right. – Bartek Banachewicz Dec 11 '13 at 16:00
2 Answers
2
In C++, an abstract class is any class that has at least 1 pure virtual function.
C++ does not have direct support for interfaces, but you can make one by making all of the functions public, virtual, and abstract, and have no data members in the class.

Zac Howland
- 15,777
- 1
- 26
- 42
2
In C++, an interface is defined as follows:
class Interface {
public:
virtual ~Interface();
virtual void aMethod() = 0;
};
Note the virtual destructor.

blackbird
- 957
- 12
- 18