• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Templates in C++

November 5, 2014 by Dhaval Dave

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

example : There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>.

Function and Class templates are two tupes …

Function Template:

a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be determined so that the compiler can generate an actual function (or class, from a class template). 
example
template<typename T>
void f(T s)
{
std::cout << s << 'n';
}

Explicit Instantiation : An explicit instantiation definition forces instantiation of the function or member function they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the program.

template<typename T>
void f(T s)
{
std::cout << s << 'n';
}
 
template void f<double>(double); // instantiates f<double>(double)
template void f<>(char); // instantiates f<char>(char), template argument deduced
template void f(int); // instantiates f<int>(int), template argument deduced

Implicit Instantiation : When code refers to a function in context that requires the function definition to exist, and this particular function has not been explicitly instantiated, implicit instantiation occurs. The list of template arguments does not have to be supplied if it can be deduced from context

#include <iostream>
 
template<typename T>
void f(T s)
{
std::cout << s << 'n';
}
 
int main()
{
f<double>(1); // instantiates and calls f<double>(double)
f<>('a'); // instantiates and calls f<char>(char)
f(7); // instantiates and calls f<int>(int)
void (*ptr)(std::string) = f; // instantiates f<string>(string)
}

Class templates

Just like we can create function templates, we can also create class templates, allowing classes to have members that use template parameters as types. For example:
template <class type> class class-name {
.
.
.
}

Here, type is the placeholder type name, which will be specified when a class is instantiated. You can define more than one generic data type by using a comma-separated list. Following is the example to define class Stack<> and implement generic methods to push and pop the elements from the stack:

Example 1.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>

using namespace std;

template <class T>
class Stack {
private:
vector
<T> elems; // elements

public:
void push(T const&); // push element
void pop(); // pop element
T top
() const; // return top element
bool empty() const{ // return true if empty.
return elems.empty();
}
};

template <class T>
void Stack<T>::push (T const& elem)
{
// append copy of passed element
elems
.push_back(elem);
}

template <class T>
void Stack<T>::pop ()
{
if (elems.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
// remove last element
elems
.pop_back();
}

template <class T>
T
Stack<T>::top () const
{
if (elems.empty()) {
throw out_of_range("Stack<>::top(): empty stack");
}
// return copy of last element
return elems.back();
}

int main()
{
try {
Stack<int> intStack; // stack of ints
Stack<string> stringStack; // stack of strings

// manipulate int stack
intStack
.push(7);
cout
<< intStack.top() <<endl;

// manipulate string stack
stringStack
.push("hello");
cout
<< stringStack.top() << std::endl;
stringStack
.pop();
stringStack
.pop();
}
catch (exception const& ex) {
cerr
<< "Exception: " << ex.what() <<endl;
return -1;
}
}
If we compile and run above code, this would produce the following result:
7
hello
Exception: Stack<>::pop(): empty stack

Example 2 : 

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second)
{a=first; b=second;}
T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}

int main () {
mypair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}

Notice the syntax of the definition of member function getmax:



template <class T>
T mypair<T>::getmax ()


Similar Articles

Filed Under: Adobe Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: c

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Microsoft BING Interview Experience

The greedy coins game Dynamic Programming

Find the kth number with prime factors 3, 5 and 7

Closed Parentheses checker

Find Percentage of Words matching in Two Strings

Python Dictionaries

Get K Max and Delete K Max in stream of incoming integers

Memory Efficient LinkedList

Subset Sum Problem Dynamic programming

Count Possible Decodings of a given Digit Sequence

Diagonal Traversal of Binary Tree

Apriori algorithm C Code Data Mining

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Reverse a Linked List in groups of given size

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Mirror of Tree

Possible sizes of bus to carry n groups of friends

Generate largest number arranging a no. of given non negative integer numbers

Maximum difference between two elements s.t larger element appears after the smaller number

Amazon Interview Experience – SDE Chennai

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

simple sql injection

Serialise Deserialise N-ary Tree

Flipkart SDET Interview Experience

C Program for TAIL command of UNIX

Daughter’s Age VeryGood Puzzle

1014 Practice Question of New GRE – Princeton

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Printing each word reverse in string

Level order traversal in Spiral form

Copyright © 2025 · Genesis Framework · WordPress · Log in