What is a Virtual Function Template Class and How to Use in C++?

What is a Virtual Function Template Class C++?

Do you want to learn what is a virtual function template class and how it can be implemented or used in C++ (CPP)? If yes, then keep reading this article.

Templates and Virtual functions are two of the polymorphism options in C++. Unfortunately, you can’t do that. Virtual template functions are prohibited in C++ due to the complexity of virtual tables that would need to be created. 

Templates for member functions cannot be declared virtual. The standard implementation of the virtual function call method employs a fixed-size database with one entry for each virtual function, which imposes this restriction. However, once the complete program has been translated, the number of instantiations of a member function template is still being determined. 

To implement virtual member function templates, C++ compilers and linkers would need to provide an entirely new mechanism. On the other hand, since their number is fixed when a class is instantiated, the regular members of class templates can be virtual. So without wasting time, let’s start moving toward the article.

 

 

What is a Template Class in C++?

As the name implies, a template class is a template for classes. We have the means to design a class in C++ that will act as a model or template for other classes. Generic variables and type T methods will be included in a template class; they can subsequently be modified to work with various data types as needed. 

A template class in C++ is a class that enables programmers to work with generic data types according to the standard specification. This eliminates the need to write the class from scratch for each data type, allowing it to be used on a wide range of them depending on the needs. In addition, using template functions allows for several types in a single piece of code. 

Let’s see an example:

 

Code

#include <iostream>

using namespace std;

template <class T>

class number {

   private:

    T num;




   public:

    number(T n) : num(n) {}  




    T getNum() {

        return num;

    }

};




int main() {




    number<int> numInt(9);

    number<double> numDouble(9.6);




    cout << "The value of the  Integer is: = " << numInt.getNum() << endl;

    cout << "The value of  Double is : = " << numDouble.getNum() << endl;




    return 0;

}

 

Output

The value of the Integer is : = 9

The value of  Double is: = 9.6

 

In the above example, we have created a class template called Number. Note that the constructor argument n, the variable num, and the method getNum() all have return values of type T. They can thus be of any sort, according to that. We created the class template’s objects in main() to implement it. 

The variables numdouble and numint in the code are already mentioned. Integer and float classes are defined as a result, and they are then utilized as necessary. Declaring the type of an object while using class template objects is a good idea. Some compilers may give an error if this is not the case.

 

 

What is a Virtual Function in C++?

A member function that is declared in a base class and redefined (overridden) by a derived class is known as a virtual function. You can call a virtual function for an object of a derived class and have it run the derived class’s version of the function when you refer to an object of a derived class using a pointer or a reference to the base class. To provide diverse classes with a consistent interface, virtual functions are helpful.

Regardless of the kind of reference (or pointer) used for the function call, virtual functions ensure that the proper function is called for an object. Their primary purpose is to implement runtime polymorphism. In base classes, functions are specified using the virtual keyword. Runtime resolution of function calls is carried out. 

 

 

How to Use a Virtual Function in C++?

In C++, a virtual function can behave in one of two ways: 

  1. Compile-time[early-binding]
  2.  Run-time[late-binding]

Let’s see an  Example illustrating both ways

 

Code

#include<iostream>

using namespace std;




class my_base {

public:

virtual void print()

{

cout << "print my_base class\n";

}




void show()

{

cout << "Show  output CompilerTime\n";

}

};




class derived : public my_base {

public:

void print()

{

cout << "Printed output for RunTime\n";

}

};




int main()

{

my_base *bptr;

derived d;

bptr = &d;

bptr->print();

bptr->show();

return 0;

}

 

Output

Printed output for RunTime

Show  output CompilerTime

 

Only the my_base class type pointer (or reference) allows for the runtime behaviour of the virtual function. The my_base class pointer can also point to objects from the base class and those from a derived class. For example, the address of object d of the derived class is included in the base class pointer bptr in the code above.

Since the print() function is declared with the virtual keyword, it will be bound at runtime (output is print derived class as a pointer is pointing to object of derived class), and show() is non-virtual, it will be bound during compile time (output is shown derived class). Late binding (Runtime) is done by the content of the pointer (i.e., location pointed to by pointer), and The type does early binding (Compile-time).

 

 

Conclusion

To summarize the article, we discussed virtual functions in C++ and some simple examples. Additionally, template class in C++ with example. You may now make your virtual functions, use them to run modified versions of base class functions, and test how a virtual function behaves at runtime or during compilation.

You may also get around the issue of a pointer constantly referring to the base class by using virtual functions.

Let’s have a quick recap of the topics discussed in this article.

  1. Virtual function Template class in C++?
  2. What is a Template class in C++?
  3. What is a Virtual function in C++? 
  4. Using a virtual function in C++?

Suppose you’ve found this article helpful. 👇: Which solutions have helped you solve the problem?

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share