Assigning data to multiple classes with many types

Hello,
let's say I want to create program that creates library:
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
//It's an example code; It's not designed to compile, but rather to show my thoughts, show what do I mean

//book.h

class Book {
    int pages;
    string name;
    string author;
    
    public:
        Book() {}
        void assign(int p, string n, string a){pages = p; name = n; author = a;}
        string getAuthor() {return author;}
};
//end of book.h

//shelf.h
#include "book.h"
#include <vector>

class Shelf {
     vector<Book>(capacity);
     int capacity;
     int level;

     public: 
        Shelf() {}
        void assign(/* what should I put here to be able to assign vector of books? */);
};
//end of shelf.h

//cabinet.h
#include "shelf.h"
#include <vector>

class Cabinet {
     vector<shelf> (capacity);
     int capacity;
     int height;

     public:
         Shelf() {}
         void assign(/*now, what should I include here, to assign vector of shelf? */);
};
//end of cabinet.h 


These relations can go on forever.
I want to create a class which contains object of other class, and it can go like in example above, for a few levels. The problem is, I don't know how to make it work, and don't look ugly.

First thing: I don't know how to create setters(assign function) for these objects that are a bit more complicated(with vectors).
Second thing: let's say I declare shelf:
 
Shelf personal;

Ignoring the fact that I don't know how to assign anything to it...
Let's say there are 3 books in it, and I want to see the Author of first one.
Normally, I'd make it through getAuthor function. However, this went a bit further, and I don't have direct access to this object.

Is there a way to accomplish this task without inheritance, or is whole concept wrong?

Thanks in advance.
I want to create a class which contains object of other class, and it can go like in example above, for a few levels. The problem is, I don't know how to make it work, and don't look ugly.


You already did, and it works fine.

I don't know how to create setters(assign function) for these objects that are a bit more complicated(with vectors).


It's as getters, but it's not void, and, the member variable is L-sided, not R-sided, that is, for example:

under public inside class definition for Book

1
2
void setpages(int);
int getpages();


body outside main(),

1
2
3
4
5
6
7
8
void Book::setpages(int p)
{
     pages = p;
}
int Book::getpages()
{
     return pages;
}



Let's say there are 3 books in it, and I want to see the Author of first one.
Normally, I'd make it through getAuthor function. However, this went a bit further, and I don't have direct access to this object.


Author is a member of Book not Shelf. Shelf is a vector of books. So you need to access the third element, for example, of capacity, and get the author name using a getfunction in the following way

1
2
// Assuming shelf has at least three elements
string str = personal.capacity[3].getAuthor();


Btw, why are you putting your variable names between parenthesis. I don't know if there is somethign wrong with that, but you're better off not doing so.
Last edited on
Let's start by thinking about a shelf.
One inserts a book on a shelf.
1
2
3
4
5
6
7
8
9
10
class Shelf {
     vector<Book> m_books;  // Collection of books on this shelf
     int level;

     public: 
        Shelf() {}
        void Insert (const Book & book)
        { m_books.push_back (book);
        }        
};

This assumes you know what shelf you want to insert the book into.

Do shelves have ranges of authors?
Likewise knowing what cabinet to insert a book into means knowing the range of whatever your organization scheme is for cabinets.
Can there be multiple copies of the same book?

You had a variable named capacity. Do shelves have a limit on the number of books they can hold? What happens when the shelf is full?

Are books ordered within the shelf? Vectors are unordered.
If you plan on searching for a book, you might want to consider a map collection which provides more efficient access than a linear search of a vector.




Hey, thanks for replying.

First of all, I made some mistakes during writing this code(I was tired, but wanted to ask it anyway). For example, both vectors of Book doesn't have a name. Let's call them m_books and m_shelf.

I was looking for answer like this:
string str = personal.capacity[3].getAuthor();
But now there's a problem, that capacity is private member of Shelf, so you can't access it directly. Also, capacity isn't an array, but rather a size of amonunt of books on the shelf.(it should be maximum amount, but I coded it wrong; doesn't really matter).

So I guess, I should make something like this:
1
2
3
4
5
6
7
8
9
//In Shelf class...
Book getBook(int number) { return m_books[number]; }

//in Cabinet class...
Shelf getShelf(int number) { return m_shelf[number]; }

//in main.cpp...
Cabinet personal;
personal.getShelf(2).getBook(2).getAuthor();

Will that work?

And to assign, how should I make setters, so creating Cabinet personal i can assign books to it?

@up - this is rather example than working class, it has many bugs, but it was used to show problem.
Last edited on
Yes, that would work. I didn't try it to be honest but I believe it does. I say you fix any remaining bugs and test it and from the looks of it you don't have much to fix.
Topic archived. No new replies allowed.