May You show us some more complex example of how those classes we create from the "abstract world" may interact with each another the "right" way?... |
The trick is to have the "lower", more basic classes not be aware of the "higher" more complicated ones. The basic classes are the foundation, and from there you stack upwards. If your basic classes are aware of the classes using them, then your program quickly becomes entangled in hard to maintain interdependencies.
Admittedly, I haven't done much networking stuff, but I'd probably approach it like so:
abstract class Connection
which represents a network connection between this computer and "something else" (what you're connected with doesn't matter, as it's abstract)
Since the class is abstract, it wouldn't really do anything. However it would have pure virtual functions to Send and Receive data, and other things you might need.
class SingleConnection : public Connection
which represents a connection between this computer and one other computer
SingleConnection would probably contain most of the networking nitty gritty. Establishing the connection and actually sending/receiving information and whatnot.
class GroupConnection : public Connection
which represents a connection between this computer and multiple other computers.
Internally this class would probably contain a
std::list<Connection*>
to represent all of the other computers connected to. Send() would just send the data to all connections, and Receive would listen for/receive data from all connections.
class ChatClient
which represents a chat client. Internally this would probably just have a SingleConnection, and would translate intuitive things like, lines of text, request for a list of chatrooms, etc into raw data to be transmitted. Basically the interface for ChatClient would be more intuitive for a chat program, whereas SingleConnection would be more "raw" and generic.
ChatClient could also have multiple Connections if you want the user to join multiple servers at once, or directly connect to other users for PMs or something.
Maybe that could even be broken down more. Like maybe have a ChatConnection which does the translating, and then have ChatClient which has a series of ChatConnections.
class ChatServer
which represents the server. Internally this would seperate the connections into logical groups. For instance it might have a GroupConnection for each chatroom on the server, and a bunch of SingleConnections to represent all of the users connected to this server. That kind of thing.
It would also translate the raw messages into something comprehensive, and direct them where they need to go.
Note that this should probably be further broken down. For example ChatServer could contain a series of ChatRooms... or something like that.
The key here is that it builds up. SingleConnection doesn't need to know that it's working with a chat program. It only needs to be concerned about sending and receiving data over a network.
ChatClient doesn't need to know how to send/receive data over a network, it can just use the SingleConnection interface to do all of that.
etc, etc.
IntMain wrote: |
---|
i kinda get what your saying , but then how come people can write an OS with C it has no classes right? |
You can write OOP in C. I'm sure all modern OS's employ OOP concepts in their code, regardless of what language they were coded in.
The C++ language features just make OOP easier to do. But you can have the same constructs without them.
(Though I guess this depends on how strictly you define "OOP". I remember getting into an argument on this forums about that before)