×

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

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 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.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.