This document discusses various approaches to implementing a stack data structure in C++ using classes. It begins by showing a basic implementation with arrays and indices, then improves on this by grouping the buffer and index into a struct. Next, it defines the stack as a class with push and pop methods. Further optimizations include making members private, adding a constructor to initialize values, and using dynamic memory allocation to allow stacks of variable sizes. The document concludes by demonstrating the use of a destructor to free allocated memory.
2. This document will help you to understand the
concepts of C++ through simple programs.The
topics covered in this document are
Size of Class
Constructor and Destructor
Lifetime of Objects
Memory Leakage
mohammed.sikander@cranessoftware.com 2
3. C
int buffer[20];
int topIndex = -1;
void push(int ele);
int pop( );
void display( );
DRAWBACKOFTHIS
APPROACH
This approach works only if
you have One stack in the
program.
Sikander 3
4. WORKWITH LOCALVARIABLES
AND PARAMETERS
void push(int buffer[],int *ti,int ele);
int pop(int buffer[],int *ti);
void display(const int buffer[],int ti);
int main( )
{
int buffer1[20] , buffer2[20];
int ti1 =-1 , ti2 = -1;
//Insert in first Stack.
push(buffer1, &ti1 , 45);
//Insert in Second Stack
push(buffer2, &ti2 , 75);
}
DRAWBACKOFTHIS
APPROACH
Every time you want to add a
new stack, we need to create
two separate variables (Array
andTopIndex)
Possibility of passing buffer1 and
ti2.
Sikander 4
5. GROUP BUFFER ANDTOP INDEX INTO ONE UNIT
struct MyStack
{
int ti;
int buffer[20];
};
void push(MyStack *ps,int ele)
{
buffer[++ti] = ele;
ps->buffer[++ps->ti] = ele;
}
int pop(MyStack *ps);
void display(const MyStack *ps);
int main( )
{
struct MyStack s1 = {-1} , s2 = {-1};
//Insert in first Stack.
push( &s1 , 45);
//Insert in Second Stack
push(&2 , 75);
}
Sikander 5
6. GROUPING DATA AND FUNCTIONS INTO ONE UNIT
struct MyStack
{
int ti;
int buffer[20];
void push(int ele)
{
buffer[++ti] = ele;
}
};
int main( )
{
MyStack s1 = {-1} , s2 = {-1};
s1.push(45);
s2.push( 75);
}
Sikander 6
7. • All members are private by default in class.
• Cannot access private members from outside the class.
• Cannot initialize the members of class / struct with traditional
method if the members are private
class MyStack
{
int ti;
int buffer[20];
void push(int ele)
{
buffer[++ti] = ele;
}
};
int main( )
{
MyStack s1 = {-1} , s2 = {-1};
s1.push(45);
s2.push( 75);
}
Sikander 7
8. • All members are private by default in class.
• Cannot access private members from outside the class.
• Cannot initialize the members of class / struct with traditional
method if the members are private
class MyStack
{
int ti;
int buffer[20];
public:
void push(int ele)
{
buffer[++ti] = ele;
}
};
int main( )
{
MyStack s1, s2;
s1.push(45);
s2.push( 75);
}
Sikander 8
9. • What happens if we forget to invoke initialize function?
• Can we write a function such that it invokes automatically on Object
Creation?
class MyStack
{
int ti;
int buffer[20];
public:
void initialize( )
{
ti = -1;
}
void push(int ele);
};
int main( )
{
MyStack s1, s2;
s1.initialize( );
s2.initialize( );
s1.push(45);
s2.push( 75);
}
Sikander 9
10. class MyStack
{
int ti;
int buffer[20];
public:
MyStack( )
{ ti = -1;
}
void push(int ele)
{
if(top == 20-1)
cout <<“Stack Full”;
else
buffer[++topIndex] = ele;
}
};
int main( )
{
MyStack s1, s2;
s1.push(45);
s2.push( 75);
}
Sikander 10
11. mystack.cpp
#include“mystack.h”
MyStack::MyStack( )
{ ti = -1;
}
void MyStack::push(intele)
{ buffer[++ti] = ele;
}
int MyStack::pop()
{ return buffer[ti--];
}
Testmystack.cpp
#include “mystack.h”
int main( )
{
MyStack s1;
s1.push(45);
s1.push( 75);
cout << s1.pop( ) << endl;
cout << s1.pop( ) << endl;
}
Sikander 11
mystack.h
class MyStack {
int ti;
int buffer[20];
public: MyStack( );
void push(int ele);
int pop();
};
12. class MyStack
{
int ti;
int buffer[20];
public:
MyStack( );
void push(int ele);
int pop();
void display();
};
int main( )
{
MyStack s1, s2;
}
Sikander 12
S1 can store 20 elements.
S2 can store 20 elements.
Requirement : I would like to have a say on the size of buffer when creating object.
Different Objects might have different sizes.
13. class MyStack
{
int topIndex;
int *buffer;
int size;
public:
MyStack(int sz) {
topIndex = -1;
size = sz;
buffer = new int[size];
}
void push(int ele)
{
if(top == size -1)
cout <<“Stack Full”;
else
buffer[++topIndex] = ele;
}
};
int main( )
{
//Create a stack object whichcan hold 5 elements.
MyStack s1 = MyStack(5);
//Create a stack object whichcan hold 10 elements.
MyStack s2 = MyStack(10);
cout <<“Inserting in first Stack n”;
for(int index = 0 ; index < 7 ; index++)
s1.push(3);
cout <<“ Inserting in Second Stack n”;
for(int index = 0 ; index < 7 ; index++)
s2.push(5);
}
Sikander 13
14. Creating object with below statement throw error.
MyStack s3;
mohammed.sikander@cranessoftware.com 14
If the user does not specify the size, it should take a fixed
size of 6
MyStack(int sz = 6)
{
topIndex = -1;
size = sz;
buffer = new int[size];
}
15. class MyStack
{
int topIndex;
int *buffer;
int size;
public:
MyStack(int sz = 6) {
topIndex = -1;
size = sz;
buffer = new int[size];
}
void push(int ele)
{
buffer[++topIndex] = ele;
}
int pop( )
{
return buffer[topIndex --];
}
};
int main( )
{
//Create a stack object whichcan hold 5 elements.
MyStack s1 = MyStack(5);
cout <<“Inserting in Stack n”;
for(int index = 0 ; index < 5 ; index++)
s1.push(3);
cout <<“Deleting from Stack n”;
for(int index = 0 ; index < 5 ; index++)
s1.pop( );
}
Sikander 15
16. class MyStack
{
int topIndex;
int *buffer;
int size;
public:
MyStack(int sz = 6) ;
void push(int ele);
int pop( );
~MyStack( )
{
delete [ ] buffer;
}
};
int main( )
{
//Create a stack object whichcan hold 5 elements.
MyStack s1 = MyStack(5);
cout <<“Inserting in Stack n”;
for(int index = 0 ; index < 5 ; index++)
s1.push(3);
cout <<“Deleting from Stack n”;
for(int index = 0 ; index < 5 ; index++)
s1.pop( );
}
Sikander 16
17. class MyStack
{
int topIndex;
int *buffer;
int size;
public:
MyStack(int sz = 6) ;
void push(int ele);
int pop( );
~MyStack( )
{
delete [ ] buffer;
}
};
int main( )
{
//Create a stack object whichcan hold 5 elements.
MyStack *ptr;
ptr = new MyStack(5);
cout <<“Inserting in Stack n”;
for(int index = 0 ; index < 5 ; index++)
ptr->push(3);
cout <<“Deleting from Stack n”;
for(int index = 0 ; index < 5 ; index++)
ptr->pop( );
}
Sikander 17
18. class MyStack
{
int topIndex;
int *buffer;
int size;
public:
MyStack(int sz = 6) ;
void push(int ele);
int pop( );
~MyStack( )
{
delete [ ] buffer;
}
};
int main( )
{
//Create a stack object whichcan hold 5 elements.
MyStack *ptr;
ptr = new MyStack(5);
cout <<“Inserting in Stack n”;
for(int index = 0 ; index < 5 ; index++)
ptr->push(3);
cout <<“Deleting from Stack n”;
for(int index = 0 ; index < 5 ; index++)
ptr->pop( );
delete ptr;
}
Sikander 18