Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Virtual function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Abstract classes and pure virtual functions == 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 type|abstract]]. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly. A [[Subclass (computer science)|subclass]] of an [[abstract class]] can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class. Pure virtual methods typically have a declaration ([[Type signature#Method signature|signature]]) and no definition ([[Method implementation|implementation]]). As an example, an abstract base class <code>MathSymbol</code> may provide a pure virtual function <code>doOperation()</code>, and derived classes <code>Plus</code> and <code>Minus</code> implement <code>doOperation()</code> to provide concrete implementations. Implementing <code>doOperation()</code> would not make sense in the <code>MathSymbol</code> class, as <code>MathSymbol</code> is an abstract concept whose behaviour is defined solely for each given kind (subclass) of <code>MathSymbol</code>. Similarly, a given subclass of <code>MathSymbol</code> would not be complete without an implementation of <code>doOperation()</code>. Although pure virtual methods typically have no implementation in the class that declares them, pure virtual methods in some languages (e.g. C++ and Python) are permitted to contain an implementation in their declaring class, providing fallback or default behaviour that a derived class can delegate to, if appropriate.<ref>[https://en.cppreference.com/w/cpp/language/destructor#Pure_virtual_destructors Pure virtual destructors - cppreference.com]</ref><ref>[https://docs.python.org/3/library/abc.html "abc β Abstract Base Classes: @abc.abstractmethod"]</ref> Pure virtual functions can also be used where the method declarations are being used to define an [[interface (Java)|interface]] - similar to what the interface keyword in Java explicitly specifies. In such a use, derived classes will supply all implementations. In such a [[design pattern]], the abstract class which serves as an interface will contain ''only'' pure virtual functions, but no data members or ordinary methods. In C++, using such purely abstract classes as interfaces works because C++ supports [[multiple inheritance]]. However, because many OOP languages do not support multiple inheritance, they often provide a separate interface mechanism. An example is the [[Java (programming language)|Java programming language]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)