C++ Premier Plus 6th edition - Programming excercise - Chapter12 - 4
阿新 • • 發佈:2018-11-28
stack.h
// stack.h -- class definition for the stack ADT
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long Item;
class Stack
{
private:
enum { MAX = 10 }; // constant specific to class
Item * pitems; // holds stack items
int size; // number of elements in stack
int top; // index for top stack item
public:
Stack(int n = MAX); // creates stack with n elements
Stack(const Stack & st);
~Stack();
bool isempty() const;
bool isfull() const;
// push() returns false if stack already is full, true otherwise
bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(Item & item); // pop top into item
Stack & operator=(const Stack & st);
friend std::ostream& operator<<(std::ostream& os, const Stack&obj);
};
#endif
stack.cpp
#include<iostream>
#include"stack.h"
using std::cout;
// regardless how large n is,it's an empty Stack, so top =0
Stack::Stack(int n) // creates stack with n elements
{
size = n;
pitems = new Item[size];
top = 0;
for (int i = 0; i < size; i++)
pitems[i] = 0;
}
Stack::Stack(const Stack & st)
{
size = st.size;
top = st.top;
pitems = new Item[size];
for (int i = 0; i < top; i++)
pitems[i] = st.pitems[i];
}
Stack::~Stack()
{
delete[]pitems;
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
// push() returns false if stack already is full, true otherwise
bool Stack::push(const Item & item) // add item to stack
{
if (isfull())
{
cout << "Stack is full,push failed.\n";
return false;
}
else
{
pitems[top++] = item;
return true;
}
}
// pop() returns false if stack already is empty, true otherwise
bool Stack::pop(Item & item) // pop top into item
{
if (isempty())
{
cout << "Stack is empty, pop failed.\n";
return false;
}
else
{
item = pitems[--top];
return true;
}
}
Stack & Stack::operator=(const Stack & st)
{
// VERY IMPORTANT TIPS: if == was written as =,then 2 pointers point to same heap zone
// destructor of the two objects will try to delete same heap zone twice, will be error
if (pitems == st.pitems)
return *this;
delete[]pitems;
size = st.size;
top = st.top;
pitems = new Item[size];
for (int i = 0; i < top; i++)
pitems[i] = st.pitems[i];
return *this;
}
std::ostream& operator<<(std::ostream& os, const Stack&obj)
{
os << "The Stack object's details: \n";
for (int i = 0; i < obj.top; i++)
os << obj.pitems[i] << "\t";
os << " \ntotal " << obj.size << " elements.";
os << " \ntop is #" << obj.top << ".\n\n";
return os;
}
main
#include<iostream>
#include"stack.h"
using std::cout;
int main()
{
Stack first;// use default constructor, with 10 elements
unsigned long n;
first.pop(n);
first.push(100000);
first.push(200000);
cout << "\nAfter pushed 2 items, object \"first\" details:\n";
cout << first;
Stack second(8);
second.push(30000);
second.push(40000);
"\nAfter pushed 2 items, object \"second\" details:\n";
cout << second;
second.pop(n);
cout << "\nAfter pop from second, n is: " << n << '\n';
cout << second;
// use overloaded operator=
first = second;
cout << "After assigned second to first, frirst's details:\n";
cout << first;
// use copy constructor
Stack third = first;
cout << "After use first to create a new object \"third\",its details:\n";
cout << third;
std::cin.get();
}