Compile Time Polymorphism with Templates in C++

When thinking of compile-time polymorphism, the first thing that comes to mind is functions overloading.  But here we will learn how to achieve compile-time polymorphism with templates.

Polymorphism is one of the four-four basic concepts of OOP (Object Oriented Programming) that are Inheritance, Abstraction, Polymorphism, and Encapsulation. I believe you already know polymorphism and its concept.

Let’s first understand the compile-time polymorphism. The Compile-time polymorphism is a polymorphism that happens at compile time. What this means is that the compiler must know what is going on. This is also mentioned as static time polymorphism, compile-time binding, static binding, early binding.

Now it’s time to understand how C++ templates can be used to achieve compile-time polymorphism. Here I am taking a very simple example to show you that how you can achieve compile-time polymorphism with templates.

 

Compile-time polymorphism using a class template:

As I have discussed that we can achieve compile-time polymorphism by templates in C++. In the below example, I am going to create a template class StudentFeethat can take any type. The template class allows a name (string) to be prepended to any class, with the proviso that the base class (the template parameter) supports the member function fees().

In this example, we have constructed a normal class, EngStudent, and MbaStudent, which supports the fees() method. When we create a StudentFee object, we give it the type of object we want to ‘decorate’ with a name.

Like in the below example I have created two objects of StudentFee one with EngStudent, and another with MbaStudent. Calling annualFees() on a StudentFee object will call the fees() on its base class – in our example EngStudent::fees() and MbaStudent::fees(). Let’s see the code,

 

Note: T is a very common name for a template parameter and we usually used it instead of more meaningful names.

#include <iostream>

template <class T>
class StudentFee : T
{
public:
    void annualFees()
    {
        this->fees();
    }
};


class EngStudent
{
public:
    void fees()
    {
        std::cout << "EngStudent Fees = INR 70,000" << std::endl;
    }
};


class MbaStudent
{
public:
    void fees()
    {
        std::cout << "MbaStudent Fees = INR 95,000" << std::endl;
    }
};



int main()
{
    StudentFee<EngStudent> engStudent;
    StudentFee<MbaStudent> mbaStudent;

    engStudent.annualFees();
    mbaStudent.annualFees();

    return 0;
}

Output:

compile time polymorphism with template

 

 

Compile-time polymorphism using a function template:

Like the template class, we can achieve compile-time polymorphism by function templates in C++. In the below example, I am going to create a function templatecustom_add()that can add any built-in data type. The compiler’s responsibility is to generate code for different input types based on the instructions you gave. See the below-mentioned code.

 

#include <iostream>

template <class T>
void custom_add (T val1, T val2)
{
    std::cout << "Addition = " << (val1 + val2) << std::endl;
}

int main ()
{
    custom_add<int>(3, 5);    // type specifier <int> present

    custom_add<float>(3.2, 4.5); // type specifier <float> present

    custom_add<double>(3.2123, 4.5456); // type specifier <float> present

    return 0;
}

Output:

Addition = 8
Addition = 7.7
Addition = 7.7579

 

Recommended Articles for you: