1

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.

All of this will be done with an array of integers with a length that will always be 20.

Assignment calls for 3 separate files:

1 header file, book.h, 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.

//book.h
class Shelf{
public:
Shelf();
void insert(int);
int bookshelf[];
};

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?)

//book.cpp
#include <iostream>  
#include "book.h"
const int shelfSize=20;

Shelf::Shelf(){
    bookshelf[shelfSize];
    for(int i=0; i <shelfSize; i++)
    bookshelf[i]=0;
}
//inserts a book to the end of the list
void Shelf::insert(int booknum){
    for(int i=0; i < shelfSize; i++)
    bookshelf[i] = booknum;
}

Now book_main.cpp, which is just supposed to give a menu and prompt the user:

//book_main.cpp
#include<iostream>

using namespace std;

#include "book.h"

int main(){

Shelf book; //creates a Shelf object named "book"
int isbn=0;
cout<<"Enter ISBN: "<<endl;
cin>>isbn;
book.insert(isbn);
return 0;
}

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.

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()

3
  • 1
    As Oli has pointed out, you can't access a member of an empty array. :) Commented Jun 22, 2011 at 0:10
  • 1
    You have better to hide bookshelf(move to private:). Commented Jun 22, 2011 at 0:14
  • thanks, @mattn, I was planning on working on that once I got everything else working ha. Commented Jun 22, 2011 at 0:16

2 Answers 2

4

Try this:

//book.h
class Shelf{
public:
    static const int shelfSize = 20;
    Shelf();
    void insert(int);
    int bookshelf[shelfSize];
};

(You'll need to remove the bookshelf[shelfSize]; line from your constructor.)

Your constructor can be simplified, by taking advantage of initializer lists:

Shelf::Shelf()
  : bookshelf()  // This initializes all elements to zero
{}

Your insert() logic doesn't make much sense; you'll need to keep track of how many items you've inserted so far, so that you'll know where to put the next one.

Also, consider using a std::vector instead of a raw C-style array. They avoid several problems, such as running out of space!

Sign up to request clarification or add additional context in comments.

9 Comments

Does that constructor truly set all of bookshelf's values to 0? And where is that defined as a default value for an int?
It doesn't need to set anything to 0, in fact, I would prefer an array size with size 20, that has yet to have anything entered into each element.
Oh interesting! I didn't know that :). Maybe add it to your answer for clarity (8.5.5). "To default-initialize an object of type T means: ... if T is an array type, each element is default-initialized; — otherwise, the object is zero-initialized." The definition is recursive, as each object is then default initialized :).
@Daniel: Yes! Although you'll notice that I hastily deleted my comment, because it seems that this has changed in C++0x (the wording becomes "otherwise, no initialization is performed"). So I'm ducking out of trying to be authoritative on this one!
@Daniel: (continued...) Ah, in C++0x, empty parentheses denotes value initialization, which in turn becomes the traditional recursive definition ending in zero initialization.
|
1

bookshelf don't have space. your definition is not determined size.

//book.h
#define shelfSize 20

class Shelf{
public:
    Shelf();
    void insert(int);
    int bookshelf[shelfSize];
};

And use of book.h

//book.cpp
#include <iostream>  
#include "book.h"

Shelf::Shelf(){
    for(int i=0; i <shelfSize; i++)
        bookshelf[i]=0;
}

//inserts a book to the end of the list
void Shelf::insert(int booknum){
    for(int i=0; i < shelfSize; i++)
        bookshelf[i] = booknum;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.