×

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:
    1. Virtual int display()=0;
    2. 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

Related Topics

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.