//InternetBookStore.cpp
#include "InternetBookStore.h"
usingnamespace std;
// notice that I used an initializing list so the constructor should be called
InternetBookStore::InternetBookStore(): _books(MAXBOOKS){
for (int i=0;i<50;++i)
_books[i].show();
}
........
// Book.cpp
#include <iostream>
#include "Book.h"
Book::Book(){
_name = _author = "no name";
_numCopies = 0;
cout << " Book Ctor ";
}
void Book::show() const{
cout<<"Name:"<<_name<<" Author:"<<_author<<" Number of copies:"<<_numCopies<<endl;
}
...
and when I call from main--->
1 2 3 4
...
int main(){
InternetBookStore i;
...
now, notice that when the constructor of InternetBookStore called the ctor of Book will be called too from the initialized list, the question how many times?
if the the size of Books' vector is 'MAXBOOKS' then it would be called 'MAXBOOKS' times but when I run the program I see that the constructor called once !!
my output:
./myStore
Book Ctor
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Based on that code, the constructor will be called 100 times, not once. Post your full code.
Also, never use using directives on global scope in a header. And you're using vector incorrectly. You're supposed to add elements to it when needed, not precreate a whole bunch of dummy objects.