Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are abstract methods and pure virtual functions the same thing?

As far as I know, both abstract methods and pure virtual functions do NOT provide any functionality ... So can we say they're both the same thing ?

Also, suppose a class (not necessarily declared as abstract) contains a number of implemented methods (not abstract or virtual), but contains a pure virtual function. Is this class then abstract ?

like image 560
Ahmad Avatar asked Jul 04 '11 18:07

Ahmad


People also ask

Are virtual functions the same as abstract?

Abstract methods do not provide an implementation and force the derived classes to override the method. Virtual methods have an implementation and provide the derived classes with the option of overriding it.

What is the difference between pure virtual function and abstract class?

A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. An abstract class is a class in C++ which have at least one pure virtual function.

What is difference between virtual method and abstract method?

The C# programming language provides support for both virtual and abstract methods, each of which has distinct advantages. You use virtual methods to implement late binding, whereas abstract methods enable you to force the subclasses of the type to have the method explicitly overridden.

Is virtual same as abstract in Java?

However, a virtual class does implement functionality itself and can be instantiated and used directly. Conversely, abstract classes must be subclassed and have methods overridden to provide functionality. abstract classes cannot be instantiated or used themselves; they are more fully "abstract base classes".


2 Answers

Yes, they are the same thing. In C++, an abstract method is just another way of describing the characteristics of a pure virtual function. Both just mean a method with no implementation provided that needs to be implemented in a sub-class before the class can actually be instantiated.

The situation with pure virtual functions and abstract classes in C++ is similar as they essentially mean exactly the same thing. Any abstract class must have at least 1 pure virtual function or else it could be instantiated and wouldn't be abstract. Likewise, any class with at least 1 pure virtual function must be abstract because it needs to be extended so that method can actually be implemented.

Therefore, a class is abstract if and only if it contains at least 1 pure virtual function/abstract method.

Later on, languages like Java and C# made things like this more explicit, allowing a special keyword to define a class abstract rather than the presence of a pure-virtual function. C++ lets you do the same things as these languages, but they're just a little more explicit about it. :D

like image 129
Gordon Gustafson Avatar answered Sep 25 '22 08:09

Gordon Gustafson


You don't explicitly declare classes or methods as abstract in C++. The presence of pure virtual methods is what makes them abstract.

like image 22
Jordão Avatar answered Sep 23 '22 08:09

Jordão