1. You are to make a copy of the main.cpp and stack.h files attached to this assignments. You will need to add additiona
Posted: Tue Jul 12, 2022 8:10 am
1. You are to make a copy ofthe main.cpp and stack.h filesattached to this assignments. You will need to add additional codeto the stack.h file andyou may modifythe main.cpp file if you feelyou need to perform additional testing. Do notalter the method names or signatures.
2. With templates you do not produce two files ( .h and .cpp).Instead the class template definition and implementation arecontained in one file. In thiscase stack.h.
3. I would suggest you comment out any member function that youhave not implemented in the main routine to help in testing.
4. All of the member function implementations (full, empty, top,push, pop, size, constructors, assignment overload, and ostreamoutput overload) will go in the stack.h file after the templatedefinition. Note there is no Integer type.
5. You should write one << ostream overload operator tohandle all the output.
Member Function Requirements
Operational Objective
You will find attached to this assignment find attacheda main.cpp and stack.h file.
Modify the stack.h file toinclude your implementation of the template methods. DO NOTCHANGE ANY OF THE NAMES OR SIGNATURE OF THE METHODS. Test your class with the attached main.cppfile.
Additional Information: You can usethe <vector> Standard Template Library if you wish.
Submission:
#ifndef STACK_H#define STACK_H//********************************************************************// * Name: Stack Class *// * Description: A template Stack class designed tobe used with *// * any simple C++ type to include floating point,double *// * floating point, character, and boolan. *// * The stack pointer, as is the convention, pointsto the last *// * or highest element in the array. So if youallocate 100 *// * elements of type "T" then the first emptyelement will be 99. * *//********************************************************************#include <iostream>using namespace std;const int DEFAULTSIZE=100;template <class T>class Stack {public: Stack(); // Default Constructor, stack issize 100. ~Stack(); // Destructor Stack(const int size); //Constructor, creates stack of size "size" Stack(const Stack<T> & item);//Copy constructor bool Full(); // Return true if the stack isfull bool Empty(); // Return true if the stack isempty int Size(); // Return the size of thestack T Top(); // Returns the top element, doesnot pop it. bool Push (const T item); // Put anitem on the stack. bool Pop(); // Pop an item off and display to stdout friend ostream &operator <<(ostream& os, Stack<T> &s) { // Your code for the ostreamoperator << overload goes here. } Stack & operator = ( const Stack<T>s);private: T * _stack; // The "stack" int _size; // The number of elements the stack canhold int _top; // Points to the first emptynode int _maxsize; // Added to allow you to keep track ofthe size you allocated. T dummy; // Something to return incase there was an error on the Top()};
// Your code for the implementation of the methods goeshere
#endif //STACK_H
//main.cpp
#include <iostream>#include "stack.cpp"using namespace std;int main(void){cout << "Assignment #7 Test Program" << endl;Stack<char> Char10; // Test Character Stack, DefaultconstructorStack<float> Float5 (5); // Test Float StackStack<double> Double8(8); // Test Double StackStack<bool> Bool2(2); // Test Bool StackStack<char> Charcy(Char10); // Default copy constructorBool2.Push(true); // Test Push on BoolBool2.Push(false); // Test Push on BoolBool2.Push(true); // Test error on Pushcout << "Size of Bool2 is: "<<Bool2.Size()<<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 Charcy, should be Empty:";cout << Charcy<<endl;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;}
2. With templates you do not produce two files ( .h and .cpp).Instead the class template definition and implementation arecontained in one file. In thiscase stack.h.
3. I would suggest you comment out any member function that youhave not implemented in the main routine to help in testing.
4. All of the member function implementations (full, empty, top,push, pop, size, constructors, assignment overload, and ostreamoutput overload) will go in the stack.h file after the templatedefinition. Note there is no Integer type.
5. You should write one << ostream overload operator tohandle all the output.
Member Function Requirements
Operational Objective
You will find attached to this assignment find attacheda main.cpp and stack.h file.
Modify the stack.h file toinclude your implementation of the template methods. DO NOTCHANGE ANY OF THE NAMES OR SIGNATURE OF THE METHODS. Test your class with the attached main.cppfile.
Additional Information: You can usethe <vector> Standard Template Library if you wish.
Submission:
#ifndef STACK_H#define STACK_H//********************************************************************// * Name: Stack Class *// * Description: A template Stack class designed tobe used with *// * any simple C++ type to include floating point,double *// * floating point, character, and boolan. *// * The stack pointer, as is the convention, pointsto the last *// * or highest element in the array. So if youallocate 100 *// * elements of type "T" then the first emptyelement will be 99. * *//********************************************************************#include <iostream>using namespace std;const int DEFAULTSIZE=100;template <class T>class Stack {public: Stack(); // Default Constructor, stack issize 100. ~Stack(); // Destructor Stack(const int size); //Constructor, creates stack of size "size" Stack(const Stack<T> & item);//Copy constructor bool Full(); // Return true if the stack isfull bool Empty(); // Return true if the stack isempty int Size(); // Return the size of thestack T Top(); // Returns the top element, doesnot pop it. bool Push (const T item); // Put anitem on the stack. bool Pop(); // Pop an item off and display to stdout friend ostream &operator <<(ostream& os, Stack<T> &s) { // Your code for the ostreamoperator << overload goes here. } Stack & operator = ( const Stack<T>s);private: T * _stack; // The "stack" int _size; // The number of elements the stack canhold int _top; // Points to the first emptynode int _maxsize; // Added to allow you to keep track ofthe size you allocated. T dummy; // Something to return incase there was an error on the Top()};
// Your code for the implementation of the methods goeshere
#endif //STACK_H
//main.cpp
#include <iostream>#include "stack.cpp"using namespace std;int main(void){cout << "Assignment #7 Test Program" << endl;Stack<char> Char10; // Test Character Stack, DefaultconstructorStack<float> Float5 (5); // Test Float StackStack<double> Double8(8); // Test Double StackStack<bool> Bool2(2); // Test Bool StackStack<char> Charcy(Char10); // Default copy constructorBool2.Push(true); // Test Push on BoolBool2.Push(false); // Test Push on BoolBool2.Push(true); // Test error on Pushcout << "Size of Bool2 is: "<<Bool2.Size()<<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 Charcy, should be Empty:";cout << Charcy<<endl;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;}