57 Replies - 1046 Views - Last Post: 08 February 2013 - 06:41 PM
#16
Re: C++ Potion Store
Posted 04 February 2013 - 09:43 PM
Jim
#17
Re: C++ Potion Store
Posted 04 February 2013 - 09:51 PM
#18
Re: C++ Potion Store
Posted 04 February 2013 - 10:37 PM
sydewayzlocc, on 05 February 2013 - 06:51 AM, said:
The stack is almost the same as the queue. The main difference is in the stack the last object is removed (LIFO) and in a queue the first (oldest) object is removed (FIFO). The stack's functions are usually called push and pop.
I would remove the queue implementation functions from Apothecary.cpp and put them in queue.cpp.
Your main.cpp with its functions looks fine. The functions are similarly named to some of the member functions.
#19
Re: C++ Potion Store
Posted 04 February 2013 - 11:14 PM
queue.cpp :
#include "apothecary.h"
#include "assert.h"
#include "queue.h"
#include <iomanip>
queue::queue()
{
front = NULL;
rear = NULL;
}
queue::queue(const queue& aQueue):front(NULL), rear(NULL)
{
if(aQueue.front == NULL)
{
front = rear = NULL;
}
else
{
//copy first node
front = new node;
assert(front != NULL); //check allocation
front->item = aQueue.front->item;
//copy the rest of the queue
node * destNode = front; //points to the last node in new queue
node * srcNode = aQueue.front->next; //points to node in aQueue
while(srcNode != NULL) //or while (srcNode)
{
destNode->next = new node;
assert(destNode->next != NULL); //check allocation
destNode = destNode->next;
destNode->item = srcNode->item;
srcNode = srcNode->next;
}
destNode->next = NULL;
//set rear pointr
rear = destNode;
}
}
const queue& queue::operator= (const queue& aQueue)
{
if(this == &aQueue)
return *this;
else
{
//release dynamically allocated memory held by current object
node * curr = front;
while(front)
{
curr = front->next;
delete front;
front = curr;
}
//make a deep copy of aQueue
if(aQueue.front == NULL)
{
front = rear = NULL;
}
else
{
//copy first node
front = new node;
assert(front != NULL); //check allocation
front->item = aQueue.front->item;
//copy the rest of the queue
node * destNode = front; //points to the last node in new queue
node * srcNode = aQueue.front->next; //points to node in aQueue
while(srcNode != NULL) //or while (srcNode)
{
destNode->next = new node;
assert(destNode->next != NULL); //check allocation
destNode = destNode->next;
destNode->item = srcNode->item;
srcNode = srcNode->next;
}
destNode->next = NULL;
//set rear pointr
rear = destNode;
}
return *this;
}
}
bool queue::enqueue(const Potion& aPotion)
{
//add to the rear
node * newNode = new node;
newNode->item = aPotion;
newNode->next = NULL;
if(front == NULL)
{
front = newNode;
}
else
{
rear->next = newNode;
rear = newNode;
}
//rear = newNode;
return true;
}
bool queue::dequeue()
{
//empty stack, has nothing to pop
if(front == NULL)
{
cout << "Queue is empty." << endl;
}
else
{
node * temp = front;
if(front == rear) //the only node
front = rear = NULL;
else
front = front->next;
temp->next = NULL;
delete temp;
}
return true;
}
apothecary.cpp
#include "apothecary.h"
#include "assert.h"
#include "queue.h"
#include <iomanip>
#include <iostream>
using namespace std;
// default constructor
Apothecary::Apothecary():MaxOrders(0), MaxPotions(0), nOrders(0), nPotions(0)
{}
Apothecary::Apothecary(int orders, int potions)
{
MaxOrders = orders;
MaxPotions = potions;
nOrders = 0;
nPotions = 0;
}
int Apothecary::MakePotions()
{
queue Qd;
Qd.dequeue();
nOrders--;
return nOrders;
}
bool Apothecary::OrderPotion(PotionType potion)
{
queue Q;
if(nOrders < MaxOrders)
{
if(Q.enqueue(Potion(potion)))
{
nOrders++;
return true;
}
else
{
cout << "enq failed" << endl;
return false;
}
}
else
{
cout << "Queue is full!" << endl;
return false;
}
}
bool Apothecary::BuyPotion(Potion& potion)
{
queue * shelf;
shelf = new stack(sizeof(shelf));
if(shelf->isEmpty())
return false;
else
shelf->pop(potion);
return true;
}
My main problem still is with the BuyPotion Function.
#20
Re: C++ Potion Store
Posted 04 February 2013 - 11:35 PM
Quote
The Potion class contains the details about a potion. Your potion class should have a private data member to store the potion type for a created potion. The Potion class needs to have a GetType() method that returns the potion type for a given potion. You also need a default value constructor. You are free to add any other methods that you need.
Your instructions say that your potion class should have a data member to store the potion type, and GetType() should return a potion type.
The potion type is an enum.
enum PotionType {UNKNOWN, SPEED, STRENGTH, HEALTH, WISDOM};
#ifndef POTION_H
#define POTION_H
#include "main.h"
class Potion
{
private:
char* potionType;
public:
Potion();
Potion(PotionType potion);
//~Potion();
void SetType(char* type);
const char* const GetType();
const Potion& operator=(const Potion& student); //overloading assignment operator
};
#endif
Notice you are incorrectly declaring the potion type as a char*.
#21
Re: C++ Potion Store
Posted 04 February 2013 - 11:48 PM
enum Potion::GetType()
{
return potionType;
}
potion.h :
#ifndef POTION_H
#define POTION_H
#include "main.h"
class Potion
{
private:
enum potionType;
public:
Potion();
Potion(PotionType potion);
~Potion();
void SetType(char* type);
enum GetType();
const Potion& operator=(const Potion& student); //overloading assignment operator
};
#endif
After recompiling I get a couple more errors regarding the change
Errors
potion.h|8|error: use of enum ‘potionType’ without previous declaration| potion.h|16|error: use of enum ‘GetType’ without previous declaration| potion.h|16|error: expected unqualified-id before ‘)’ token| apothecary.cpp||In member function ‘bool Apothecary::BuyPotion(Potion&)’:| apothecary.cpp|72|error: expected type-specifier before ‘stack’| apothecary.cpp|72|error: cannot convert ‘int*’ to ‘queue*’ in assignment| apothecary.cpp|72|error: expected ‘;’ before ‘stack’| apothecary.cpp|82|error: ‘class queue’ has no member named ‘pop’| ||=== Build finished: 7 errors, 0 warnings ===|
#22
Re: C++ Potion Store
Posted 05 February 2013 - 12:05 AM
To create a variable of PotionType you can do :
08 PotionType potionType;
#23
Re: C++ Potion Store
Posted 05 February 2013 - 02:48 PM
potion.h
#ifndef POTION_H
#define POTION_H
#include "main.h"
class Potion
{
private:
PotionType potionType;
public:
Potion();
Potion(PotionType potion);
~Potion();
void SetType(char* type);
enum GetType();
const Potion& operator=(const Potion& student); //overloading assignment operator
};
#endif
Now I am getting an error regarding the GetType function.
potion.h|16|error: use of enum ‘GetType’ without previous declaration|
#24
Re: C++ Potion Store
Posted 05 February 2013 - 03:12 PM
PotionType GetType();
This function will be returning a PotionType. PotionType is a type of variable that was defined in main.h.
I also recommend using a PotionType in your SetType() function:
void SetType(PotionType type);
Jim
#25
Re: C++ Potion Store
Posted 05 February 2013 - 03:28 PM
#26
Re: C++ Potion Store
Posted 05 February 2013 - 03:34 PM
Quote
How are you creating your stack? Please show the current code for your Apothecary class and the code for both your queue and your stack.
Jim
#27
Re: C++ Potion Store
Posted 05 February 2013 - 03:50 PM
apothecary.h :
#ifndef APOTHECARY_H
#define APOTHECARY_H
#include "main.h"
#include "potion.h"
#include "queue.h"
class Apothecary
{
private:
int MaxOrders;
int MaxPotions;
int nOrders;
int nPotions;
public:
Apothecary();
Apothecary(int queuelim, int stacklim);
bool OrderPotion(PotionType potion);
int MakePotions();
bool BuyPotion(Potion& potion);
};
#endif
queue.h:
#ifndef QUEUE_H
#define QUEUE_H
#include "potion.h"
#include "main.h"
class queue
{
public:
queue();
queue(const queue& aQueue);
~queue();
const queue& operator= (const queue& aQueue);
bool enqueue(const Potion&);
bool dequeue();
void Display();
bool peek(Potion&)const;
bool isEmpty(void)const;
struct node
{
Potion item;
node * next;
};
node * front;
node * rear;
};
#endif
queue.cpp :
#include "apothecary.h"
#include "assert.h"
#include "queue.h"
#include <iomanip>
queue::queue()
{
front = NULL;
rear = NULL;
}
queue::queue(const queue& aQueue):front(NULL), rear(NULL)
{
if(aQueue.front == NULL)
{
front = rear = NULL;
}
else
{
//copy first node
front = new node;
assert(front != NULL); //check allocation
front->item = aQueue.front->item;
//copy the rest of the queue
node * destNode = front; //points to the last node in new queue
node * srcNode = aQueue.front->next; //points to node in aQueue
while(srcNode != NULL) //or while (srcNode)
{
destNode->next = new node;
assert(destNode->next != NULL); //check allocation
destNode = destNode->next;
destNode->item = srcNode->item;
srcNode = srcNode->next;
}
destNode->next = NULL;
//set rear pointr
rear = destNode;
}
}
const queue& queue::operator= (const queue& aQueue)
{
if(this == &aQueue)
return *this;
else
{
//release dynamically allocated memory held by current object
node * curr = front;
while(front)
{
curr = front->next;
delete front;
front = curr;
}
//make a deep copy of aQueue
if(aQueue.front == NULL)
{
front = rear = NULL;
}
else
{
//copy first node
front = new node;
assert(front != NULL); //check allocation
front->item = aQueue.front->item;
//copy the rest of the queue
node * destNode = front; //points to the last node in new queue
node * srcNode = aQueue.front->next; //points to node in aQueue
while(srcNode != NULL) //or while (srcNode)
{
destNode->next = new node;
assert(destNode->next != NULL); //check allocation
destNode = destNode->next;
destNode->item = srcNode->item;
srcNode = srcNode->next;
}
destNode->next = NULL;
//set rear pointr
rear = destNode;
}
return *this;
}
}
bool queue::enqueue(const Potion& aPotion)
{
//add to the rear
node * newNode = new node;
newNode->item = aPotion;
newNode->next = NULL;
if(front == NULL)
{
front = newNode;
}
else
{
rear->next = newNode;
rear = newNode;
}
//rear = newNode;
return true;
}
bool queue::dequeue()
{
//empty stack, has nothing to pop
if(front == NULL)
{
cout << "Queue is empty." << endl;
}
else
{
node * temp = front;
if(front == rear) //the only node
front = rear = NULL;
else
front = front->next;
temp->next = NULL;
delete temp;
}
return true;
}
#28
Re: C++ Potion Store
Posted 05 February 2013 - 04:35 PM
Quote
No the Apothecary class is not a stack. A stack is a dynamic data structure, Apothecary is not a dynamic data structure.
Quote
So what do these variables represent?
Quote
Where is your queue class declared, it should be declared inside your Apothecary class.
The Apothecary class represents the "store" where your potions are stored and the orders are processed. You should have an instance of your queue class in your Apothecary class to hold the orders waiting to be processed. Then you will need an instance of a stack class to hold the information about the potions that are on the shelf.
Jim
#29
Re: C++ Potion Store
Posted 05 February 2013 - 04:44 PM
#30
Re: C++ Potion Store
Posted 05 February 2013 - 04:47 PM
|
|

New Topic/Question
Reply




MultiQuote



|