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

Out of fgets() and gets() which function is safe to use?

649


How do I run c++?

582


Why is c++ still used?

615


What are punctuators in c++?

663


What is virtual destructor? What is its use?

586






How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?how can you find the nodes with repetetive data in a linked list?

659


Can we make any program in c++ without using any header file and what is the shortest program in c++.

625


Can union be self referenced?

589


Write a single instruction that will store an EVEN random integer between 54 and 212 inclusive in the variable myran. (NOTE only generate EVEN random numbers)

1496


What are the syntactic rules to be avoid ambiguity in multiple inheritance?

641


What do you mean by “this” pointer?

627


What is scope operator in c++?

576


Which software is best for c++ programming?

584


What is a syntax in c++?

617


Explain function overloading

595