What is Pure Virtual Function? Why and when it is used ?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
Answer / rajni
a virtual when equated to zero then it is a pure virtual
function'
Is This Answer Correct ? | 25 Yes | 6 No |
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 |
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 |
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 |
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 |
Is there something that we can do in C and not in C++?
What are compilers in c++?
Explain the ISA and HASA class relationships. How would you implement each in a class design?
what is pulse code modulation?
What are the uses of pointers?
What are the various compound assignment operators in c++?
Why is it difficult to store linked list in an array?
How can we access protected and private members of a class?
What is the full form of ios?
What is private, public and protected inheritance?
Explain the term memory alignment?
Will the following program execute?