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

Answers were Sorted based on User's Feedback



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

Answer / mahfuzur rahman

Virtual function vs pure virtual function :

Virtual function :-

1. Virtual function have a function body.
2. Overloaded can be done by the virtual funciton.
(Optional)
3. It is define as : virtual int myfunction();


Pure virtual function :-

1. Pure virtual function have no function body.
2. Overloading is must in pure virtual funciton. (Must)
3. It is define as : virtual int myfunction() = 0;
4. A class is "abstract class" when it has at least one
pure virtual function.
5. You cann't create instance of "abstract class", rather
you have to inherit the "abstract class" and overload all
pure virtual function.

Like :- CControlBar class is an "abstract class".

Is This Answer Correct ?    144 Yes 24 No

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

Answer / 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

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

Answer / ognas(shradha-asutosh)

in inheritance if the base class contains a virtual
function equating to zero, it is known also as do-nothing
function & that base class is called as abstract base class
as there are no instances or objects can be created by this
base class. And this pure virtual can be filled with the
codes in successiv derived classes accordin to the user
requirements.
The syntax of the pure virtual function is....

class class_name
{
visibility mode:\\should be protected:
virtual return_type function_name()=0;
}
new class_name : inheritance_type old class_name
{
........ //class body
........
}

Is This Answer Correct ?    69 Yes 19 No

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

Answer / guest

The abstract class whose pure virtual method has to be
implemented by all the classes which derive on these.
Otherwise it would result in a compilation error.
This construct should be used when one wants to ensure that
all the derived classes implement the method defined as
pure virtual in base class.

Is This Answer Correct ?    77 Yes 44 No

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

Answer / prajesh gupta (ferozepur, punj

Pure Virtual Functions:
1. "Do nothing Function"
virtual void display() = 0;
2. declared in base class.
3. class containing a pure virtual function does not
declare an object of its own. (That class is known as
ABSTRACT CLASS)

Advantages Of Using:

1. To inherit the properties of the derived class.
2. To create a base pointer required for runtime
poymorphism.
3. To avoid overwriting of member function.

Is This Answer Correct ?    37 Yes 11 No

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

Answer / rajni

a virtual when equated to zero then it is a pure virtual
function'

Is This Answer Correct ?    25 Yes 6 No

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

Answer / harry

pure virtual function declared in the base class.
pure virtual function having intializer=0;
pure virtual function also know as do nothing function &
dummy function.
class contain atleast one pure virtual function.
object cannote be create of that class in which pure
virtual function are declared and that class are know as
abstract class.

Is This Answer Correct ?    15 Yes 4 No

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

Answer / shakti singh

A virtual function in a base class which is equated to 0 is called a pure virtual function.The class then is called a Abstract Base Class or in general ABC.No object of such class can be instantiated.ABC in general acts as an interface and implement the general flow of algorithm.A pure virtual function must be overloaded in the derived class otherwise the compiler will throw an error.
A pure virtual function do nothing and it is not concerned with the implementation detail.

Is This Answer Correct ?    13 Yes 5 No

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

Answer / jun

Floor 6, you have mentioned the most salient part of pure
virtual function(pointer access). Thanks

Is This Answer Correct ?    14 Yes 10 No

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

Answer / talha bilal

Pure Virtual Function

class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};
class Derived:public Base
{
public:
void show()
{
cout << "Implementation of Virtual Function in Derived class";
}
};

int main()
{
Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}

Virtual Function

class Base
{
public:
virtual void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
private:
void show()
{
cout << "Derived Class";
}
};

int main()
{
Base *b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Occurs
}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C++ General Interview Questions

What is pointer -to-members in C++? Give their syntax?

0 Answers   Honeywell, Zomato,


program to print this triangle * * * * * *

12 Answers   Infosys,


write the prime no program in c++?

16 Answers  


Write a Program for find and replace a character in a string.

0 Answers  


Please post the model question paper of hal?

2 Answers  






Write a program to add three numbers in C++ utilizing classes.

0 Answers   TCS,


When is a template better solution than a base class??

2 Answers   emc2,


What is binary object model?

0 Answers  


How can you tell what shell you are running on unix system?

0 Answers  


What is #include c++?

0 Answers  


What is endl c++?

0 Answers  


What is the use of this pointer in c++?

0 Answers  


Categories