Pure Virtual Function in C
Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below.
- In c++ the member function that is defined in the base and that function is redefined in the derived class such function is called virtual function.
- In this function “virtual” keyword is used in the function declaration.
Virtual<func_type><func_name>()
{
//function defination;
}
- If the virtual function of the base class is not redefined in the derived classthen there is no effect at the time of compilation.
Pure Virtual Function:
It is also a member function where there is no function definition but only the function declaration in the base class. The function definition takes place in the derived class.
- It is also known as “do-nothing” because it has no function definition.
- The syntax is the same as the virtual function but “= 0”is included.
Virtual<func_type><func_name>()=0;
- In the declaration of the pure virtual function “0”is assigned to the function. Here=0 doesn’t mean that 0 is assigned for the function but 0 is just used to define the pure virtual function.
- If there is no definition for the function, that was indicated to the compiler using “=0” in the function declaration.
- When the pure virtual function of the base class is not defined in the derived class there will be an effect at the time of compilation.
- If atleast one or more pure virtual functions are contained by a class then that class is known as the abstract class.
- An object for the abstract class is not created directly but it’s okay to create a pointer or reference for the abstract class.
- The object is not created directly for the class that contains pure virtual function or abstract class.
- We can create this function in two ways:
- Virtual int display()=0;
- Virtual int display() { }
Pure virtual functions are used in the case of the following:
- No use of function in the base class. But the function must be implemented by the derived class.
As the virtual and pure virtual functions are different but there are some similarities exist between them they are:
- Both are Run-time polymorphism concepts.
- Throughout the program, the declaration of the function is in the base class.
- These can’t be global or static functions.
Example:
class Example
{
public:
virtual void display() = 0; // Pure Virtual Function
};
Program:
#include<iostream>
using namespace std;
class Javatpoint {
public:
virtual void display() = 0; // Pure Virtual Function
};
class Intern:public Javatpoint {
public:
void display() {
cout << "Technical content writer\n";
}};
int main() {
Javatpoint *j;
Intern i;
j = &i;
j->display();
}
Output:
Technical content writer
From the above example program, in the base class(Javatpoint)the display() function is declared but not defined in the base class. But in the derived class(Intern)the function is defined. In the main method, the pointer to the base or abstract class is created and the direct object to the derived class(Intern). The address of the derived class object is assigned to the base class reference object. So that the abstract class defines the same function as defined in the derived class.
Important points about the pure virtual function:
- If a class has a pure virtual class that is the abstract class.
Program:
#include<iostream>
using namespace std;
class Javatpoint {// abstract class
public:
virtual void display() = 0; // Pure Virtual Function
};
class Intern:public Javatpoint {
public:
void display() {
cout << "Technical content writer\n";
}};
int main() {
Javatpoint j;
Intern i;
j = &i;
j->display();
}
Output:
Error: cannot declare variable 'j' to be of abstract type 'Javatpoint'
- We create the object to the base class by objects of Derived class Or pointer reference of base class pointed towards the object of the derived class.
Program:
#include<iostream>
using namespace std;
class Javatpoint {// abstract class
public:
virtual void display() = 0; // Pure Virtual Function
};
class Intern:public Javatpoint {
public:
void display() {
cout << "Technical content writer\n";
};
int main() {
Javatpoint *j;//pointer reference
Intern i;
j = &i;
j->display();
}
Output:
Technical content writer
- In the derived class if we do not override the pure virtual function then the derived class becomes the abstract class.
Program:
#include<iostream>
using namespace std;
class Javatpoint {// abstract class
public:
virtual void display() = 0; // Pure Virtual Function
};
class Intern:public Javatpoint {};
int main() {
Javatpoint *j;// pointer reference
Intern i;
j = &i;
j->display();
}
Output:
Error: cannot declare variable 'i' to be of abstract type 'Intern'
- The abstract class can also be defined by the struct keyword in c++.
struct Javatpoint {// abstract class
public:
virtual void display() = 0; // Pure Virtual Function
};
- The abstract or base class can have its own constructors.
Program:
#include <iostream>
using namespace std;
class Base
{
public:
int a, b;
virtual void display() = 0;
Base(){ // base class can have its own constructor
a=5; b=1;
}
};
class Derived : public Base{
public:
Derived(){
a=30; b=10;
}
void display(){
cout << "Values are -\n" << a << " and " << b;
}
};
int main(){
Derived derived;
derived.display();
return 0;
}
Output:
Values are -
30 and 10