Interface vs class with pure virtual methods [duplicate] - c++

This question already has answers here:
How do you declare an interface in C++?
(17 answers)
Closed 8 years ago.
I wanted to write shell extensions for windows in plain C++, but then I got confused by the keyword interface. In many articles I read that I can create interfaces in C++ by writing classes containing only virtual methods without any code. For example:
class IIsThisAnInterface_QuestionMark {
virtual MyMethod (
int firstParameter,
double secondParameter) = 0;
virtual AnotherMethod (
wchar_t *firstParameter) = 0;
}
But the author of this article defined interfaces by using the interface keyword. So my question is: How to correctly define interfaces in C++? (Becuase I grew up in C#'s world, I know interfaces as constructs specifying methods for classes that are implementing these interfaces.)

C++ doesn't strictly provide interfaces in the way that some languages do. The C++ mechanism is to provide a class with one or more pure virtual methods that declare the desired interface. Strictly speaking such a class is just an abstract class, but one could consider calling it an interface that child classes would then implement.

Related

How to extend C++ classes from Terralang?

Is there a way to embed terralang into a C++ application, such that C++ classes can be extended from terra, including overloading virtual functions?
BONUS: Can this be done (semi?)automatically for a (large)set of classes, without needing to write "wrapper code" for each class and each virtual method?

Difference between interface and polymorphism [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I was reading an online excerpt from a C++ book on polymorphism and interfaces.
The book made a distinction between polymorphism and interfaces, and specified how to implement them in C++. However, I was always under the idea that interfaces in C++ (implemented using a base class with pure virtual functions) were nothing more than an application of polymorphism.
I would like to know the clear distinction between polymorphism and interfaces because the excerpt confused me.
Polymorphism is the abstract concept of dealing with multiple types in a uniform manner, and interfaces are a way to implement that concept. Code that interacts with an interface can interact with any type that provides that interface.
Note that C++ has (at least) two forms of polymorphism: dynamic (i.e. run-time) polymorphism via interfaces formally defined by virtual functions, and static (i.e. compile-time) polymorphism via interfaces informally defined by the use of template parameters.
Typical C++ interfaces make use of virtual functions and polymorphism to provide the actual implementation. But polymorphism covers many other things, which is "not interfaces".
An interface is a base-class that can be used to a class that is passed back to something that accepts that interface. In some cases, a class may provide more than one interface:
class MyClass: public InterfaceGUI, InterfaceAudio
{
....
};
here, MyClass provides a class that interfaces both with the GUI and the Audio interfaces. This is a one case of multiple inheritance.
On the other hand:
class Animal
{
int numLegs;
public:
Animal(int nLegs): numLegs(nLegs) {}
};
class Dog : public Animal
{
Dog() : Animal(4) { }
};
here Animal is not a pure interface class, since it implements some functionality, and this is generally not a good design for an interface.
What i understood from the concept of polymorphism and Interface is as follows:
Polymorphism :
Polymorphism is nothing but having more than one form(As per all books).
But As per the Object orientation when you relate it with the real life You will get to know that Polymorphism is nothing but having more than one form with same name or some other Quality but have one's own Patent quality which no one have.
As per the programming:
A function with same name But different arguments (Compile time Polymorphism) and a Virtual function used for Runtime Polymorphism As explained here
Check this Example
Interface
An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.
The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.
For this purpose we use Pure Virtual function
For Reference you can go through this Link
And for example you can use explanation by Mats Petersson
Hope this will help you to understand the basic senario.

Can we declare an interface class by making all the methods abstract using the keyword ‘abstract’ in a class in C++? [duplicate]

This question already has answers here:
How do you declare an interface in C++?
(17 answers)
Closed 9 years ago.
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++.
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.
In C++, an interface is defined as follows:
class Interface {
public:
virtual ~Interface();
virtual void aMethod() = 0;
};
Note the virtual destructor.

How is polymorphism implemented at low level in a language like c++ ? [duplicate]

This question already has answers here:
Alternative virtual function calls implementations?
(11 answers)
Closed 9 years ago.
I read in a book that polymorphism is implemented in c++ by three levels of pointers(using vtable) but are there other methods of implementing it in c++ .
Virtual functions can also be implemented directly with function pointers like this.
struct A
{
void (*foo)(A *thiz);
void (*goo)(A *thiz, int x);
};
But obviously this is less efficient than the normal implementations. And actually C++ implementations may differ a little when dealing with multiply inheritance and virtual base classes.

What does `= 0` mean in the decalartion of a pure virtual function? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
C++ Virtual/Pure Virtual Explained
What's the difference between virtual function instantiations in c++
Why pure virtual function is initialized by 0?
This is a method in some class declaration that someone gave me. And I don't know what '..=0' means. What is it?
virtual void Print() const = 0;
The = 0 makes the function pure virtual, rendering the class an abstract class.
An abstract class basically is a kind of interface, which derived classes need to implement in order to be instantiable. However, there's much more to this, and it is some of the very basics of object-oriented programming in C++. If you don't know these, you need to go back to the textbook and read up. There's no way you can advance without understanding them.
That said, see this related question for some explanations of what virtual and pure virtual functions are. And as always, the C++ FAQ is an excellent resource for such questions.
It means that the virtual function is pure, meaning that you cannot call it as such: the function doesn't have any code to it, hence the = 0. Only by deriving the class and overriding the function you can call it. The class with pure virtual functions cannot be instantiated so they are called abstract classes, interfaces in some languages.
Basically, it means the function has no code. This means that you cannot use instances of this class. Rather, it can only be a base class.