What is Pure Virtual Function? Why and when it is used ?

Answer Posted / apple dugar

A virtual function that is initialized to zero (0) is
referred to as pure virtual function.It has no body and
hence also known as do-nothing or the dummy function.
Example: virtual void show()=0;

A class containing one or more pure virtual functions is
called an Abstract class, which means an instance of such
class can't be created (but pointer to that class can be
created).We should use pure virtual function if we do not
want to instantiate a class but make it act as a base class
for all the classes that derive from it.An important thing
to note about pure virtual functions is that these
functions must be overridden in all the derived classes
otherwise the compile would flag out an error.

Sample program:

class alpha
{
public:virtual void show()=0; //pure virtual function
};
class beta:public alpha
{
public:void show() //overriding
{
cout<<"OOP in C++";
}
};
void main()
{
alpha *p;
beta b;
p=&b;
p->show();
}

Output: OOP in C++

Is This Answer Correct ?    118 Yes 13 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between method overloading and method overriding in c++?

558


Is it possible to write a c++ template to check for a function's existence?

576


What is :: operator in c++?

580


What parameter does the constructor to an ofstream object take?

609


What is the difference between a template and a macro?

582






What is the best c c++ compiler for windows?

560


Can the operator == be overloaded for comparing two arrays consisting of characters by using string comparison?

562


What is a .h file c++?

528


Explain the scope of resolution operator.

629


What is size of string in c++?

551


What is purpose of abstract class?

584


Explain binary search.

576


How do I run c++?

572


What are its advantages and disadvantages of multiple inheritances (virtual inheritance)?

601


When do you call copy constructors?

647