C++ Potion Store

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

57 Replies - 1713 Views - Last Post: 08 February 2013 - 06:41 PM Rate Topic: -----

#31 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,559
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 05 February 2013 - 04:52 PM

I don't think Jim means that you should declare the queue class in the Apothecary class.

He means you should have an object/variable of the queue class/type within the Apothecary class.

eg

// like declaring an integer variable
int number;

// declaring a queue variable/instance/object
queue myqueuename;




.

This post has been edited by #define: 05 February 2013 - 07:46 PM

Was This Post Helpful? 1
  • +
  • -

#32 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 05 February 2013 - 07:17 PM

Im going to implement my stack of and get back to you
Was This Post Helpful? 0
  • +
  • -

#33 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 06 February 2013 - 09:02 PM

I am confused about how to implement my stack class. What should the data members be? What are the member functions associated with the stack? I am lost about how to tie the stack into the whole program.
Was This Post Helpful? 0
  • +
  • -

#34 rajdeep01  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 04-February 13

Re: C++ Potion Store

Posted 07 February 2013 - 01:18 AM

#include <iostream>
#include <cmath>
using namespace std;
void Shop(int);

int Gold = 10;

int HealthPotion = 0;
int ManaPotion = 0;

int Choice;
string Inventory[5];


int main(){



cout << "You are in the shop. What would you like to buy?";
cout << " You have " << Gold << " gold.";
cout << endl;
cout << "1\) Health potion: 2 gold.  2\) Mana Potion: 3 gold.";
cin >> Choice;
Shop(Choice);

cin.ignore();
cin.get();
return 0;
}

void Shop(int x){
 if(x==1){
   HealthPotion++;
   Gold = Gold-2;
   }
 if(x==2){
   ManaPotion++;
   Gold = Gold-3;
   }

}

This post has been edited by jimblumberg: 07 February 2013 - 07:24 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Was This Post Helpful? 0
  • +
  • -

#35 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,559
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 07 February 2013 - 01:37 AM

The stack is almost the same as the queue class except enqueue is called push and instead of dequeue pop takes a value from the end.

I notice that you don't seem to have an isEmpty function defined.


Stack (abstract data type)


Since the classes are similar you could have a base class which is inherited.

I will have a closer look at the queue class.
Was This Post Helpful? 0
  • +
  • -

#36 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 07 February 2013 - 11:55 AM

since there are only so many potions that can be on the shelf, should I use an array based implementation of the stack, or a dynamically allocated stack using linked list?
Was This Post Helpful? 0
  • +
  • -

#37 jimblumberg  Icon User is online

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 07 February 2013 - 12:30 PM

Either will work but remember that you can't really use a fixed stack size. You need to be able to set the size when you construct the class.

Jim
Was This Post Helpful? 0
  • +
  • -

#38 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 07 February 2013 - 12:54 PM

Jim , I suppose for this assignment the array would be better, and I can define a constant to use for the index like

const int index = 100;

arr[index]

right?

Then I can change the size by changing the constant
Was This Post Helpful? 0
  • +
  • -

#39 jimblumberg  Icon User is online

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 07 February 2013 - 02:11 PM

Quote

Then I can change the size by changing the constant

Not really, you need to change the size of the stack based on the constructor of your Apothecary class. The two parameters in your constructor govern the sizes of your queue and the stack.

I still recommend using an array in your stack and queue classes, but you will need to dynamically allocate the array used in the class. Similar to:
class tryit
{
   public:
      tryit(int Asize) : size(Asize), marray(new int[Asize]) {}
   private
      const int Max_size;
      int *marray;
};




Jim

This post has been edited by jimblumberg: 07 February 2013 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#40 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 07 February 2013 - 02:25 PM

ok, so this is my constructor :

from apothecary.h :

Apothecary(int queuelim, int stacklim);



I suppose that queuelim = MaxOrders and stacklim=MaxPotions?
Was This Post Helpful? 0
  • +
  • -

#41 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 07 February 2013 - 02:32 PM

This is what I have so far for my stack.h

#ifndef STACK_H
#define STACK_H
#include "potion.h"
#include "main.h"

class stack
{
    stack();
    stack(const stack& aStack);
    //~stack();
    
    const stack& operator=(const stack& aStack);




Am I on the right track?
Was This Post Helpful? 0
  • +
  • -

#42 jimblumberg  Icon User is online

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 07 February 2013 - 02:33 PM

Probably.

Jim
Was This Post Helpful? 0
  • +
  • -

#43 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 07 February 2013 - 04:25 PM

My book says that the copy constructor and the destructor are supplied by the compiler, so this is what I have done with my stack so far

#ifndef STACK_H
#define STACK_H
#include "apothecary.h"
#include "potion.h"
#include "main.h"
const int index = MaxPotions;
class stack
{
    public:
    stack();
    




Next, I was going to declare the member functions and then the private section with the array and a variable to hold the index of the top.
Was This Post Helpful? 0
  • +
  • -

#44 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 07 February 2013 - 05:18 PM

Am I doing this right?
Was This Post Helpful? 0
  • +
  • -

#45 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,559
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 07 February 2013 - 06:22 PM

I don't think this is quite right.

06	const int index = MaxPotions;



Each stack object could have a different maximum number of potions.

Firstly you could ask, what data will the stack class hold?


Classes (I)
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4