Please refer to the below code to know how many objects are there in class. In your case you are saying you may not know the number of books (objects in class). You can do so by using static data member (in code below it is index) and set it to zero.
1 2 3
As such this data member will be shared by all the objects. In constructor simply increment this data member. So whenever any object will be initialized this index will increament and will give the number of objects.
staticint index; //static data member declaration. This means that index will be shared between all the object
void showdata() const;
void sample::showdata() const //this is how defination of member function is done outside the class
int sample::index=0; //defination of static data member is done outside the class
//name of class must be included in defination. Can be used to keep track of how many objects are created for class
int main(int argc, char* argv)