What is a pure virtual function?
A pure virtual function is a function that has the notation "= 0" in the declaration of that function. Why we would want a pure virtual function and what a pure virtual function looks like is explored in more detail below.Here is a simple example of what a pure virtual function in C++ would look like:
Example of a pure virtual function in C++
class ABC {
public:
virtual void pure_virtual() = 0; // a pure virtual function
// note that there is no function body
};
The pure specifier
|
|
The quick answer to that question is yes! A pure virtual function can have an implementation in C++ – which is something that even many veteran C++ developers do not know. So, using the SomeClass class from our example above, we can have the following code:
class SomeClass {
public:
virtual void pure_virtual() = 0; // a pure virtual function
// note that there is no function body
};
/*This is an implementation of the pure_virtual function
which is declared as a pure virtual function.
This is perfectly legal:
*/
void SomeClass::pure_virtual() {
cout<<"This is a test"<<endl;
}
For Read More http://www.programmerinterview.com/index.php/c-cplusplus/pure-virtual-function/
No comments:
Post a Comment