SlideShare a Scribd company logo
1 of 18
Download to read offline
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)

Function basics
Function basicsFunction basics
Function basics
 
Implementing string
Implementing stringImplementing string
Implementing string
 
Cquestions
Cquestions Cquestions
Cquestions
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
C++ file
C++ fileC++ file
C++ file
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
C++ programs
C++ programsC++ programs
C++ programs
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C questions
C questionsC questions
C questions
 
C programs
C programsC programs
C programs
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C program
C programC program
C program
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
informatics practices practical file
informatics practices practical fileinformatics practices practical file
informatics practices practical file
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Array notes
Array notesArray notes
Array notes
 
Vcs16
Vcs16Vcs16
Vcs16
 

Viewers also liked

Early warning signs Business Infoload 2016
Early warning signs   Business Infoload 2016Early warning signs   Business Infoload 2016
Early warning signs Business Infoload 2016Infoload
 
Anatomia arterias-ilovepdf-compressed
Anatomia arterias-ilovepdf-compressedAnatomia arterias-ilovepdf-compressed
Anatomia arterias-ilovepdf-compressedMishel Guerra
 
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
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...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...Warnet Raha
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.CIIT Atd.
 
Indian media industry
Indian media industryIndian media industry
Indian media industryRajib jena
 
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)Adeline Dlin
 
Kegawat daruratan pada neonatal
Kegawat daruratan pada neonatalKegawat daruratan pada neonatal
Kegawat daruratan pada neonatalIrma Delima
 
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidanKepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidanIrfan Nur
 

Viewers also liked (14)

Early warning signs Business Infoload 2016
Early warning signs   Business Infoload 2016Early warning signs   Business Infoload 2016
Early warning signs Business Infoload 2016
 
Anatomia arterias-ilovepdf-compressed
Anatomia arterias-ilovepdf-compressedAnatomia arterias-ilovepdf-compressed
Anatomia arterias-ilovepdf-compressed
 
Kti restika oki lamorim
Kti restika oki lamorimKti restika oki lamorim
Kti restika oki lamorim
 
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
 
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
 
breastcare
breastcarebreastcare
breastcare
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Kti rasmar yanti AKBID YKN BAU BAU
Kti rasmar yanti AKBID YKN BAU BAUKti rasmar yanti AKBID YKN BAU BAU
Kti rasmar yanti AKBID YKN BAU BAU
 
Indian media industry
Indian media industryIndian media industry
Indian media industry
 
Kti ratma ningsih
Kti ratma ningsihKti ratma ningsih
Kti ratma ningsih
 
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
 
Selenium web driver in Java
Selenium web driver in JavaSelenium web driver in Java
Selenium web driver in Java
 
Kegawat daruratan pada neonatal
Kegawat daruratan pada neonatalKegawat daruratan pada neonatal
Kegawat daruratan pada neonatal
 
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidanKepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
 

Similar to Implementing stack

Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diplomamustkeem khan
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by DivyaDivya Kumari
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical FileFahad Shaikh
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queueAmit Vats
 
とある断片の超動的言語
とある断片の超動的言語とある断片の超動的言語
とある断片の超動的言語Kiyotaka Oku
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxbradburgess22840
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...DroidConTLV
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptSaadAsim11
 

Similar to Implementing stack (20)

Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Java practical
Java practicalJava practical
Java practical
 
Stack
StackStack
Stack
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by Divya
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Stack array
Stack arrayStack array
Stack array
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
STLStack.pdf
STLStack.pdfSTLStack.pdf
STLStack.pdf
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
Manual specialization
Manual specializationManual specialization
Manual specialization
 
とある断片の超動的言語
とある断片の超動的言語とある断片の超動的言語
とある断片の超動的言語
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
 
Xiicsmonth
XiicsmonthXiicsmonth
Xiicsmonth
 

Recently uploaded

WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfryanfarris8
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 

Recently uploaded (20)

WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 

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 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