简体   繁体   中英

Do derived classes need their own version of a virtual function declared on base?

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?

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, eg

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM