Derived Classes with 1 member different

Hi,
I have 2 main classes: BookShelf and Library. Both these classes need to have a member class which would be BookList. Now BookList is a linked list with each object have listing of 10 books and a link pointer to another BookList. I need to maintain a list pointer for the Shelf objects separately and the Library separately. So I have a bookIndex variable for both of them. But since both of them are different classes how do I set the definition of parent in BookList class so that its getNextBook function is the same. I have pasted simplified code below:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct Book{
   std::string BookName;
   std::string Author;
}
class BookList{
public:
   class BookList *prevList;
   class BookList *nextList;
   struct Book thisList[10];
   class <ParentType> *parent;

   class Book *getNextBook(){
       parent->bookIndex = parent->bookIndex + 1;
       if(parent->bookIndex == 10){
           parent->bookIndex = 0;
           return nextList->thisList[0];
       }
       else {
           return thisList[parent->bookIndex];
       }
   }
}
class BookShelf{
public:
   int bookIndex;
   class BookList shelfList;
}

class Library{
public:
   int bookIndex;
   class BookList libList;
}


Ideally my problem will be solved if do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
struct Book{
   std::string BookName;
   std::string Author;
}
class BookListLib{
public:
   class BookList *prevList;
   class BookList *nextList;
   struct Book thisList[10];
   class Library *parent;

   class Book *getNextBook(){
       parent->bookIndex = parent->bookIndex + 1;
       if(parent->bookIndex == 10){
           parent->bookIndex = 0;
           return nextList->thisList[0];
       }
       else {
           return thisList[parent->bookIndex];
       }
   }
}
class BookListShelf{
public:
   class BookList *prevList;
   class BookList *nextList;
   struct Book thisList[10];
   class BookShelf *parent;

   class Book *getNextBook(){
       parent->bookIndex = parent->bookIndex + 1;
       if(parent->bookIndex == 10){
           parent->bookIndex = 0;
           return nextList->thisList[0];
       }
       else {
           return thisList[parent->bookIndex];
       }
   }
}
class BookShelf{
public:
   int bookIndex;
   class BookListShelf shelfList;
}

class Library{
public:
   int bookIndex;
   class BookListLib libList;
}


But this way I create 2 nearly identical classes - BookListLib and BookListShelf with everything identical except the defintion for parent.

So is there a better way to do this?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Something{//think of a name..
public:
   int bookIndex;
};
struct Book{
   std::string BookName;
   std::string Author;
};
class BookList{
public:
   BookList *prevList;
   BookList *nextList;
   Book thisList[10];
   Something *parent;

   Book *getNextBook(){
       parent->bookIndex = parent->bookIndex + 1;
       if(parent->bookIndex == 10){
           parent->bookIndex = 0;
           return nextList->thisList[0];
       }
       else {
           return thisList[parent->bookIndex];
       }
   }
};
class BookShelf : Something{
public:
   BookList shelfList;//this could also be inherited..
};

class Library : Something{
public:
   BookList libList;
};
Thanks hamsterman.
After thinking about the solution I came to realize that if I define a class Something then each instance of BookList has that. My purpose of putting bookIndex in the parent was to have one index for all the linked lists (forming one big list) for one object that being a BookShelf or Library.

If I put bookIndex inside BookList then I am using more memory to store all the extra bookIndex variables.

So is there any other memory efficient way rather other than defining 2 separate BookList classes?
Shouldn't library contain a list of lists?
Hi Naraku,
Sorry I didn't understand. Which library?
Topic archived. No new replies allowed.