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?
Asked
Active
Viewed 115 times
-2

Striezel
- 3,693
- 7
- 23
- 37

Miguel Mano
- 103
- 1
- 12
-
1Possible 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 Answers
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,693
- 7
- 23
- 37
-
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
- 90,663
- 31
- 146
- 203