why we need interface ( pure virtual function or abstract class) in c++? Instead of having abstract class, Can we have a base class with virtual function defined in it, and override that virtual function in derived class. what would be the advantage and disadvantage with the above approach ( except we can create the object of the base class)?
-
Because polymorphism. – Ignacio Vazquez-Abrams Oct 06 '16 at 14:05
-
One major advantage is being able to use late binding which allows for runtime polymorphism – P. Hinker Oct 06 '16 at 14:05
-
are you asking for "why virtual" or "why should something be made pure virtual" ? – Hayt Oct 06 '16 at 14:06
-
An interface allows you to separate the the definition of how you interact with a class from the implementation of that class. That allows you to have different classes that can use the same interface, but it's a useful technique even if you only have one class that uses the interface. As for virtual functions, see http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c?rq=1 – Mark Ransom Oct 06 '16 at 14:09
-
"Except we can create the object of the base class." That is exactly the point. Abstract class prevents you from creating an object of the base class. Or is your question "Why would you ever want to prevent creation of the base class?" – Raymond Chen Oct 06 '16 at 14:11
-
Look up the diamond of death - interfaces are one way to resolve this – UKMonkey Oct 06 '16 at 14:13
-
the same functionality we have in case of virtual function in the base class ,rather than the pure virtual function in the base class. so what is the advantage of pure virtual function defined in the base class ( except not able to create the object of the base class)? – Sanjay Oct 06 '16 at 14:58
3 Answers
Pure virtual functions are for when there's no sensible way to implement the function in the base class. For example:
class Shape {
public:
virtual float area() const = 0;
};
You can write derived classes like Circle
and Rectangle
that implement area()
using the specific formulas for those kinds of shapes. But how would you implement area()
in Shape
itself, if it weren't pure virtual? How do you compute the area of a shape without even knowing what kind of shape it is?
If your function can be implemented (in a useful way) in the base class, then go ahead and implement it. Not all base classes need to be abstract. But some of them just inherently are abstract, like Shape
.

- 33,849
- 3
- 67
- 87
-
Thanks , But my question is that rather than the pure virtual function in the base class , can we have a virtual function ( in your code virtual float area() ) in the base class ? And what would be the advantage and disadvantage with the above approach ? – Sanjay Oct 06 '16 at 14:50
-
You *can* but then you have to *implement* it in the base class, and there's no good way to do that in this case. The best you could do is throw an exception or return a dummy value like zero or NaN, but that would just lead to errors at runtime. Better to prevent it from happening at compile time. – Wyzard Oct 06 '16 at 14:56
-
yes but we can leave the function definition with nothing, like a dummy function . and override the same function in the derived class with the proper definition ( like shape and circle , square example). – Sanjay Oct 06 '16 at 15:03
-
And in other words my question is : what would be the advantage and disadvantage of the pure virtual mechanism over the virtual concept in the base class ? – Sanjay Oct 06 '16 at 15:10
-
1The advantage is that you don't have to write a "dummy" function that should never actually be called, and you can't accidentally create an object on which that function *could* be called. You also can't forget to override the function in a derived class. The compiler prevents you from doing things that don't make sense to do. – Wyzard Oct 07 '16 at 00:40
Pure virtual functions is your way of telling the users of your class that they cannot use the class on its own, without inheriting from it.
Obviously, you can do what you describe, and the system is going to compile and work as expected. However, an pure virtual function is not a construct for the compiler; it is for humans who read your code. It is with this construct that you tell the readers of your code that they must inherit from your class, because the class is not designed to be instantiated on its own.
You use pure virtual functions in situations when there is no reasonable default implementation for a function. This tells people who implement your class that they must provide certain functionality, and the compiler helps them in detecting situations when they forgot to provide an implementation.
If, on the other hand, you provide a default implementation for a virtual function that should be implemented by a subclass, and then the users of your class library forget to provide an implementation, the problem would not be detected until run-time.

- 714,442
- 84
- 1,110
- 1,523
An interface give you the ability to specify a set of behaviors that all classes that implement the interface will share in common. Consequently, we can define variables and collections (such as arrays) that don't have to know in advance what kind of specific object they will hold, only that they'll hold objects that implement the interface.
As others have said, an interface is a contractual obligation to implement certain methods, properties and events [...] That's a sufficiently awesome benefit to justify the feature.
and here
(please refer to these very good explanations)