• 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

Skiing on Mountains Matrix

Check if an array has duplicate numbers in O(n) time and O(1) space

Serialise Deserialise N-ary Tree

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Templates in C++

System Design: Designing a LLD for Hotel Booking

The greedy coins game Dynamic Programming

LeetCode: Container With Most Water

Circular Linked List

Knight Tour Problem (Graph – Breadth First Search)

Maximum of all subarrays of size k

Find Pythagorean Triplets in an array in O(N)

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Binary Tree in Java

Sort an array according to the order defined by another array

Reversal of LinkedList

strtok()

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

Advanced SQL Injection

Given a float number convert it into the string WITHOUT using any inbuilt Function

Print Power Set of a Set

VMWare Openings

Introduction To Number Theory ( Part 1 )

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Find Percentage of Words matching in Two Strings

Convert number to words java

Adobe Interview Questions 8 month Exp

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

TicTacToe Game As Asked in Flipkart

Wrong Directions given find minimum moves so that he can reach to the destination

Copyright © 2025 · Genesis Framework · WordPress · Log in