-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdestructors_using_dyanamic stack.cpp
More file actions
61 lines (53 loc) · 1.02 KB
/
destructors_using_dyanamic stack.cpp
File metadata and controls
61 lines (53 loc) · 1.02 KB
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
60
61
#include <iostream>
#include <stdlib.h>
using namespace std;
class stack
{
private:
char *data;
int top_;
public:
stack();
~stack();
int empty()
{
return (top_ == -1);
}
void push(char x)
{
data[++top_] = x;
}
void pop()
{
--top_;
}
char top()
{
return data[top_];
}
};
stack::stack() : data(new char[10]), top_(-1) //memory being assigned in heap of 10 chars and its pointer is returned to the data member 'data' .
{
cout << "call using stack::stack()" << endl;
}
stack::~stack()
{
//memory being deallocated at the end of main parenthesis by an implicit call to destructor.
cout << "\ncall using stack::~stack:() function" << endl;
delete data;
}
int main()
{
char str[10] = "VATSAL";
stack s;
for (int i = 0; i < 6; ++i)
{
s.push(str[i]);
}
while (!s.empty())
{
cout << s.top();
s.pop();
}
return 0;
}