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;
}
std::vector.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.