Constructors/Destructors
Extend the Stack.h class given in order for it to store double numbers (instead of void* pointers).
Add a destructor that will delete all the stack by making calls to pop(), and for each element
destroyed the element will be printed to the output. Now add two constructors: the default one
that will create an empty stack, and another one that will receive an integer n and will build a
stack of n elements, such that the first element is 1.0, and the next ones are incremented by 0.1.
Upload your Stack class, which will be tested for correctness with stacks.cpp .
SAMPLE OUTPUT
s1
2 1.5 1 0.5
s2
1.3 1.2 1.1 1
end
Stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
struct Stack {
struct Link {
void* data;
Link* next;
void initialize(void* dat, Link* nxt) {
data = dat;
next = nxt;
}
}* head;
void initialize() {
head = 0;
}
void push(void* dat) {
Link* newLink = new Link;
newLink->initialize(dat, head);
head = newLink;
}
void* peek() {
if (head == 0) {
std::cout << "Stack is empty";
}
return head->data;
}
void* pop() {
if(head == 0)
return 0;
void* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}
void cleanup() {
if (head == 0){
std::cout << "Stack is empty";
}
else {
std::cout << "Stack is not empty";
}
}
};
#endif
stacks.cpp
#include <iostream>
#include "Stack.h"
using namespace std;
int main(int argc, const char * argv[])
{
cout << "s1n";
{
Stack s1;
for( int i = 1; i < 5; i++ )
s1.push( double(i)/2.0 );
}
cout << endl;
cout << "s2n";
{
Stack s2(4);
}
cout << "nendn";
return 0;
}

More Related Content

PDF
Modify the Stack class given in the book in order for it to store do.pdf
PPT
constructors and destructors in c++ language.ppt
PPTX
Constructor and destructor
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
Modify the Stack class given in the book in order for it to store do.pdf
constructors and destructors in c++ language.ppt
Constructor and destructor
Stack queue
Stack queue
Stack queue
Stack queue
Stack queue

Similar to Constructors-Destructors Extend the Stack-h class given in order for i.pdf (20)

PPT
Stack queue
PPT
Stack queue
PPTX
Stack and its applications
PPTX
An introduction to Constructors and destructors in c++ .pptx
PPT
Constructors & Destructors in C++ Simplified
PPTX
Oop presentation
PDF
Lab Manual-OOP.pdf
PDF
Opps manual final copy
PDF
OOPs manual final copy
PDF
Implementing stack
PPT
Friend this-new&delete
PPT
classes and constructors lec 03 & 04.ppt
PDF
00-intro-to-classes.pdf
PDF
DS & Algo 1 - C++ and STL Introduction
PDF
CS 32 Final Review Fall 2024 ucla school data
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PDF
STLStack.pdf
PPTX
Constructors in C++.pptx
PDF
Constructors and Destructors
PDF
Classes
Stack queue
Stack queue
Stack and its applications
An introduction to Constructors and destructors in c++ .pptx
Constructors & Destructors in C++ Simplified
Oop presentation
Lab Manual-OOP.pdf
Opps manual final copy
OOPs manual final copy
Implementing stack
Friend this-new&delete
classes and constructors lec 03 & 04.ppt
00-intro-to-classes.pdf
DS & Algo 1 - C++ and STL Introduction
CS 32 Final Review Fall 2024 ucla school data
Object Oriented Programming (OOP) using C++ - Lecture 3
STLStack.pdf
Constructors in C++.pptx
Constructors and Destructors
Classes
Ad

More from 21stcenturyjammu21 (20)

PDF
Consider the following intertemporal social welfare function- W-min(U0.pdf
PDF
Consider the classes below- Here is the code for the ParentChildRelati.pdf
PDF
Consider poiters who led the Professionat Goifers' Associabon of Amenc.pdf
PDF
Consider an economy with the following values- Autonomous Consumptio.pdf
PDF
Conduct research on IEEE- ACM or other relevant publications about the.pdf
PDF
Config Lab L2 EtherChannel 2 Both layer 2 EtherChannel and layer 3 Eth.pdf
PDF
Connecting youths io caing adels and octiaks Ae in ine Ahop Qtistiniti.pdf
PDF
consequences of immobility- System Affected Physiological Effect and P.pdf
PDF
Consequences of misrepresentation in contracts- where a party who reli (1).pdf
PDF
consequences are endured by only specific individuals or groups- True.pdf
PDF
Conduct research using the internet for one article concerning the Hum.pdf
PDF
Constructive receipt of an asset does NOT exst in which of the followi.pdf
PDF
Construct a support vector machine that computes the kernel function- (1).pdf
PDF
Construct a support vector machine that computes the kernel function-.pdf
PDF
Construct conceptual models for the following types of graphs- using E.pdf
PDF
Consider tossing a coin 6 times- This is a binomial experiment with P( (2).pdf
PDF
Consider tossing a coin 6 times- This is a binomial experiment with P(.pdf
PDF
Consider the Solow growth model with a production function per person (1).pdf
PDF
Consider the Solow growth model with a production function per person.pdf
PDF
Consider the standard model of impacts of immigration- Labor demand in.pdf
Consider the following intertemporal social welfare function- W-min(U0.pdf
Consider the classes below- Here is the code for the ParentChildRelati.pdf
Consider poiters who led the Professionat Goifers' Associabon of Amenc.pdf
Consider an economy with the following values- Autonomous Consumptio.pdf
Conduct research on IEEE- ACM or other relevant publications about the.pdf
Config Lab L2 EtherChannel 2 Both layer 2 EtherChannel and layer 3 Eth.pdf
Connecting youths io caing adels and octiaks Ae in ine Ahop Qtistiniti.pdf
consequences of immobility- System Affected Physiological Effect and P.pdf
Consequences of misrepresentation in contracts- where a party who reli (1).pdf
consequences are endured by only specific individuals or groups- True.pdf
Conduct research using the internet for one article concerning the Hum.pdf
Constructive receipt of an asset does NOT exst in which of the followi.pdf
Construct a support vector machine that computes the kernel function- (1).pdf
Construct a support vector machine that computes the kernel function-.pdf
Construct conceptual models for the following types of graphs- using E.pdf
Consider tossing a coin 6 times- This is a binomial experiment with P( (2).pdf
Consider tossing a coin 6 times- This is a binomial experiment with P(.pdf
Consider the Solow growth model with a production function per person (1).pdf
Consider the Solow growth model with a production function per person.pdf
Consider the standard model of impacts of immigration- Labor demand in.pdf
Ad

Recently uploaded (20)

PDF
Empowerment Technology for Senior High School Guide
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
advance database management system book.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
IGGE1 Understanding the Self1234567891011
PPTX
20th Century Theater, Methods, History.pptx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
HVAC Specification 2024 according to central public works department
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Empowerment Technology for Senior High School Guide
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
advance database management system book.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
IGGE1 Understanding the Self1234567891011
20th Century Theater, Methods, History.pptx
AI-driven educational solutions for real-life interventions in the Philippine...
History, Philosophy and sociology of education (1).pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
B.Sc. DS Unit 2 Software Engineering.pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
HVAC Specification 2024 according to central public works department
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...

Constructors-Destructors Extend the Stack-h class given in order for i.pdf

  • 1. Constructors/Destructors Extend the Stack.h class given in order for it to store double numbers (instead of void* pointers). Add a destructor that will delete all the stack by making calls to pop(), and for each element destroyed the element will be printed to the output. Now add two constructors: the default one that will create an empty stack, and another one that will receive an integer n and will build a stack of n elements, such that the first element is 1.0, and the next ones are incremented by 0.1. Upload your Stack class, which will be tested for correctness with stacks.cpp . SAMPLE OUTPUT s1 2 1.5 1 0.5 s2 1.3 1.2 1.1 1 end Stack.h #ifndef STACK_H #define STACK_H #include <iostream> struct Stack { struct Link { void* data; Link* next; void initialize(void* dat, Link* nxt) { data = dat; next = nxt; } }* head; void initialize() { head = 0;
  • 2. } void push(void* dat) { Link* newLink = new Link; newLink->initialize(dat, head); head = newLink; } void* peek() { if (head == 0) { std::cout << "Stack is empty"; } return head->data; } void* pop() { if(head == 0) return 0; void* result = head->data; Link* oldHead = head; head = head->next; delete oldHead; return result; } void cleanup() { if (head == 0){
  • 3. std::cout << "Stack is empty"; } else { std::cout << "Stack is not empty"; } } }; #endif stacks.cpp #include <iostream> #include "Stack.h" using namespace std; int main(int argc, const char * argv[]) { cout << "s1n"; { Stack s1; for( int i = 1; i < 5; i++ ) s1.push( double(i)/2.0 ); } cout << endl; cout << "s2n"; {
  • 4. Stack s2(4); } cout << "nendn"; return 0; }