-2

For instance, the class Plant has a virtual void info() method. The class flower derives from Plant.
Is Plant obligated to have its own implementation of the method?

Striezel
  • 3,497
  • 7
  • 20
  • 35
Miguel Mano
  • 83
  • 10
  • 1
    Possible duplicate of [pure virtual function with implementation](http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation) – gsamaras Oct 15 '16 at 13:11
  • Are you asking whether your compiler is standards compliant in accepting your code? – juanchopanza Oct 15 '16 at 13:11

2 Answers2

0

No.

Base classes do not need their own implementation of a virtual method that is implemented by a dervided class. However, they can have an implementation.

To skip the implementation in the base class, just make it pure virtual, e.g.

virtual void info() = 0;

In that case any derived classes - or to be more specific: any derived class that you want to have an instance of - needs to implement the virtual method.

Striezel
  • 3,497
  • 7
  • 20
  • 35
  • But the question is the other way around, I'm afraid. I was talking about declaring the virtual function on the base class and not on the derived one. – Miguel Mano Oct 15 '16 at 14:29
0

If the function is pure virtual, that is declared virtual void info() = 0;, then No. Otherwise Yes.

Bo Persson
  • 88,437
  • 31
  • 141
  • 199