• 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

Generic Object Oriented Stack with Template

April 27, 2015 by Dhaval Dave

Here We have implemented a Generic Stack Example with Templates in C++ where we can Push any element into stack.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
using namespace std;

template <class T>
class Stack { 
  private: 
  vector<T> element; // 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 element.empty(); 
  } 
};
template <class T> void Stack<T>::push (T const& item) { 
 element.push_back(item); 
}
template <class T>
void Stack<T>::pop () { 
  if (element.empty()) { 
  throw out_of_range("Stack<>::pop(): empty stack"); 
  }
 element.pop_back(); 
}
template <class T> T Stack<T>::top () const 
{ 
  if (element.empty()) { 
  throw out_of_range("Stack<>::top(): empty stack"); 
  }
 return element.back(); 
}
int main() 
{ 
 try { 
  Stack<int> myIntegerStack; // stack of ints 
  Stack<string> myStringStack; // stack of strings
  myIntegerStack.push(10); 
       cout << myIntegerStack.top() <<endl;

myStringStack.push(“Messi”);
cout << myStringStack.top() << std::endl;
}
catch (exception const& ex) {
cerr << “Exception: ” << ex.what() <<endl;
return -1;
}
}
See Working Code at http://ideone.com/8sfDfC

Similar Articles

Filed Under: Adobe Interview Questions, Flipkart Interview Questions, Interview Questions, problem Tagged With: Stack

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

Cisco Hiring Event 21st – 22nd Feb 2015

Print Power Set of a Set

strtok()

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Reversal of LinkedList

Trie Dictionary

Microsoft BING Interview Experience

Search element in a matrix with all rows and columns in sorted order

Printing intermediate Integers between one element & next element of array

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Practo Hiring Experience

Print vertical sum of all the axis in the given binary tree

System Design: Designing a LLD for Hotel Booking

Interfaces in C++ (Abstract Classes in C++)

Maximum occurred Smallest integer in n ranges

Trapping Rain Water

TicTacToe Game As Asked in Flipkart

Naurki.com Security Breach

Test Cases for Round Function

Python String and numbers

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

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Maximum path sum between two leaves

Best Java Book | Top Java Programming Book for Beginners

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

Calculate price of parking from parking start end time prices

Memory Efficient LinkedList

Singly linked list

Find Percentage of Words matching in Two Strings

Copyright © 2026 · Genesis Framework · WordPress · Log in