Templates in C++

Templates in C++




Templates in C++

Templates in C++

Templates in C++

1.1 What is Templates in C++
1.2 How templates work
1.3 Example of template
1.4 Difference between function overloading and template.

1.1 What is Templates in C++

Templates in C++ is a simple idea to pass data type as a parameter so that we don't need to write the same code for the different data type.

In C++ we have two keywords to support template "template" and "typename" the second keyword can be replaced by keyword "class".

1.2 How templates work

Our code will contain only one function but based on our program compiler creates a copy of our function of the required data type.

How templates work
How templates work
In the above program, we wrote a function to find the maximum out of two inputs and we are using a template to set the data type of myMax function according to the input.
For using a template:
  1. we have to declare template like (template<typename T>).
  2. Then instead of declaring the function as  (int myMax/char MyMax/float MyMax/double MyMax) declare it as T myMax (replace data type by T).
  3. Replace the data type of variable(int x/double x/float x..) by T x.

1.3 Example of Template:

#include <iostream>
using namespace std; 
// One function works for all data types.  This would work
template <typename T>
T myMax(T x, T y)
{
   return (x > y)? x: y;
}
  
int main()
{
  cout << myMax<int>(2, 8) << endl;  // Call myMax for int
  cout << myMax<double>(5.0, 8.0) << endl; // call myMax for double
  cout << myMax<char>('k', 'g') << endl;   // call myMax for char
  
  return 0;
}
Output:
8
8
k

1.4 Difference between function overloading and templates

Both are an example of polymorphism. Templates are used when multiple functions do identical operation while overloading is used when multiple functions do a similar operation.

8 comments:

  1. C & C++ so You Can Contact US-+91-9311002620 Or Visit Our Website- https://www.htsindia Thanks for Sharing a very Nice Blog for Us & it is very Unique Content Otherwise if anyone want to Learn.com/Courses/business-analytics

    ReplyDelete
  2. Informative Post. thanks For Sharing this Post. Keep It Up. Keep posting. Otherwise anyone wants to Learn C++ Training Course so, Contact here- +91-9311002620 or visit website- https://www.htsindia.com/Courses/modular-courses/c-plus-plus-training-course

    ReplyDelete
  3. Wow, yeah it's a very good Blogs for you Query. Other wise If If Any one who want learn C++ & Advance contact us on 9311002620 or visit :- https://htsindia.com/Courses/embedded-systems-and-robotics/cplusplustraininginstituteinsouthdelhi

    ReplyDelete
  4. A very very informative content you put in your blog. Thanks for sharing this kind of information I really learned a lot from it. Further More Information About C++Language Training Institute in Delhi So Contact Here-+91-9311002620 Or Visit Website-https://htsindia.com/Courses/embedded-systems-and-robotics/cplusplustraininginstituteinsouthdelhi

    ReplyDelete
  5. Great Work. I always use to follow your valuable contents. Thanks for sharing this kind of knowledge. It is very helpful and very informative and I really learned a lot from it. Further More Information About AUTOCAD Training Institute in Delhi So Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/AutoCAD-training-courses

    ReplyDelete
  6. Thanks for sharing this amazing post this is the content i really looking for, it's very helpful i hope you will continue your blogging anyway if anyone looking for java training institute in delhi contact us +91-9311002620 visit-
    https://www.htsindia.com/java-training-courses

    ReplyDelete
  7. I really appreciate your hard work you put into your blog and detailed information you provide. Further More Information About excel training institute in Delhi Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/Business-Analytics/adv-excel-training-course

    ReplyDelete
  8. Thanks for sharing a really needed post that i am looking for , if anyone looking for best java institute in delhi so join with us visit our website : https://www.htsindia.com/java-training-courses or contact us : +91-9311002620

    ReplyDelete

For Query and doubts!

Powered by Blogger.