0

I have created a class called Book and it can have many Books. But I want to create a class shelf which can contain only 10 of Books if it is greater than than 10 ten it should print Error Message! But I cannot think of a way to make class shelf. So far I have done this :

#include <iostream>
#include <string>
using namespace std;

class Book{
private:
    string bookName;
    int pNum;

public:
    Book();
    Book(string tempName, int tNum){
        setName(tempName);
        setPageNum(tNum);
    }

    void setName(string bName){
    bookName = bName;
    }



    void setPageNum(int tempNum){
        pNum = tempNum;
    }

    string getName(){
     return bookName;

         }
    int getPageNum(){
        return pNum;

         }
};

class Shelf{
    public:
    Book nBook[10];
    void addbook();
void Book::addbook(Book nBook[10])
{
    for(int i = 0; i<10; i++)
        nBook[i] = nBook[i].setName(string bName)

}


};
int main(){

    Book math = Book("math", 500);
    Book abcd = Book("abcd", 501);


    cout << English.getName() <<" "<<English.getPageNum()<<endl;
    cout << German.getName() <<" "<<German.getPageNum()<<endl;


}
5
  • 1
    Perhaps a std::vector. Commented Apr 12, 2015 at 3:19
  • how should i use std vectors? i have completely no idea to use vectors? Commented Apr 12, 2015 at 3:24
  • I recommend finding some introductory material on them to read. They are one of the most practical things in the standard library IMO. Commented Apr 12, 2015 at 3:40
  • 2
    nBook[i] = nBook[i].setName(string bName) This line won't compile. setName doesn't return anything, and that is not how to pass a parameter. I would expect a function called addbook would only add 1 book, not several. Commented Apr 12, 2015 at 4:08
  • Also Book math = Book("math", 500); is a waste, just do Book math("math", 500); Commented Apr 12, 2015 at 12:33

2 Answers 2

2

You can use the standard vector class to store the Book objects in the shelf. In the example below, the book object will be copied when added to the Shelf

#include <vector>
using namespace std;
class Shelf{
public:
  vector<Book> books;

  bool addbook(Book book)
  {
    if(books.size() > 10) 
      {
        return false;
      }
    else 
      {
        books.push_back(book);
        return true;
      }
  }
};
Sign up to request clarification or add additional context in comments.

Comments

1

There are many ways to implement this. Note the default constructor of class Book. Let me know if you have any questions.

#include <iostream>
#include <string>
using namespace std;

class Book{
private:
    string bookName;
    int pNum;

public:
    Book(){
    }
    Book(string tempName, int tNum){
        setName(tempName);
        setPageNum(tNum);
    }

    void setName(string bName){
    bookName = bName;
    }

    void setPageNum(int tempNum){
        pNum = tempNum;
    }

    string getName(){
     return bookName;

    }
    int getPageNum(){
        return pNum;

    }
};

class Shelf{
    public:
    Book nBook[10];
    int numberOfBooks;
    void addbook(Book);

    Shelf()
    {
       numberOfBooks = 0;
    }

    bool addBook(Book newBook)
    {
        if (numberOfBooks == 10)
        {
            return false;
        }
        else
        {
            nBook[numberOfBooks] = newBook;
            numberOfBooks++;
            return true;
        }
    }


};
int main(){

    Book English = Book("math", 500);
    Book German = Book("abcd", 501);


    Shelf bookShelf = Shelf();

    bookShelf.addBook(English);

}

4 Comments

What's the point of copyBook? Just use the copy constructor or assignment operator, which is generated for you automatically.
Three is no need to use new in this code, which is leaking memory as you have no delete.
Book nBook[10]; ... nBook[numberOfBooks - 1] = newBook;
@Hasnain It works for me. I just tried the code on cpp.sh I just added the following in main: for (int i = 0; i < 11; i++) cout << bookShelf.addBook(English) << endl;

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.