0

I have 2 classes

class Book
{
public:

    int _pages;
    string* _name;
};
class Shelf
{
public:
int shelfName;
int _booksCount;
book** _books;
};

(with more irrelevant function and variables)

and I want to create function which will calculate the total pages on the shelf, i am new OOP so that what i tried to do:

double Shelf:: getAvg()
{
    int sum, i;
    for (int i = 0; i < __bookCount-1; i++)// the count not considering inedx 0
    {
        sum += _books[i]._pages;// need to be fixed<<
    }
}  

I am pretty sure the only problem is the last line syntax, can you please guide me how to correct it? thank in advance

ariel20
  • 15
  • 10
  • What type is `books[i]`? How do you dereference one of those? Don't start your identifiers with an underscore; those names are reserved. – 1201ProgramAlarm Nov 12 '16 at 23:29
  • my teacher requires it the underscore, books is 2d array, so i thought if i will put index it will be the book itself, but this is the problem in my code so I don't know what is the syntax... – ariel20 Nov 12 '16 at 23:35
  • `__bookCount` is a bad idea. [What are the rules about using an underscore in a C++ identifier?](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Nov 12 '16 at 23:49

3 Answers3

0

Four issues:

  1. You have a typo in name __bookCount inside the for loop : it should be _bookCount (one '_' instead of two)
  2. You're increasing i as long as it's less then _bookCount - 1 : you should change that to less or equal sign, or put there _bookCount only.
  3. You've got no return statement in your code.
  4. You've got double pointer to _books, so _books[i] is not an object instance, it's a pointer for an object of class Book. You should use _books[i]->_pages instead, or (*_books[i])._pages
delabania
  • 784
  • 1
  • 5
  • 14
0

You have multiple errors there.

  • Senseless * in _name member
  • Typo _booksCount
  • Missing return in method getAvg
  • Missing declaration of method getAvg in Shelf class
  • And last, major error is, that you have member Book** _books which is pointer of pointer, but you access it as it if were only pointer. You should declare _books only as Book* _books.

Your code should (can) looks like:

class Book {
    public:
        int _pages;
        string _name;
};

class Shelf {
public:
    int shelfName;
    int _booksCount;
    Book* _books;

    double getAvg();
};

double Shelf::getAvg()
{
    int sum, i;
    for (int i = 0; i < _booksCount; i++)
    {
        sum += _books[i]._pages;
    }

    return sum / _booksCount;
}

I hope, it helps :)

Idea: You can use some stl container (for example vector) instead of array for Books in Shelf

vector<Book> _books;

Because if you use * and new (or malloc) for initialization, your object is not allocated on the Stack, but on the Heap, so you should delete it with delete (or free) or it will cause memory leaks.

0

I think You would like to do sth like this:

#include <vector>
#include <string>
#include <iostream>
#include <memory>

class Book
{
public:
    int _pages;
    std::string* _name;
    Book(const char* nameArg, size_t pagesArg): _name(new std::string(nameArg)), _pages(pagesArg) {}
    ~Book() { delete name;}
};

class Shelf
{
    std::string shelfName;
    std::vector<std::unique_ptr<Book>> _books;
public:
    Shelf(const char* nameArg): shelfName(nameArg) {
    }   

    void addBook(const char* bookNameArg, size_t pagesArg) {
        _books.push_back(std::unique_ptr<Book>(new Book(bookNameArg, pagesArg)));
    }   

    size_t getNoPagesOnShelf() const {
        size_t pagesCount = 0;
        for( int tmp = 0; tmp < _books.size(); ++tmp) { 
                pagesCount += _books[tmp]->_pages;
        }           
    return pagesCount;
    }   
};


int main() {
 Shelf shelf("Number 1");
 shelf.addBook("Book1", 500);
 shelf.addBook("Book1", 20);
 shelf.addBook("Book1", 300);
 std::cout << "Total number of pages in shelf is : " << shelf.getNoPagesOnShelf() << std::endl;
}

Compile with c++11 flag, if You are using g++ add --std=c++11 Hope this helps

Mateusz Wojtczak
  • 1,621
  • 1
  • 12
  • 28