Assignment Description

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.

  • Distinguish between a function template and a concrete function: definition, implementation, and file structure conventions
  • Demonstrate correct notation and syntax for defining and implementing a function template
  • Use a friend to create an overload operator for the << operator
  • State the rules compilers use to instantiate a function template
  • Test a function template for correct syntax
  • Test a function template for correct behavior
  • Test a function template for use with multiple types.
  • Test the overload << operations.
  • Test the overload << operations.

Detailed Instructions

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:

  • Full - returns true if the Stack is full, false otherwise.
  • Empty - return true if the Stack is empty, false otherwise
  • Top - returns the top element but does not remove it from the Stack. (Note: It returns a copy to the calling routine but does not print it out)
  • Push - puts an element on the Stack, Issues an error to standard output a message if you try to Push an element on a full stack.
  • Pop - prints the top element from the Stack and removes it. Prints an error message if the stack is empty before the Pop operation.
  • Number - Returns the number of elements stored in the stack.
  • Size - Returns the Size of the Stack ( what was allocated ).
  • << - ostream overload, has the same effect as Pop but the error message is returned instead of the top element. Returns an error message if the Stack is empty.
  • The default constructor which allocates 100 elements.
  • The constructor with one parameter allocates the number of elements indicated by the integer passed in. If the integer is less than zero, the default value is used.

Operational Objectives

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;
}

Sample Output

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
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.