SlideShare a Scribd company logo
Prepared by
Mohammed Sikander
Technical Lead
Cranes Software International Limited
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
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
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
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
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
• 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
• 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
• 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
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
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();
};
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.
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
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];
}
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
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
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
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

More Related Content

What's hot (20)

PPTX
Function basics
mohamed sikander
 
PDF
Implementing string
mohamed sikander
 
PPT
Cquestions
mohamed sikander
 
PDF
C++ Programming - 1st Study
Chris Ohk
 
DOCX
C++ file
Mukund Trivedi
 
PDF
C++ Programming - 11th Study
Chris Ohk
 
PDF
C++ programs
Mukund Gandrakota
 
PDF
C++ Programming - 2nd Study
Chris Ohk
 
DOCX
Basic Programs of C++
Bharat Kalia
 
PPT
C questions
mohamed sikander
 
PDF
C programs
Vikram Nandini
 
PPTX
C sharp 8
Germán Küber
 
PDF
C++ Programming - 4th Study
Chris Ohk
 
PDF
C program
Komal Singh
 
PDF
C++ TUTORIAL 1
Farhan Ab Rahman
 
DOCX
informatics practices practical file
Sai Sathvick Chirakala
 
PDF
C++ TUTORIAL 5
Farhan Ab Rahman
 
DOCX
Pratik Bakane C++
pratikbakane
 
PDF
Array notes
Hitesh Wagle
 
Function basics
mohamed sikander
 
Implementing string
mohamed sikander
 
Cquestions
mohamed sikander
 
C++ Programming - 1st Study
Chris Ohk
 
C++ file
Mukund Trivedi
 
C++ Programming - 11th Study
Chris Ohk
 
C++ programs
Mukund Gandrakota
 
C++ Programming - 2nd Study
Chris Ohk
 
Basic Programs of C++
Bharat Kalia
 
C questions
mohamed sikander
 
C programs
Vikram Nandini
 
C sharp 8
Germán Küber
 
C++ Programming - 4th Study
Chris Ohk
 
C program
Komal Singh
 
C++ TUTORIAL 1
Farhan Ab Rahman
 
informatics practices practical file
Sai Sathvick Chirakala
 
C++ TUTORIAL 5
Farhan Ab Rahman
 
Pratik Bakane C++
pratikbakane
 
Array notes
Hitesh Wagle
 

Viewers also liked (14)

PPTX
Early warning signs Business Infoload 2016
Infoload
 
PDF
Anatomia arterias-ilovepdf-compressed
Mishel Guerra
 
PDF
Kti restika oki lamorim
RESTIKAOKILAMORIM
 
PDF
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
Warnet Raha
 
DOCX
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
Warnet Raha
 
PDF
breastcare
Inmas Andi Sermoati
 
PPT
stack and queue array implementation in java.
CIIT Atd.
 
PDF
Kti rasmar yanti AKBID YKN BAU BAU
Operator Warnet Vast Raha
 
PPT
Indian media industry
Rajib jena
 
PDF
Kti ratma ningsih
Operator Warnet Vast Raha
 
DOCX
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
Adeline Dlin
 
PDF
Selenium web driver in Java
Xavier Sala Pujolar
 
PPTX
Kegawat daruratan pada neonatal
Irma Delima
 
PDF
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Irfan Nur
 
Early warning signs Business Infoload 2016
Infoload
 
Anatomia arterias-ilovepdf-compressed
Mishel Guerra
 
Kti restika oki lamorim
RESTIKAOKILAMORIM
 
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
Warnet Raha
 
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
Warnet Raha
 
stack and queue array implementation in java.
CIIT Atd.
 
Kti rasmar yanti AKBID YKN BAU BAU
Operator Warnet Vast Raha
 
Indian media industry
Rajib jena
 
Kti ratma ningsih
Operator Warnet Vast Raha
 
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
Adeline Dlin
 
Selenium web driver in Java
Xavier Sala Pujolar
 
Kegawat daruratan pada neonatal
Irma Delima
 
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Irfan Nur
 
Ad

Similar to Implementing stack (20)

PPTX
Stack and its applications
Ahsan Mansiv
 
DOCX
Java practical
shweta-sharma99
 
PDF
DATA STRUCTURE USING C & C++
mustkeem khan
 
PDF
DSU C&C++ Practical File Diploma
mustkeem khan
 
DOCX
Data Structure in C (Lab Programs)
Saket Pathak
 
PDF
Data struture lab
krishnamurthy Murthy.Tt
 
PDF
Stack concepts by Divya
Divya Kumari
 
PPT
Whats new in_csharp4
Abed Bukhari
 
PPTX
Object Oriented Programming using C++: C++ Templates.pptx
RashidFaridChishti
 
DOCX
Stack array
rimshailyas1
 
DOCX
C# labprograms
Jafar Nesargi
 
PDF
STLStack.pdf
rajaratna4
 
PDF
Advanced Java - Practical File
Fahad Shaikh
 
PPT
Stacks and queue
Amit Vats
 
PDF
Manual specialization
Szymon Matejczyk
 
ZIP
とある断片の超動的言語
Kiyotaka Oku
 
DOCX
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
bradburgess22840
 
PPTX
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
DroidConTLV
 
PPT
OBJECTS IN Object Oriented Programming .ppt
SaadAsim11
 
Stack and its applications
Ahsan Mansiv
 
Java practical
shweta-sharma99
 
DATA STRUCTURE USING C & C++
mustkeem khan
 
DSU C&C++ Practical File Diploma
mustkeem khan
 
Data Structure in C (Lab Programs)
Saket Pathak
 
Data struture lab
krishnamurthy Murthy.Tt
 
Stack concepts by Divya
Divya Kumari
 
Whats new in_csharp4
Abed Bukhari
 
Object Oriented Programming using C++: C++ Templates.pptx
RashidFaridChishti
 
Stack array
rimshailyas1
 
C# labprograms
Jafar Nesargi
 
STLStack.pdf
rajaratna4
 
Advanced Java - Practical File
Fahad Shaikh
 
Stacks and queue
Amit Vats
 
Manual specialization
Szymon Matejczyk
 
とある断片の超動的言語
Kiyotaka Oku
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
bradburgess22840
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
DroidConTLV
 
OBJECTS IN Object Oriented Programming .ppt
SaadAsim11
 
Ad

Recently uploaded (20)

PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Letasoft Sound Booster 1.12.0.538 Crack Download+ Product Key [Latest]
HyperPc soft
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Executive Business Intelligence Dashboards
vandeslie24
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Letasoft Sound Booster 1.12.0.538 Crack Download+ Product Key [Latest]
HyperPc soft
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 

Implementing stack

  • 1. Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited
  • 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 [email protected] 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; [email protected] 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