1

In a C++ programming course I have learned about the difference between definition and declaration. A function without a function body is a declaration and a function with a function body is a definition. Later I have learned about pure virtual functions. A pure virtual function doesn't have a function body but it is "initialized" with = 0;. Does that mean that a pure virtual function is a definition or is it considered as declaration only?

Edit: I don't think this is a duplicate of pure virtual function with implementation because the question "Does that mean that a pure virtual function is a definition or is it considered as declaration only?" is not answered.

stonar96
  • 1,359
  • 2
  • 11
  • 39
  • 1
    @Yunnosch A "pure virtual function" is definitely a thing in C++ - there is no such thing as an "abstract function". There is such a thing as an "abstract class". –  Mar 06 '19 at 20:11
  • Possible duplicate of [pure virtual function with implementation](https://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation) – UnholySheep Mar 06 '19 at 20:13
  • @Neil I researched a little. You seem to be right about "pure virtual functions" being a clearly understood term. Thanks. However, my "abstract functions" can also be found - admittedly less often. Still, thanks for correcting me. – Yunnosch Mar 06 '19 at 20:13
  • @UnholySheep did you even read the question? – stonar96 Mar 06 '19 at 20:14
  • @stonar96 The duplicate indicates that you can provide a definition for an pure virtual function, which implies that it would otherwise not have one. It's not a duplicate but it is related. – François Andrieux Mar 06 '19 at 20:15
  • 3
    "A pure virtual function doesn't have a function body" but it can. – juanchopanza Mar 06 '19 at 20:16
  • 2
    @Yunnosch -- the C++ standard defines and uses the terms "pure virtual function" and "abstract class". It does not use the term "abstract function". – Pete Becker Mar 06 '19 at 20:41
  • Do you need an answer with just an explanation, or an answer with a definitive quote from the std? – curiousguy Mar 10 '19 at 10:39
  • @curiousguy I don't need a quote but if it's possible it would be nice of course. – stonar96 Mar 11 '19 at 11:21

1 Answers1

-1

A vitual function is function which is an inheritable and overridable function which will support dynamic dispatch. Ideally this is what we call runtime polymorphism. In a nutshell, the virtual function provides a target function to be executed, which we have no idea at the compile time. Suppose you extend a class having a virtual function, you get the ability to use polymorphism to execute the function in the derrived class, using a base class pointer.

More read on https://www.geeksforgeeks.org/virtual-functions-and-runtime-polymorphism-in-c-set-1-introduction/

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract.

A point to note is, if you have a pure virtual function in a class, you cannot instantiate it as an object.

look at the following example

class Base {
public:
    virtual string getType() = 0;
}

class Derived : public base {
public:
    string getType();
}

string
derived::getType() {
    //do necessary stuff <------------------ (1)
}

Base* myBase = new Derived(); // OK
myBase->getType() //this will call the logic at (1).That's in the derived class but not base

Base* myBase2 = new Base();//Error

And for your question, pure virtual function is a declaration. And does not need a definition.

*EDIT You can provide a default implementation but you have to override in the derived classes of this base class with pure virtual function *

Destructor
  • 523
  • 1
  • 3
  • 13
  • 5
    Maybe I missed it, but I don't see where this answers whether or not a pure virtual function declaration is also a definition for that function or not. – François Andrieux Mar 06 '19 at 20:18
  • Let me add that as well :) THanks – Destructor Mar 06 '19 at 20:19
  • 5
    "*And does not need a definition*" - it does not NEED a definition in the declaring class, but it CAN HAVE a definition, if the class wants to provide a default implementation. A derived class MUST override a pure virtual function, but it can call the base's implementation if one is provided. – Remy Lebeau Mar 06 '19 at 20:26
  • @RemyLebeau What is the syntax for implementing a pure virtual function then? – tuket Mar 06 '19 at 20:30
  • 1
    @tuket The same as with any other class method, the ONLY difference is the added `= 0` on the declaration. – Remy Lebeau Mar 06 '19 at 20:32
  • Indeed, it seems to work. You need to define the function out of the class declaration though. `virtual f() = 0 {/*stuff*/}` or `virtual f() {/*stuff*/} = 0` are invalid. – tuket Mar 06 '19 at 20:52