Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp Setting/Constructing Static Array Size in Classes and Objects in C++
    primarykey
    data
    text
    <p>Have a homework assignment, I thought was simple and can do it not using classes/objects, but I guess I'm just not understanding classes/objects well enough to complete it the way my teacher is asking me to do.</p> <p>All of this will be done with an array of integers with a length that will always be 20. </p> <p>Assignment calls for 3 separate files:</p> <p>1 header file, <strong>book.h</strong>, containing class elements/member functions/prototypes) 1 .cpp file, containing all the member functions. For this I will simply include one example(, 1 .cpp file, which simply contains int main and acts as what the user sees as an interface and calls the member functions etc.</p> <pre><code>//book.h class Shelf{ public: Shelf(); void insert(int); int bookshelf[]; }; </code></pre> <p>Now book.cpp which contains member functions ( I really don't know if I need a constructor like this and do I name the array the same name as the array in book.h?)</p> <pre><code>//book.cpp #include &lt;iostream&gt; #include "book.h" const int shelfSize=20; Shelf::Shelf(){ bookshelf[shelfSize]; for(int i=0; i &lt;shelfSize; i++) bookshelf[i]=0; } //inserts a book to the end of the list void Shelf::insert(int booknum){ for(int i=0; i &lt; shelfSize; i++) bookshelf[i] = booknum; } </code></pre> <p>Now <strong>book_main.cpp</strong>, which is just supposed to give a menu and prompt the user:</p> <pre><code>//book_main.cpp #include&lt;iostream&gt; using namespace std; #include "book.h" int main(){ Shelf book; //creates a Shelf object named "book" int isbn=0; cout&lt;&lt;"Enter ISBN: "&lt;&lt;endl; cin&gt;&gt;isbn; book.insert(isbn); return 0; } </code></pre> <p>Now I realize this logic doesn't make much sense, there is a lot more in my program besides this. I'm watering it down, just to show what I'm having problems with. </p> <p>1) How should I go about setting up the array in my object and member functions etc? I imagine my function design is horrible, as well as incorrect name of the array in the file that contains all the member functions etc. Any advice would be appreciated. This program compiles but crashes every time I call book.insert()</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload