You are to write a C++ Stack class template (see stack.h) which implements a stack. You must implement your own dynamically allocated list for this assignment that will handle these simple C++ simple types ( char, float, double, and bool). You may not use any C++ standard templates. You are to provide for the implementation for the member functions full, empty, top, push, pop, and size, as well as a default constructor that creates an empty stack of size 100, a constructor where the size is past in as a const int, and a destructor. You should also implement a copy constructor, an assignment operator (=), and overload the << operator to represent a pop operation to standard output. The size of the stack will be fixed upon definition (call of the constructor) and cannot be changed just to simplify the implementation More discussion on this later.
1. You are to make a copy of the cpp and stack.h files attached to this assignments. You will need to add additional code to these files and create your own makefile.
2. With templates you do not produce two file ( .h and .cpp). Instead the class template definition and implementation can be contained in one file. In this case h.
3. I would suggest you comment out any member function that you have not implemented in the main routine to help in testing.
4. All of the member function implementations (full, empty, top, push, pop, number, and size) will go in the h file. Note: There is no test for the Integer type.
5. Each type (char, float, double, and bool) will use the same ostream << operator overload.
6. Member Function requirements:
Make a copy of the following code and add the implementation of the Stack Template to the bottom. Do not modify any of the existing code.. just add to it. Name the file stack.h.
// Created by on.
// ********************************************************************
// * Name: Stack Class *
// * Description: A template Stack class designed to be used with *
// * any simple C++ type to include floating point, double *
// * floating point, character, and Boolean. *
// * The stack pointer, as is the convention, points to the last *
// * or highest element in the array. So if you allocate 100 *
// * elements of type "T" then the first empty element will be 99. *
// * Author: *
// * Date: *
// ********************************************************************
#include < iostream>
using namespace std;
const int DEFAULTSIZE=100;
template < class T>
class Stack {
public:
Stack(); // Default Constructor, stack is size 100.
Stack(const int size); // Constructor, creates stack of size "size"
Stack(const T & item); // Copy constructor
bool Full(); // Return true if the stack is full
bool Empty(); // Return true if the stack is empty
int Size(); // Return the size of the stack
int Number(); // Return the number of elements stored
T Top(); // Returns the top element, does not pop it
bool Push (const T item); // Put an item on the stack
bool Pop(); // Pop an item off and display to std out
friend ostream &operator <<(ostream & os, Stack&s);
private:
T * _stack; // The "stack"
int _size; // The number of elements the stack can hold
int _top; // Points to the first empty node
int _number; // The number of elements in the stack.
};
Make a copy of the following code and add the implementation of the ostream << operation overload one for each of the types float, double, char, bool Name the file main.cpp. Do not modify any of the existing code.
#include < iostream>
#include < string>
#include "stack.h"
using namespace std;
int main(void) {
cout << "Assignment #0 Test Program" << endl;
Stack< char> Char10; // Test
Character Stack, Default constructor
Stack< float> Float5(5); // Test
Float Stack
Stack< double> Double8(8); // Test
Double Stack
Stack< bool> Bool2(2); // Test Bool
Stack
Bool2.Push(true); // Test
Push on Bool
Bool2.Push(false); // Test
Push on Bool
Bool2.Push(true); // Test error
on Push
cout << "Size of Bool2 is: "<< Bool2.Size()<< endl;
cout << “The number of elements in Bool2 is” “<< Bool2.Number()<< endl;
cout << "Top element of Bool2 is: " << Bool2.Top() << endl;
cout << "Size on Double8 should be 8: "<< Double8.Size()<< endl;
Char10.Push('A');
Char10.Push('B');
Char10.Push('C');
cout << "Test Pop on Char10, should produce a 'C': ";
Char10.Pop();
Char10.Push('C');
cout << "Test ostream overload on Char10, should be a 'C': ";
cout << Char10<< endl;
cout << "Test ostream overload on Char10, should be a 'B': ";
cout << Char10<< endl;
cout << "Test ostream overload on Char10, should be a 'A': ";
cout << Char10<< endl;
cout << "Test ostream overload on Char10, should be an error: ";
cout << Char10<< endl;
return 0;
}
Assignment #0 Test Program
Stack overflow!
Size of Bool2 is: 2
The number of elements in Bool2 is: 2
Top element of Bool2 is: 0
Size on Double8 should be 8: 8
Test Pop on Char10, should produce a 'C': C
Test ostream overload on Char10, should be a 'C': C
Test ostream overload on Char10, should be a 'B': B
Test ostream overload on Char10, should be a 'A': A
Test ostream overload on Char10, should be an error: Empty Stack