A partially-written program stack.cpp posted in this site under examples. Finish
ID: 3724369 • Letter: A
Question
A partially-written program stack.cpp posted in this site under examples. Finish this program by submitting the push & pos functions, which should work as the programs documentation describes. Add a little bit to main to test the push & pos functions.
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
class Stack{
public:
static const unsigned MAX_CAP = 3;
Stack();
// free copy ctor
Stack(const Stack & other) = default;
Stack(const int ar[], unsigned els);
Stack(const vector<int> & v);
void show() const;
unsigned size() const;
private:
int _data[MAX_CAP];
unsigned _els;
}; // class Stack
bool die(const string & msg);
int main(){
Stack st; // declare a Stack var st
st.show();
Stack st2(st);
st2.show();
int data[] = {11, 22, 33};
// pass anonymous temporary to show
Stack(data, 3).show();
std::cout <<"bye" <<std::endl;
}
Stack::Stack(){
cout <<"I'm default ctor ";
_els = 0;
}
Stack::Stack(const int ar[], unsigned els){
if(els > MAX_CAP)
die("Stack::Stack: array too big");
for(unsigned i = 0; i < els; i++)
_data[i] = ar[i];
_els = els;
}
Stack::Stack(const vector<int> & v){
if(v.size() > MAX_CAP)
die("Stack::Stack: vector too big");
for(unsigned i = 0; i < v.size(); i++)
_data[i] = v[i];
_els = v.size();
}
void Stack::show() const {
cout <<"[" <<_els <<"]:";
for(unsigned i = 0; i < _els; i++)
cout <<" " <<_data[i];
cout <<endl;
}
unsigned Stack::size() const {return _els;}
bool die(const string & msg){
cout <<"Fatal error: " <<msg <<endl;
exit(EXIT_FAILURE);
}
Explanation / Answer
Below is your program. Let me know if you have any issues
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
class Stack{
public:
static const unsigned MAX_CAP = 3;
Stack();
// free copy ctor
Stack(const Stack & other) = default;
Stack(const int ar[], unsigned els);
Stack(const vector<int> & v);
void show() const;
unsigned size() const;
bool push(int x);
int pop();
private:
int _data[MAX_CAP];
unsigned _els;
}; // class Stack
bool die(const string & msg);
int main(){
Stack st; // declare a Stack var st
st.show();
Stack st2(st);
st2.push(2);
st2.show();
int data[] = {11, 22, 33};
// pass anonymous temporary to show
Stack(data, 3).show();
Stack st4(data,3);
st4.pop();
st4.show();
std::cout <<"bye" <<std::endl;
}
Stack::Stack(){
cout <<"I'm default ctor ";
_els = 0;
}
Stack::Stack(const int ar[], unsigned els){
if(els > MAX_CAP)
die("Stack::Stack: array too big");
for(unsigned i = 0; i < els; i++)
_data[i] = ar[i];
_els = els;
}
Stack::Stack(const vector<int> & v){
if(v.size() > MAX_CAP)
die("Stack::Stack: vector too big");
for(unsigned i = 0; i < v.size(); i++)
_data[i] = v[i];
_els = v.size();
}
void Stack::show() const {
cout <<"[" <<_els <<"]:";
for(unsigned i = 0; i < _els; i++)
cout <<" " <<_data[i];
cout <<endl;
}
unsigned Stack::size() const {return _els;}
bool die(const string & msg){
cout <<"Fatal error: " <<msg <<endl;
exit(EXIT_FAILURE);
}
bool Stack::push(int x)
{
if (_els >= MAX_CAP)
{
die("Stack::Stack: Stack is full");
return false;
}
else
{
_data[_els++] = x;
return true;
}
}
int Stack::pop()
{
if (_els <= 0)
{
die("Stack::Stack: Stack is empty");
return 0;
}
else
{
int x = _data[--_els];
return x;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.