What are pure virtual functions? How are they different from normal functions in c++? Explain with an example
Answers
Answered by
1
Abstract classes cannot be instantiated. Derived classes need to override/implement all inherited pure virtual functions. Ifthey do not, they too will become abstract. An interesting 'feature' of C++ is that a class can define apure virtual function that has an implementation.
mahalakshmi524:
can you explain with an example
Answered by
2
Virtual Function and Pure Virtual Function differs in declaration.
Virtual Function is declared with keyword 'virtual' at the start of declaration.
Ex : virtual return_type function_name(function arguments);
While Pure Virtual Function is declared as
Ex : virtual return_type function_name(function arguments) = 0;
NOTE : Sometime it has been written that pure virtual function has no function body. But this is not always true. Having a default definition of pure virtual function is not common but its possible.
Ex : virtual return_type function_name(function arguments) = 0; //Declaration
return_type class_name::function_name(function argument) //Definition
{
.
.
.
}
Virtual Function and Pure Virtual Function differs in use also.
Thanks
Similar questions