Dear all,
The concept behind the code is:
There is an associationlist that holds association pointers. Each association pointer has two pointers, that point to a book and to a member object respectively.
Now what I'm trying to do is:
To have the association pointer to use its two pointers to point to an array of books(booklist, up to 10 books) and to an array of members (memberlist, up to 10) respectively.
If you copy and paste the code the program works ok only for books(adding a new book to the booklist(up to 3 for test purposes) and displaying the books).
If one wants to add a member and display the members, they have to use the function: get_book(); in the file:
template<class Object>
void List<Object>::displayElement(char* type) of the List.cpp, and also to put in comments the //book1.displayElement("Books"); of the main.cpp file.
If I don't use the comments in the main.cpp file and use the get_book() function then an error will occur: " 'get_book':cannot convert parameter 1 from 'class Book*' to 'class Member*' "
Could someone suggest a solution using a single template function, for displaying members and books?
The code of each file is:
<br />
#ifndef _ASSOCIATION<br />
#define _ASSOCIATION<br />
<br />
template<class Book,class Member><br />
class Association<br />
{<br />
public:<br />
Association(Book* book, Member* member);<br />
<br />
Book* linked_book(){return this->book;}<br />
<br />
Member* linked_member(){return this->member;}<br />
<br />
private:<br />
Book* book;<br />
Member* member;<br />
};<br />
#endif<br />
<br />
#ifndef _ASSOCIATIONCPP<br />
#define _ASSOCIATIONCPP<br />
#include "Association.h"<br />
<br />
template<class Book,class Member><br />
Association<Book,Member>::Association(Book* book, Member* member)<br />
{<br />
this->book=book;<br />
this->member=member;<br />
}<br />
#endif<br />
<br />
#ifndef _ASSOCIATIONLIST<br />
#define _ASSOCIATIONLIST<br />
#include "Association.h"<br />
<br />
const int LIST_SIZE=100;<br />
<br />
template<class Book,class Member><br />
class AssociationList<br />
{<br />
public:<br />
AssociationList();<br />
<br />
<br />
Member* get_member(Book* book);<br />
<br />
<br />
Book* get_book(Member* member);<br />
<br />
template<class R,class P><br />
R* get_member(P* pArg);<br />
<br />
<br />
bool link(Book* book,Member* member);<br />
<br />
<br />
bool unlink(Book* book,Member* member);<br />
private:<br />
Association<Book,Member>* association_list[LIST_SIZE];<br />
};<br />
#endif<br />
<br />
#ifndef _ASSOCIATIONLISTCPP<br />
#define _ASSOCIATIONLISTCPP<br />
#include <iostream><br />
#include "Association.h"<br />
#include "Association.cpp"<br />
#include "AssociationList.h"<br />
#include "Book.h"<br />
#include "Member.h"<br />
using namespace std;<br />
<br />
template<class Book,class Member><br />
AssociationList<Book,Member>::AssociationList()<br />
{<br />
cout<<"AssociationList constructor called\n";<br />
int index;<br />
<br />
for(index=0; index<LIST_SIZE; index++)<br />
this->association_list[index]=0;<br />
}<br />
<br />
<br />
<br />
<br />
template<class Book,class Member><br />
<br />
Member* AssociationList<Book,Member>::get_member(Book* book)<br />
{<br />
Member* member=0;<br />
bool searching=true;<br />
int index=0;<br />
<br />
while(searching)<br />
{<br />
if (this->association_list[index])<br />
if (this->association_list[index]->linked_book()==book)<br />
{<br />
member=this->association_list[index]->linked_member();<br />
searching=false;<br />
}<br />
else<br />
index++;<br />
else<br />
index++;<br />
if (searching && (index == LIST_SIZE))<br />
{<br />
searching=false;<br />
}<br />
}<br />
return member;<br />
}<br />
<br />
template<class Book,class Member><br />
Book* AssociationList<Book,Member>::get_book(Member* member)<br />
{<br />
Book* book=0;<br />
bool searching=true;<br />
int index=0;<br />
<br />
while(searching)<br />
{<br />
if (this->association_list[index])<br />
if (this->association_list[index]->linked_member()==member)<br />
{<br />
book=this->association_list[index]->linked_book();<br />
searching=false;<br />
}<br />
else<br />
index++;<br />
else<br />
index++;<br />
if (searching && (index == LIST_SIZE))<br />
{<br />
searching = false;<br />
}<br />
}<br />
return book;<br />
}<br />
<br />
template<class Book,class Member><br />
bool AssociationList<Book,Member>::link(Book* book,Member* member)<br />
{<br />
bool searching=true;<br />
bool success=true;<br />
int index=0;<br />
int free_slot;<br />
<br />
while(searching)<br />
{<br />
if(this->association_list[index])<br />
if((this->association_list[index]->linked_book()==book ||<br />
(this->association_list[index]->linked_member()==member)))<br />
{<br />
searching=false;<br />
success=false;<br />
}<br />
else<br />
index++;<br />
else<br />
{<br />
free_slot=index;<br />
index++;<br />
}<br />
if(searching&&(index == LIST_SIZE))<br />
searching=false;<br />
}<br />
if (success)<br />
this->association_list[free_slot]=new Association(book,member);<br />
return success;<br />
}<br />
<br />
template<class Book,class Member><br />
bool AssociationList<Book,Member>::unlink(Book* book,Member* member)<br />
{<br />
bool searching=true;<br />
bool success=true;<br />
int index=0;<br />
int link_slot;<br />
<br />
while(searching)<br />
{<br />
if (this->association_list[index])<br />
if (this->association_list[index]->linked_book()==book &&<br />
this->association_list[index]->linked_member()==member)<br />
{<br />
searching=false;<br />
link_slot=index;<br />
}<br />
else<br />
index++;<br />
else<br />
{<br />
index++;<br />
}<br />
if (searching && (index == LIST_SIZE))<br />
{<br />
searching=false;<br />
success=false;<br />
}<br />
}<br />
if (success)<br />
delete this->association_list[link_slot];<br />
return success;<br />
}<br />
#endif<br />
<br />
#include<iostream><br />
#include"Book.h"<br />
#include"Member.h"<br />
using namespace std;<br />
<br />
Book::Book()<br />
{<br />
cout<<"Book constructor called called\n";<br />
this->bookDetails=get_string_ver2("input book title and author separated by a comma and a space character: ");<br />
}<br />
<br />
Book::~Book()<br />
{<br />
if (this->bookDetails)<br />
delete [] this->bookDetails;<br />
}<br />
<br />
void Book::display(Member* borrower)<br />
{<br />
cout<<"Book title and author are: "<<this->bookDetails<<"."<<endl;<br />
if (borrower)<br />
cout<<"The member's name is: "<<borrower->get_name()<<"."<<endl;<br />
else<br />
cout<<"No member has borrowed a book."<<endl;<br />
}<br />
<br />
#ifndef _BOOK<br />
#define _BOOK<br />
#include "utility.h"<br />
class Member;
class Book<br />
{<br />
public:<br />
Book();<br />
<br />
~Book();<br />
<br />
char* get_bookDetails(){return this->bookDetails;}<br />
<br />
void display(Member* borrower);<br />
private:<br />
Member* borrower;<br />
char* bookDetails;<br />
};<br />
#endif<br />
<br />
#include<iostream><br />
#include"Book.h"<br />
#include"Member.h"<br />
using namespace std;<br />
<br />
Member::Member()<br />
{<br />
cout<<"Member constructor called\n";<br />
this->name=get_string("Input member's surname and forename respectively, separated by a comma and a space character: <br />
<br />
");<br />
}<br />
<br />
Member::~Member()<br />
{<br />
if (this->name)<br />
delete [] this->name;<br />
}<br />
<br />
void Member::display(Book* book)<br />
{<br />
cout<<"Member's name is: "<<this->name<<"."<<endl;<br />
if (book)<br />
cout<<"Book title and author are: "<<book->get_bookDetails()<<"."<<endl;<br />
else<br />
cout<<this->name<<" has not borrowed any book."<<endl;<br />
}<br />
<br />
#ifndef _MEMBER<br />
#define _MEMBER<br />
#include "utility.h"<br />
class Book;
class Member<br />
{<br />
public:<br />
Member();<br />
<br />
~Member();<br />
<br />
char* get_name(){return this->name;}<br />
<br />
void display(Book* book);<br />
private:<br />
Book* book;<br />
char* name;<br />
};<br />
#endif<br />
<br />
#ifndef _LISTCPP<br />
#define _LISTCPP<br />
#include <iostream><br />
#include "AssociationList.h"<br />
#include "List.h"<br />
<br />
using namespace std;<br />
<br />
template<class Object><br />
List<Object>::List()<br />
{<br />
this->num_elements=0;<br />
}<br />
<br />
template<class Object><br />
List<Object>::~List()<br />
{<br />
for(int element=0; element<this->num_elements; element++)<br />
delete this->element_list[element];<br />
}<br />
<br />
template<class Object><br />
void List<Object>::addElement(char* type)<br />
{<br />
if (this->num_elements == MAX_ELEMENTS)<br />
{<br />
cout<<"No more room in the "<<type<<" array.\n";<br />
cout<<"The maximum number of "<<type<<" is "<<MAX_ELEMENTS<<"."<<endl;<br />
}<br />
else<br />
{<br />
this->element_list[num_elements]=new Object;<br />
(this->num_elements)++;<br />
}<br />
}<br />
<br />
template<class Object><br />
void List<Object>::displayElement(char* type)<br />
{<br />
if (num_elements == 0)<br />
cout<<"No "<<type<<" is found in the "<<type<<" array.\n";<br />
else<br />
for(int element=0; element<this->num_elements; element++)<br />
{<br />
cout<<'\n';<br />
this->element_list[element]->display(association_list.get_member(element_list[element]));<br />
}<br />
}<br />
<br />
<br />
template<class Object><br />
void List<Object>::borrowElement()<br />
{<br />
cout<<"Borrow book function invoked\n";<br />
}<br />
<br />
template<class Object><br />
void List<Object>::returnElement()<br />
{<br />
cout<<"Return book function invoked\n";<br />
}<br />
<br />
<br />
<br />
#endif<br />
<br />
#ifndef _LIST<br />
#define _LIST<br />
#include "AssociationList.h"<br />
#include "Book.h"<br />
#include "Member.h"<br />
const int MAX_ELEMENTS=3;<br />
template<class Object><br />
class List<br />
{<br />
public:<br />
List();<br />
~List();<br />
void addElement(char* type);<br />
void removeElement();<br />
void displayElement(char* type);<br />
void borrowElement();<br />
void returnElement();<br />
private:<br />
AssociationList<Book,Member>association_list;<br />
Object* element_list[MAX_ELEMENTS];<br />
int num_elements;<br />
};<br />
#endif<br />
<br />
#include <iostream><br />
#include <cstring><br />
#include <ctype.h><br />
using namespace std;<br />
#define cinFlush cin.seekg(ios::end)<br />
<br />
char* get_string (char* prompt)<br />
{<br />
const int MAX_SIZE=100;<br />
char string[MAX_SIZE];<br />
char* out_string;<br />
char *substring=", ";<br />
int position=0;<br />
bool result=false;<br />
do<br />
{<br />
cout<<prompt; cinFlush;<br />
cin.getline(string,MAX_SIZE);<br />
if (strstr(string,substring) == NULL)<br />
{<br />
cout<<"Invalid Name(substring ', ' not found into the string)"<<endl;<br />
result=false;<br />
}<br />
else<br />
{<br />
position=strstr(string,substring) - string;<br />
if (position == 0)<br />
{<br />
cout<<"Invalid Name(substring ', ' found in the beginning of the string)"<<endl;<br />
result=false;<br />
}<br />
else if (toascii(string[position + 1]) == 32 && string[position + 2] == '\0')<br />
{<br />
cout<<"Invalid Name(substring ', ' found at the end of the string)"<<endl;<br />
result=false;<br />
}<br />
else<br />
{<br />
for(int j=0; j<position; j++)<br />
if ((toascii(string[j]) >= 0 && toascii(string[j]) <= 64)<br />
|| (toascii(string[j]) >= 91 && toascii(string[j]) <= 96)<br />
|| (toascii(string[j]) >= 123 && toascii(string[j]) <= 127))<br />
{<br />
cout<<"Invalid character(s) found before the substring ', '"<<endl;<br />
result=false;<br />
break;<br />
}<br />
else<br />
{<br />
for(int m=position + 2; m<strlen(string); m++)<br />
if ((toascii(string[m]) >= 0 && toascii(string[m]) <= 64)<br />
|| (toascii(string[m]) >= 91 && toascii(string[m]) <= 96)<br />
|| (toascii(string[m]) >= 123 && toascii(string[m]) <= 127))<br />
{<br />
result=false;<br />
break;<br />
}<br />
else<br />
result=true;<br />
}<br />
<br />
}<br />
}<br />
}while(!result && !cin.eof());<br />
out_string=new char [strlen(string)+1];<br />
if (out_string)<br />
strcpy(out_string,string);<br />
return out_string;<br />
}<br />
<br />
<br />
char* get_string_ver2 (char* prompt)<br />
{<br />
const int MAX_SIZE=100;<br />
char string[MAX_SIZE];<br />
char* out_string;<br />
char *substring=", ";
int position=0;
bool result=false;
do
{
cout<<prompt; cinFlush;<br />
cin.getline(string,MAX_SIZE);<br />
if (strstr(string,substring) == NULL)<br />
{<br />
cout<<"Invalid String(substring ', ' not found into the string)"<<endl;<br />
result=false;<br />
}<br />
else<br />
{<br />
position=strstr(string,substring) - string;<br />
if (position == 0)<br />
{<br />
cout<<"Invalid String(substring ', ' found in the beginning of the string)"<<endl;<br />
result=false;<br />
}<br />
else if (toascii(string[position + 1]) == 32 && string[position + 2] == '\0')<br />
{<br />
cout<<"Invalid String(substring ', ' found at the end of the string)"<<endl;<br />
result=false;<br />
}<br />
else<br />
{<br />
for(int z=0;z<position; z++)<br />
if (!(( toascii(string[z]) > 64 && toascii(string[z]) < 91 ) <br />
|| ( toascii(string[z]) > 96 && toascii(string[z]) < 123)<br />
|| ( toascii(string[z]) == 32)))<br />
{<br />
cout<<"Invalid character(s) found before the substring"<<endl;<br />
result=false;<br />
break;<br />
}<br />
else<br />
{<br />
for(int m=position + 2; m<strlen(string); m++)<br />
if (!(( toascii(string[z]) > 64 && toascii(string[z]) < 91 ) <br />
|| ( toascii(string[z]) > 96 && toascii(string[z]) < 123)<br />
|| ( toascii(string[z]) == 32)))<br />
{<br />
result=false;<br />
break;<br />
}<br />
else<br />
result=true;<br />
}<br />
<br />
}<br />
}<br />
}while(!result);<br />
out_string=new char [strlen(string)+1];<br />
if (out_string)<br />
strcpy(out_string,string);<br />
return out_string;<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
void hold_screen()<br />
{<br />
char ch;<br />
<br />
cout << "hit enter to continue" << endl;<br />
cin.get(ch);<br />
}<br />
<br />
void prompt_to_quit()<br />
{<br />
char ch;<br />
<br />
cout << "hit enter to quit" << endl;<br />
cin.get(ch); <br />
}<br />
<br />
#ifndef _UTILITY<br />
#define _UTILITY<br />
<br />
char* get_string (char* prompt);<br />
<br />
<br />
char* get_string_ver2(char* prompt);<br />
<br />
<br />
void hold_screen();<br />
<br />
<br />
void prompt_to_quit();<br />
<br />
<br />
<br />
#endif<br />
and finally
<br />
#include <iostream><br />
#include "List.cpp"<br />
#include "Association.cpp"<br />
#include "AssociationList.h"<br />
#include "AssociationList.cpp"<br />
#include "Book.h"<br />
#include <conio.h><br />
#include "Member.h"<br />
#include "utility.h"<br />
#define cinFlush cin.seekg(ios::end)<br />
using namespace std;<br />
<br />
void main()<br />
{<br />
List<Member>member1;<br />
List<Book>book1;<br />
char menuChoice;<br />
do<br />
{<br />
cout<<'\n';<br />
cout<<'\n';<br />
cout<<"**** Main Menu ****"<<endl;<br />
cout<<'\n';<br />
cout<<"1: Add Book "<<endl;<br />
cout<<'\n';<br />
cout<<"2: Remove Book "<<endl;<br />
cout<<'\n';<br />
cout<<"3: Display Books "<<endl;<br />
cout<<'\n';<br />
cout<<"4: Add Member "<<endl;<br />
cout<<'\n';<br />
cout<<"5: Remove Member "<<endl;<br />
cout<<'\n';<br />
cout<<"6: Display Members "<<endl;<br />
cout<<'\n';<br />
cout<<"7: Borrow Book "<<endl;<br />
cout<<'\n';<br />
cout<<"8: Return Book "<<endl;<br />
cout<<'\n';<br />
cout<<"0: Quit "<<endl;<br />
cout<<endl;<br />
cout<<"Enter one of the above choices: ";cinFlush;<br />
cin>>menuChoice;<br />
switch(menuChoice)<br />
{<br />
case '1': cout<<'\n';<br />
book1.addElement("Books");<br />
break;<br />
case '2': cout<<'\n';<br />
<br />
break;<br />
case '3': cout<<'\n';<br />
book1.displayElement("Books");<br />
break;<br />
case '4': cout<<'\n';<br />
break;<br />
case '5': cout<<'\n';<br />
<br />
break;<br />
case '6': cout<<'\n';<br />
break;<br />
case '7': cout<<'\n';<br />
break;<br />
case '8': cout<<'\n';<br />
break;<br />
default: cout<<'\n';<br />
cout<<"Invalid Selection\n";<br />
}<br />
}while(menuChoice != '0' && !cin.eof());<br />
prompt_to_quit();<br />
}<br />
What I want to do is to use one function for displaying members and books (to use a template).
What needs to be changed to use only one function for displaying members and books?
Regards,
grscot
|