Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

< 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.

  1. 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)
  2. From the output, the instructor must be able to clearly see what test is being done and how it is done.
  3. Do not interact with the user to receive inputs so that program testing time can be reduced.
  4. When you fill the stack with many keys or elements, use a loop for efficiency.

//main.cpp

#include #include "dStack.h" #include "Student.h"

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

// 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 s2(s1); std::cout << "S2: Expected: 5 4 3 2 1 -->" << std::endl; for (int i = 0; i < 5; i++) { std::cout << s2.top() << std::endl; s2.pop(); }

// 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::StackEmptyException) { std::cout << "Exception: cannot pop, stack is empty" << std::endl; }

dStack s3; s3 = s1; for (int i = 0; i < 5; i++) { s3.push(1 * (i + 1)); } }

void stack_test_double() { dStack s1;

// 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 s2(s1); std::cout << "S2: Expected: 5.5 4.4 3.3 2.2 1.1 -->" << std::endl; for (int i = 0; i < 5; i++) { std::cout << s2.top() << std::endl; s2.pop(); }

// 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::StackEmptyException) { std::cout << "Exception: cannot pop, stack is empty" << std::endl; }

dStack s3; s3 = s1; for (int i = 0; i < 5; i++) { s3.push(1.1 * (i + 1)); } }

void stack_test_student() { dStack s1; }

//------------------------------------------------------------------------------------

//dNode.h

#ifndef DNODE_H

#define DNODE_H

template

class dNode {

public:

T data;

dNode* next;

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::dNode(T data)

{

this->data = data;

next = 0;

}

template

dNode::~dNode()

{

}

#endif

//-------------------------------------

//dStack.h

#ifndef DSTACK_H

#define DSTACK_H

#include

#include

#include "dNode.h"

using namespace std;

template

class dStack {

private:

dNode* topNode;

public:

class StackEmptyException {

};

dStack();

dStack(const dStack& copy);

dStack& operator=(const dStack&);

virtual ~dStack();

void push(T);

void pop();

T top();

bool isEmpty();

bool isFull();

};

#include //for exit()

#include "dStack.h"

using namespace std;

// constructor: new stack is created. topNode is null.

template

dStack::dStack()

{

topNode = 0;

}

//copy constructor

template

dStack::dStack(const dStack& copy)

{

dNode* tempNode = copy.topNode;

if (tempNode == NULL) { //same as !tempNode because NULL is 0 in C++

topNode = NULL;

}

else {

dNode* newNode = topNode = new dNode(tempNode->data);

while (tempNode->next) {

tempNode = tempNode->next;

newNode->next = new dNode(tempNode->data);

newNode = newNode->next;

}

}

}

template

dStack& dStack::operator=(const dStack& original)

{

dStack localStack(original);

dNode* tempNode = this->topNode;

this->topNode = localStack.topNode;

localStack.topNode = tempNode;

return *this;

}

// destructor: free all the memory allocated for the stack

template

dStack::~dStack()

{

//taking a reference to the top node

dNode* discard = topNode;

//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* temp = discard->next;

//deleting current node

delete discard;

//moving to the next node

discard = temp;

}

}

// push a data onto the stack

template

void dStack::push(T data)

{

try {

dNode* newNode = new dNode(data);

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::pop()

{

if (isEmpty())

throw StackEmptyException();

dNode* discard = topNode;

topNode = topNode->next;

delete discard;

}

// read the data on the top of the stack

template

T dStack::top()

{

if (isEmpty())

throw StackEmptyException();

return topNode->data;

}

// is stack empty?

template

bool dStack::isEmpty()

{

return (topNode == 0);

}

// is stack full?

template

bool dStack::isFull()

{

return false; // never, unless memory is full

}

#endif

//-----

//student.h

#ifndef STUDENT_H #define STUDENT_H #include using namespace std;

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 #include "Student.h"

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Strategic Database Technology Management For The Year 2000

Authors: Alan Simon

1st Edition

155860264X, 978-1558602649

More Books

Students also viewed these Databases questions

Question

How can you use sentence style to emphasize key thoughts? [LO-5]

Answered: 1 week ago

Question

1. Always guess when only right answers are scored.

Answered: 1 week ago

Question

Distinguish between poor and good positive and neutral messages.

Answered: 1 week ago

Question

Describe the four specific guidelines for using the direct plan.

Answered: 1 week ago