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
57 Replies - 1033 Views - Last Post: 08 February 2013 - 06:41 PM
#47
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.
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.
#48
Re: C++ Potion Store
Posted 07 February 2013 - 09:42 PM
Your stack class could look something like:
By the way what is the course title of the class you are taking?
Jim
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
#49
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
Im signing off for the day. Ill take a stab at this stack in the morning
#50
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 :
stack.cpp
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;
}
#51
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
Jim
#52
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?
#53
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
#54
Re: C++ Potion Store
Posted 08 February 2013 - 05:04 PM
Maybe Im not ready for it, but at least I am trying
#55
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
Also, my book said that the compiler generated destructor is sufficient
#56
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.
All the class member variables need to be initialized, this is the purpose of the constructor.
Then your book is not using dynamic memory allocation.
Jim
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
#57
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.
#58
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.
|
|

New Topic/Question
Reply




MultiQuote



|