Question
< c ++> With a reference to the code provided below, In main enhance it so that you test Stack class for int type and
< c ++> With a reference to the code provided below, In main enhance it so that you test Stack class for inttype and Student type that are attached with this assignment. The required details are as follows.
- Each member function of the Stack class must be clearly tested for each data type, int and Student (16 tests = 8 member functions * 2 data types)
- From the output, the instructor must be able to clearly see what test is being done and how it is done.
- Do not interact with the user to receive inputs so that program testing time can be reduced.
- When you fill the stack with many keys or elements, use a loop for efficiency.
//main.cpp
#include
void stack_test_int(); void stack_test_double(); void stack_test_student();
int main() { //The 3 types to tests that need to be tested stack_test_int(); stack_test_double(); stack_test_student();
return 0; }
/ //tests need editing void stack_test_int() { dStack
// Stack empty test if (s1.isEmpty()) { std::cout << "s1 is empty at the beginning." << std::endl; } else { std::cout << "s1 must be empty. Something's wrong!" << std::endl; }
for (int i = 0; i < 5; i++) { s1.push(1 * (i + 1)); }
dStack
// pop() test: reverses the items std::cout << "S1: Expected: 5 4 3 2 1 -->" << std::endl; for (int i = 0; i < 5; i++) { std::cout << s1.top() << std::endl; s1.pop(); }
// Stack empty test if (s1.isEmpty()) { std::cout << "s1 is empty after five pop() calls." << std::endl; } else { std::cout << "s1 must be full. Something's wrong!" << std::endl; }
// StackEmptyException test try { std::cout << "One more pop when the stack is empty..." << std::endl; s1.pop(); } catch (dStack
dStack
void stack_test_double() { dStack
// Stack empty test if (s1.isEmpty()) { std::cout << "s1 is empty at the beginning." << std::endl; } else { std::cout << "s1 must be empty. Something's wrong!" << std::endl; }
for (int i = 0; i < 5; i++) { s1.push(1.1 * (i + 1)); }
dStack
// pop() test: reverses the items std::cout << "S1: Expected: 5.5 4.4 3.3 2.2 1.1 -->" << std::endl; for (int i = 0; i < 5; i++) { std::cout << s1.top() << std::endl; s1.pop(); }
// Stack empty test if (s1.isEmpty()) { std::cout << "s1 is empty after five pop() calls." << std::endl; } else { std::cout << "s1 must be full. Something's wrong!" << std::endl; }
// StackEmptyException test try { std::cout << "One more pop when the stack is empty..." << std::endl; s1.pop(); } catch (dStack
dStack
void stack_test_student() { dStack
//------------------------------------------------------------------------------------
//dNode.h
#ifndef DNODE_H
#define DNODE_H
template
class dNode {
public:
T data;
dNode
dNode(T);
virtual ~dNode(); //for later use of polymorphism, review the topic again
//declaring templated class dStack as friend
template
friend class dStack; // allows dStack for private member access
}; // constructor: create a new Node with d as data
template
dNode
{
this->data = data;
next = 0;
}
template
dNode
{
}
#endif
//-------------------------------------
//dStack.h
#ifndef DSTACK_H
#define DSTACK_H
#include
#include
#include "dNode.h"
using namespace std;
template
class dStack {
private:
dNode
public:
class StackEmptyException {
};
dStack();
dStack(const dStack& copy);
dStack
virtual ~dStack();
void push(T);
void pop();
T top();
bool isEmpty();
bool isFull();
};
#include
#include "dStack.h"
using namespace std;
// constructor: new stack is created. topNode is null.
template
dStack
{
topNode = 0;
}
//copy constructor
template
dStack
{
dNode
if (tempNode == NULL) { //same as !tempNode because NULL is 0 in C++
topNode = NULL;
}
else {
dNode
while (tempNode->next) {
tempNode = tempNode->next;
newNode->next = new dNode
newNode = newNode->next;
}
}
}
template
dStack
{
dStack localStack(original);
dNode
this->topNode = localStack.topNode;
localStack.topNode = tempNode;
return *this;
}
// destructor: free all the memory allocated for the stack
template
dStack
{
//taking a reference to the top node
dNode
//looping until all nodes are deallocated
while (discard != NULL) {
//displaying address of node being de allocatd
cout << "Node being destructed: " << discard << endl;
//storing next node in a variable
dNode
//deleting current node
delete discard;
//moving to the next node
discard = temp;
}
}
// push a data onto the stack
template
void dStack
{
try {
dNode
newNode->next = topNode;
topNode = newNode;
}
catch (bad_alloc& e) {
//e.what(): returns a char sequence for exception details
cout << "memory allocation exception: " << e.what() << endl;
exit(1);
}
}
// pop the data from the stack
template
void dStack
{
if (isEmpty())
throw StackEmptyException();
dNode
topNode = topNode->next;
delete discard;
}
// read the data on the top of the stack
template
T dStack
{
if (isEmpty())
throw StackEmptyException();
return topNode->data;
}
// is stack empty?
template
bool dStack
{
return (topNode == 0);
}
// is stack full?
template
bool dStack
{
return false; // never, unless memory is full
}
#endif
//-----
//student.h
#ifndef STUDENT_H #define STUDENT_H #include
class Student { private: float gpa; string ssn; public: void setGPA(float v); void setSSN(string s); void print();
class OutOfGPARange { }; // empty inner class definition under public class InvalidSSN { }; // empty inner class definition under public }; #endif
//-----------------------
//student.cpp
#include
void Student::setGPA(float g) { //assert(g >= 0.0 && g <= 4.0);
//create an instance of the OutOfGPARange class, called exception if (g < 0.0 || g > 4.0) throw OutOfGPARange(); gpa = g; } void Student::setSSN(string s) { const int SSN_LENGTH = 9; if (s.length() != SSN_LENGTH) throw InvalidSSN(); //Checking each character is a digit should be here //Otherwise, throw InvalidSSN(); ssn = s; } void Student::print() { cout << "gpa: " << gpa << endl; cout << "ssn: " << ssn << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started