Generic Programming Using C++

Filed Under: C++
Generic Programming Using C

In this article, we will learn to do generic programming using C++. We use generic programming for increasing the flexibility of the projects and programs. Large projects and software rely heavily on generic programming as far as the code is concerned. So without any further delay, let’s go see what generic programming means.

Also read: Large Prime Check Using The Sieve Of Eratosthenes in C++

What Is Generic Programming?

Generic programming is a modern-day form of code reusability. In generic programming, we do not write code intended for specific data types. Instead, we write code that can support multiple data types. Let’s consider a few examples.

  1. Function to print a vector.
    • The data can be of multiple types, say int, char, char *, float, etc. Instead of writing separate functions for every data type, we can generalize a common function to work for all the data types.
  2. Function to add two numbers
    • Again, the same story. There can be a large number of different types of numbers. Isn’t it cool to write one common function that can perform addition on all such data types?

There are countless examples of generic programming in our lives. Consider the example of plants, their food intake is restricted to vitamins, minerals, and water through the soil. But, humans on the other hand can even consume carbonated drinks, non-veg, vegetables, etc.

Concept of Generic Programming

The underlying concept behind generic programming is easy to understand. One just needs to have a basic understanding of data structures and object-oriented programming. Below are the steps that we will follow to use generic programming.

  • First, decide the number of different data types you want to use in your function.
  • To make generic functions and data structures, we will use the following idiom:
    • template <typename T1, typename t2,…………, typename Tn)
    • In the above idiom, the keyword typename represents one data type.
    • Tn represents the nth data type that we want to use in our function, structure, etc.
  • Once you declare the variable data types, now it’s time to replace all the fixed data types using the variable data types.
  • These variable data types will get replaced by the type that is passed to the function at the time of execution.

Generic Programming Using C++ Program

Following is a C++ program that will help you get more information regarding the topic.

#include <iostream>
#include <vector>

using namespace std;

// generic linear search
// this linear search will for for
// all kind of data types that are predefined
// in the C++ std namespace
template <typename T0>
int linear_search(vector <T0> v, T0 key)
{
	for(int i = 0; i < v.size(); i++)
	{
		if(v[i] == key)
			return i;
	}

	return v.size();
}

// generic input function
// pass the vector by reference to reflect
// the changes made during the function call
template <typename T>
void take_input_and_call_linear_search(vector <T> &v)
{
	cout << "Enter the size of the vector" << endl;
	int n;
	cin >> n;

	cout << "Enter the elements" << endl;
	while(n--)
	{
		T ele;
		cin >> ele;
		v.push_back(ele);
	}

	cout << "Enter the key" << endl;
	T key;
	cin >> key;

	int loc = linear_search(v, key);
	if(loc == v.size())
	{
		cout << "The key is not present in the vector" << endl;
	}
	else
	{
		cout << "Key found at the index: " << loc << endl;
	}
}

// driver code
int main()
{
	//Take the input
	cout << "We'll perform linear search on multiple vectors of differnt types" << endl;
	cout << "Integer type" << endl;
	vector <int> v1;
	take_input_and_call_linear_search(v1);

	cout << "Char type" << endl;
	vector <char> v2;
	take_input_and_call_linear_search(v2);

	cout << "String type" << endl;
	vector <string> v3;
	take_input_and_call_linear_search(v3);

	return 0;
}
Generic Programming Code
Code

Output

Generic Programming Output
Output

Conclusion

In this article, we learned the concept of generic programming using C++. First, we looked at the meaning of generic programming, and then we understood the generic programming idiom and concept. In the end, we wrote a C++ program to see the working of the idiom that we discussed. As you can see, for different data types, we just wrote a single function and it did all the work for us, this is the beauty of this concept. That’s all for today, thanks for reading.

Further Readings

To learn more about C++ data structures and algorithms, do visit the following articles.

https://www.journaldev.com/60624/staircase-search-on-sorted-2d-matrices-cpp

https://www.journaldev.com/61078/reverse-a-stack-cpp

close
Generic selectors
Exact matches only
Search in title
Search in content