Inheritance

Hi!

I'm having trouble with multiple inheritance. I'm supposed to write a program for chatrooms, the situation being this:
-A chatroom contains a name, a list of users, an administrator and a list of messages. It can be either private, requiring a password, or open.
-A user has a name and possibly a password.
-An adminstrator is a user that in addition has a list of chatrooms that he is adminstrating.

My problem is that a chatroom contains an administrator, but an administrator also contains a list of chatrooms! How can I solve this?
My first idea has to do like for struct which allows me to predefine administrator so that I can use administrators in the definition of a chatroom structure. But I don't think this works for classes (I can't write class Admin; and then include an Admin-vector in a chatroom)?

Thank you in advance for your help!
Last edited on
You need some pointers to help you here. Like you found out, you can't just use the actual objects here, otherwise you'd have infinite recursion.
closed account (S6k9GNh0)
I'm not sure I see the question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct User {
    std::string name, password;
};

struct Chatroom;

struct Admin : public User {
    std::list<Chatroom> m_Rooms; 
};

enum ROOM_FILTER { 
    F_OPEN = 0,
    F_PRIVATE
}

struct Chatroom {
    std::string m_Name;
    std::list<User> m_Users;
    std::deque<std::string> m_Messages;
    ROOM_FILTER m_Filter;
    Admin* m_Admin;
};


This is definitely not the best example but it seems to fit your description.
Last edited on
Thanks, I think the last answer wouldn't be a good solution, because afterwards I'm asked to create functions for the different objects, so I'll have to use class instead of struct. So for the pointers, my class Admin will contain a real user while the class Chatroom will contain a pointer to an Admin, right?
closed account (S6k9GNh0)
Well, struct is the same as a class in *almost* every aspect aside from default specifier.
Last edited on
Ok, but is there also a way to solve the problem using class instead of struct? And can I really use struct just like I would use class?
I still don't really get how using pointers will help me. What difference does it make that your struct Chatroom contains pointers to Admin instead of Admin? My code so far is below, but it obviously doesn't work, since Admin hasn't been defined before Room. I also tried a predefinition like with struct, inserting "class Admin;" at the very beginning, but it seems this isn't valid.


#include <iostream>
#include <string>
#include <vector>

using namespace std;

class User {
public:
//constructors
User(string name, string password)
: name(name), password(password) {}

protected:
string name;
string password;

};
class Room {
public:
//constructors
Room(string name, Admin adm)
: name(name), adm(adm) {}

protected:
string name;
Admin* adm;
vector<User*> list_users;
vector<string> list_messages;


};
class Admin: public User, Room {
public:
//constructors
Admin(User lad)
: lad(lad) {}

private:
vector<Room> list_rooms;
User lad;
};
Topic archived. No new replies allowed.