Guys, im having trouble with C++ object oriented.
I have a class library, class member, class book, class loan.
In library.h i have :
Code:
class Library
{
const unsigned short NB_MEMBER_MAX = 100;
const unsigned short NB_BOOK_MAX = 1000;
const unsigned short NB_LOAD_MAX = 200;
public:
Library();
~Library();
//Methods
void addMember(Member);
void removeMember(const string&);
void addBook(Book*);
void removeBook(const string&);
void findTitle(const string&);
void findCode(const string&);
bool loan(const string&, const string&, unsigned int);
bool returnBook(const string&, const string&);
void infoMember(const string&);
private:
Member** arrayMember_; //Aggregation
unsigned int nbMember_;
Book** arrayBook_; //Aggregation
unsigned int nbBook_;
Loan** arrayLoan_; //Composition
unsigned int nbLoan_;
};
In Library.cpp, the build for dynamic allocation pointer arrays would be this
Code:
Library::Library() {
arrayMember_ = new Member*[NB_MEMBER_MAX];
for (int i = 0; i < NB_MEMBER_MAX; i++)
arrayMember_[i] = nullptr;
nbMember = 0;
arrayBook_ = new Book*[NB_BOOK_MAX];
for (int i = 0; i < NB_BOOK_MAX; i++)
arrayBook_[i] = nullptr;
nbBook_ = 0;
arrayLoan_ = new Loan*[NB_LOAN_MAX];
for (int i = 0; i < NB_LOAN_MAX; i++)
arrayLoan_[i] = nullptr;
nbLoan_ = 0;
}
Library::~Library() {
for (int i = 0; i < NB_MEMBER_MAX; i++) {
if (arrayMember_ != nullptr) {
delete arrayMember_[i];
arrayMember_[i] = nullptr;
}
}
delete[] arrayMember_;
arrayMember_ = nullptr;
for (int i = 0; i < NB_BOOK_MAX; i++) {
if (arrayBook_ != nullptr) {
delete arrayBook_[i];
arrayBook_[i] = nullptr;
}
}
delete[] arrayBook_;
arrayBook_ = nullptr;
for (int i = 0; i < NB_LOAN_MAX; i++) {
if (arrayLoan_ != nullptr) {
delete arrayLoan_[i];
arrayLoan_[i] = nullptr;
}
}
delete[] arrayLoan_;
arrayLoan_ = nullptr;
}
... and much more code in library...
Now, can we even determine composition/aggregation from that? Because this seems like the only way of making a dynamic array of pointers that i know of.
Could it be from the main? we create the members and books in main, and then create the library and add them.
Code:
int main()
{
Member a1 = Member("1839456", "Doe", "John", 23);
Book* b1 = new Book("GA403", "Big C++", 2009, 8, 3);
...
I thought composition/aggregation had to be within the class and not the main?