Explain Pure Virtual Functions

Estudies4you
Q20. What are pure virtual functions? Explain.
Answer:
Pure Virtual Function
Pure virtual function can be defined as the functions that are declared inside a base class without any relative definition.
Hence, each derived class must re-declare it as a pure virtual function or redefine it.
They are also called as “do-nothing” functions as their definitions are empty and they are of the form,
virtual void display() = 0;
The display( ) in the above declaration is a pure virtual function with no definition relative to the base class. The assigned operator does not specify that zero is assigned to this function, instead it is used to tell the compiler that the declared function is a pure virtual function and that it will not have a definition.
Moreover, a class consisting of pure virtual function is called abstract class or pure abstract class. This abstract class cannot declare any object of its own. Doing so may lead to an error from the compiler “cannot create instance of an abstract class”. Besides this, even if the base class declares a pure virtual function, this function cannot perform any operation and cannot be used to declare the objects as well.
virtual type function_name (arguments_list) = 0;
Here, virtual is the keyword, type indicates the type of function, function_name indicates the name of the function, arguments_list indicates the list of parameters passed to the function. Pure virtual function is initialized to zero because they are do nothing functions and does not have a body.

Example
virtual void xyz = 0;

Program
#include<conio.h>
#include<iostream.h>
class SIAgroup
{
public:
virtual void demo() = 0;
};
class B-tech: public SIAgroup
{
public:
void demo()
{
cout<<“B.tech AIO published by SIAgroup”;
}
};
class B.com: public SIAgroup
{
public
void demo()
{
cout<<“B.com materials published by, SIAgroup”;
}
};
class Polytechnic: public SIAgroup
{
public
void demo():
{
cout<<“Polytechnic materials published by SIAgroup”;
}
};
void main()
{
SlAgroup* array[3];
B.tech sl;
B.com s2;
Polytechnic s3;
array[0] = &s1;
array[1] = &s2;
array[2] = &s3;
array[0] → demo();
array[1] → demo();
array[2] → demo();
}

Output
Explain Pure Virtual Functions,What are pure virtual functions,Pure Virtual Function in c++,use of Pure Virtual Functions in c++,examples of Pure Virtual Functions in c++,programs for Pure Virtual Functions in c++,estudies4you,c++ lecture notes jntuh,c++ notes in jntuh,c++ study material jntuh,oops using c++ lecture notes,

To Top