-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathTemplate-III (class templates).cpp
More file actions
59 lines (48 loc) · 1012 Bytes
/
Template-III (class templates).cpp
File metadata and controls
59 lines (48 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
/*
Template - III
Class templates
We can have default parameters in templates as well
template<class T1 = int, class T2 = string>
class Student {
//body
};
//All below are valid now
Student<int> s1(5, "avc");
Student<int, string> s2(5, "abc");
Student<> s3(5, "dfg");
Student<string> s4("abc", "def");
Student<int> s5(5, 4);
Student<string, string> s5("dfd", "dfs");
Student<int, int> s6(1, 2);
*/
template<class T>
class Stack {
private :
T data[100];
int top_;
public:
Stack() : top_(-1) {};
~Stack();
void push(const T& item) {
data[++top_] = item;
}
void pop() {
--top_;
}
const T& top() const {
if(top_ != -1)
return data[top_];
cout << "Empty stack" << endl;
return -1;
}
bool empty() {
return top_==-1;
}
};
int main() {
stack<int> st1;
stack<char> st2;
return 0;
}