I'm not trying to be harsh, but this needs a lot of work.
Line 12 is not legal C++ syntax. If you want an array of variable size, you need to use a pointer, dynamic memory management functions (operator new[] and operator delete[]), a proper destructor, and variables to track capacity and usage.
const int defaultArraySize = 10;
class Sample {
private:
int *myArray;
int ArrayCapacity;
int ArrayUsed;
public:
Sample():ArrayCapacity(defaultArraySize), ArrayUsed(0)
{ myArray = new int[defaultArraySize]; } //default constructor
Sample (int newSize):ArrayCapacity(newSize), ArrayUsed(0)
{ myArray = new int[newSize]; } //specified constructor
~Sample() { delete[] myArray; } //destructor
}; //end Sample class definition
Also, you really shouldn't use global variables if you can avoid it. Globalconstants are okay, even advisable, but not variables. It would be a good idea to get rid of Lines 6 and 7 and find a better way to do that.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
Do you know how to traverse an array?
const int arraySize = 10;
int anArray[arraySize] = {0};
for (int i = 0; i < arraySize; ++i) {
cout << "Array element " << i << " is " << anArray[i] << endl;
}
Add a Display method to the class. Then, inside the method, apply this concept formatting the output as you see fit.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
>>I'm sure this s getting kind of tedious, but it is a huge help, sorry!
No worries, your listening and trying, that's good enough for me.
You would declare, implement, and call it just like you did your Add() or Take() method. The only difference is that it displays the contents instead of manipulating them. I would also suggest declaring it as a "const" method, so that you can't accidentally edit its contents inside the function. Extending above example:
const int defaultArraySize = 10;
class Sample {
private:
int *myArray;
int ArrayCapacity;
int ArrayUsed;
public:
Sample():ArrayCapacity(defaultArraySize), ArrayUsed(0)
{ myArray = new int[defaultArraySize]; } //default constructor
Sample (int newSize):ArrayCapacity(newSize), ArrayUsed(0)
{ myArray = new int[newSize]; } //specified constructor
~Sample() { delete[] myArray; } //destructor
void Display() const; //declare a const Display function
}; //end Sample class definition
void Sample::Display() const{ //implement the Sample::Display method
for (int i = 0; i < this->ArrayUsed; ++i) {
cout << "Array element " << i << " is " << myArray[i] << endl;
} //end for
} //end Sample::Display
int main() {
Sample aSample; //create a default-size Sample object
//populate and manipulate the contents of aSample
aSample.Display(); //tell the Sample to Display itself
//more actions
return 0;
} //end main()
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
I gave it a quick once-over, it looks like you've got the right idea, you just need to clean it up a little and make it your own.
Make sure to modify the output(s) to fit the requirements of your assignment. I have no idea what your assignment requires, the code that you copy/pasted was intended to be generic for demonstration purposes only.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
cout << "Thank you. Press any key to close.";
I just noticed this. There is no legal way in Standard C++ to do this. The best way to do it is place a cin.get(); directly before the return 0; and require the user to press the ENTER key:
int main() {
//misc code...
cout << "Please press ENTER to close...";
cin.get();
return 0;
} //end main()
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393