// Specification file for the InvItem class #ifndef INVITEM_H #define INVITEM_H
ID: 3635924 • Letter: #
Question
// Specification file for the InvItem class#ifndef INVITEM_H
#define INVITEM_H
#include <string>
using namespace std;
class InventoryItem
{
private:
long serialNum; //integer variable for serial number
string manufactDate; //member that holds the date for part manfactured
int lotNum; //integer hold the lot number
public:
// Constructor
InventoryItem(long serialNum, string manufactDate);
// define mutators here
void setSerialNum(long serial)
{ serialNum = serial; }
void setManufactDate (string mDate)
{ manufactDate = mDate; }
// define accessors here
long getSerialNum() const
{ return serialNum; }
string getManufactDate()
{ return manufactDate; }
};
#endif
#include <iostream>
#include "InvItem.h"
#include "DynStack.h"
using namespace std;
int main()
{
DynStack<InventoryItem> stack; // create stack
InventoryItem item(); // create inventory item object
int choice; // Menu choice
long serial; // Serial number
string mDate; // Manufacture date
do
{
// Display the menu.
cout << " ------ Inventory Menu -------- ";
cout << "1. Enter a part into the inventory. ";
cout << "2. Take a part from the inventory. ";
cout << "3. Quit. ";
cout << "Please make a choice (1, 2, or 3): ";
cin >> choice;
// Validate choice
while (choice < 1 || choice > 3)
{
cout << "Please enter 1, 2, or 3: ";
cin >> choice;
}
// Act on the user's choice.
switch(choice)
{
case 1:
// Enter a part into inventory.
cout << " You have chosen to add an item to the inventory bin. ";
cout << "Enter the item's serial number: ";
cin >> serial;
item.setSerialNum(serial);
cout << "Enter the item's manufacture date: ";
cin >> mDate;
item.setManufactDate(mDate);
stack.push(item);
break;
case 2:
// Take a part out of inventory.
cout << " You have chosen to remove an item from the inventory bin. ";
if (stack.isEmpty())
cout << "No parts to remove. ";
else
{
stack.pop(item);
cout << " The part you removed was: ";
cout << " Serial number: " << item.getSerialNum() << endl;
cout << " Manufacture date: " << item.getManufactDate() << endl;
cout << endl;
}
break;
case 3:
while (!stack.isEmpty())
{
stack.pop(item);
cout << " The part was left in inventory: ";
cout << " Serial number: " << item.getSerialNum() << endl;
cout << " Manufacture date: " << item.getManufactDate() << endl;
cout << endl;
}
cout << "Goodbye! ";
break;
}
} while (choice != 3);
return 0;
}
// Dynamic stack class that can hold objects of any class.
// DynStack template
template <class T>
class DynStack
{
private:
struct StackNode
{
T value;
StackNode *next;
};
StackNode *top;
public:
DynStack()
{ top = NULL; }
void push(T);
void pop(T &);
bool isEmpty();
};
//*********************************************************
// Member function push pushes the argument onto *
// the stack. *
//*********************************************************
template <class T>
void DynStack<T>::push(T num)
{
StackNode *newNode; //pointer to new node
//Allocate a new node and store num there
newNode = new StackNode;
newNode->value = num;
//If there are no nodes in the list, make the newNode first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else //otherwise, insert newNode before top
{
newNode->next = top;
top =newNode;
}
}
//*********************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//*********************************************************
template <class T>
void DynStack<T>::pop(T &num)
{
StackNode *temp; //temporary point
//make sure the stack isn't empty
if (isEmpty())
{
cout << "The stack is empty. ";
}
else //pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
//*********************************************************
// Member funciton isEmpty returns true if the stack *
// is empty, or false otherwise. *
//*********************************************************
template <class T>
bool DynStack<T>::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
Explanation / Answer
Here, this code compiles, the error was in initialising an object of the class InventoryItem item(); where no such constructor is available. The constructor provided required 2 arguments, however that constructor was only declared. So I just defined the default empty constructor, with him the object initialising is alright, and program compiles in VS2010. #ifndef INVITEM_H #define INVITEM_H #include #include using namespace std; // Dynamic stack class that can hold objects of any class. // DynStack templatea template class DynStack { private: struct StackNode { T value; StackNode *next; }; StackNode *top; public: DynStack(){ top = NULL; } void push(T); void pop(T &); bool isEmpty(); }; class InventoryItem { private: long serialNum; //integer variable for serial number string manufactDate; //member that holds the date for part manfactured int lotNum; //integer hold the lot number public: //InventoryItem(long serialNum, string manufactDate); InventoryItem(){ } void setSerialNum(long serial){ serialNum = serial; } void setManufactDate (string mDate){ manufactDate = mDate; } // define accessors here long getSerialNum() const { return serialNum; } string getManufactDate() { return manufactDate; } }; #endif int main() { DynStack stack; // create stack InventoryItem item; // create inventory item object int choice; // Menu choice long serial; // Serial number string mDate; // Manufacture date do { // Display the menu. cout next; delete top; top = temp; } } //********************************************************* // Member funciton isEmpty returns true if the stack * // is empty, or false otherwise. * //********************************************************* template bool DynStack::isEmpty() { bool status; if (!top) status = true; else status = false; return status; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.