C++ Potion Store

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

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

#46 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 - 07:38 PM

Ok, I don't understand what data should be in my stack class, I know that the member functions should be public, but not sure how to proceed with my stack class
Was This Post Helpful? 0
  • +
  • -

#47 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 07 February 2013 - 08:16 PM

The stack class is going to hold a shelf of Potions.

You said you are going to use an array to hold the Potions on the shelf.

The shelf is going to hold a maximum number of potions.

There is going to be a specific number/count of potions on the shelf at any one time.

So that is three pieces of data so far.


Public means data/functions can be accessed from outside the class object, private means they cannot. Usually data is made private and functions are public, if in doubt, just now, make everything public.
Was This Post Helpful? 0
  • +
  • -

#48 jimblumberg  Icon User is online

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 07 February 2013 - 09:42 PM

Your stack class could look something like:
class Stack
{
   public:
      Stack();
      Stack(int max_size);
      void pop();
      void push(const Potion& potion);
      Potion top();

      int size();
      int capacity();
      bool empty();
   private:
      const int Max_size;
      int current_size;
      Potion *data;
};


By the way what is the course title of the class you are taking?

Jim

This post has been edited by jimblumberg: 07 February 2013 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -

#49 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 PM

Jim, its called data structures and is a 200very level course.

Im signing off for the day. Ill take a stab at this stack in the morning
Was This Post Helpful? 0
  • +
  • -

#50 sydewayzlocc  Icon User is offline

  • D.I.C Head

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

Re: C++ Potion Store

Posted 08 February 2013 - 04:24 PM

I have been working on implementing a .cpp for the stack.h file, here is what I have come up with, and Im not sure if it is correct. Specifically, I declared an array in the header and am trying to use it in the .cpp for push and pop, with the top variable, I need some feedback on my .cpp file to see if I am doing this correctly

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

class stack
{
    public:
        Stack();
        Stack(int max_size);
        void pop();
        void push(const Potion& potion);
        Potion top();
        
        int size();
        int capacity();
        bool empty();
    private:
        const int max_size;
        int current_size;
        Potion potion[max_size];
        Potion *data;
};

#endif




stack.cpp

#include "stack.h"
#include <iostream>

Stack::Stack() : top(-1)
{
}

bool Stack::empty() const
{
    return top < 0;
}

void Stack::push(const Potion& potion)
{
    if (top >= max_size-1)
        cout <<"Shelf Full"<<endl;
        break;
    else    
        {
            ++top;
            potion[top]=potion;
        }
}

void Stack::pop()
{
    if (empty())
        cout<<"Shelf empty, no potions"
    else
    
        --top;
}


Was This Post Helpful? 0
  • +
  • -

#51 jimblumberg  Icon User is online

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 08 February 2013 - 04:32 PM

You need to initialize your const int and your constructor and delete this allocated memory in your destructor.

Jim
Was This Post Helpful? 0
  • +
  • -

#52 sydewayzlocc  Icon User is offline

  • D.I.C Head

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

Re: C++ Potion Store

Posted 08 February 2013 - 04:47 PM

Jim, I understand initializing my const int, does that mean giving it a value? And I don't get what you mean by my constructor and deleting my allocated memory in my destructor. Can you show me what you mean?
Was This Post Helpful? 0
  • +
  • -

#53 jimblumberg  Icon User is online

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 08 February 2013 - 04:57 PM

Quote

Jim, I understand initializing my const int, does that mean giving it a value?

Yes.

Quote

And I don't get what you mean by my constructor

You already have an example, your own constructor:
Stack::Stack() : top(-1)
{
}


If you are asking these basic C++ questions I really wonder whether you are really ready for a C++ Data Structures class. Maybe you missed some of the pre-requsites for this course.

Jim

This post has been edited by jimblumberg: 08 February 2013 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#54 sydewayzlocc  Icon User is offline

  • D.I.C Head

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

Re: C++ Potion Store

Posted 08 February 2013 - 05:04 PM

Maybe Im not ready for it, but at least I am trying
Was This Post Helpful? 0
  • +
  • -

#55 sydewayzlocc  Icon User is offline

  • D.I.C Head

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

Re: C++ Potion Store

Posted 08 February 2013 - 05:12 PM

What else needs to be initialized in my constructor?

Also, my book said that the compiler generated destructor is sufficient
Was This Post Helpful? 0
  • +
  • -

#56 jimblumberg  Icon User is online

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 08 February 2013 - 05:14 PM

But you need to fill the missing information, quickly! You need to bone up on the basics, it is almost impossible to jump into these advanced topics if you don't understand the basics. To learn to program you follow a step by step process each step builds on upon the preceding step.

I'm not saying you can't accomplish your goals but you have surely made it harder on yourself.

Quote

What else needs to be initialized in my constructor?

All the class member variables need to be initialized, this is the purpose of the constructor.

Quote

Also, my book said that the compiler generated destructor is sufficient

Then your book is not using dynamic memory allocation.

Jim

This post has been edited by jimblumberg: 08 February 2013 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#57 sydewayzlocc  Icon User is offline

  • D.I.C Head

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

Re: C++ Potion Store

Posted 08 February 2013 - 05:23 PM

I took every class before this and passed, I don't know what I am missing. Maybe I should just give up.
Was This Post Helpful? 0
  • +
  • -

#58 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 08 February 2013 - 06:41 PM

Perhaps you should scan/speed read through a tutorial or book, up to your current level, taking a note of what you do and don't understand. Then you can read and learn about what you don't know. It is usually better to read through several tutorials and books, to pick up knowledge.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4