OOP communication protocol class

Hi
I want to create a class A that contains a communication protocol for a RS232 interface.
Furthermore I want to create a class B that use the communication protocol of class A.
It should be possible to have several instances of class B.
My first question: Is class A a static class?
Second question: should I use inharitance or a interface pattern?

Thanks
Michael
> My first question: Is class A a static class?
Do you only have one (and only every one) physical RS232 port?

Are you coming from Java/C# lands?
https://www.tutorialspoint.com/how-to-create-a-static-class-in-cplusplus

> Second question: should I use inharitance or a interface pattern?
Does B also implement a communication protocol?

Which makes more sense to you
B "is a" A - it's inheritance
B "has a" A - it's composition (or as you would say, interface)



I have two physical RS232 ports but my communication protocol uses only one RS232 port.
So every class B should use the protocol form class A and class A uses one of the RS232 ports.

I come form C. I know OOP but haven't much experience with it.


------------ ------------ -------------
| Class B | | Class B | | Class B |
------------ ------------ -------------
| | |
\ | /
\ | /
---------------------------
| Class A (Protocol) |
----------------------------
|
---------------------------
| RS232 |
----------------------------


class B
{
A port; //just a variable of class A type... is this good enough that other more complex inheritance is not necessary?
};

then you can simply use it like nested structs in C:
B var;
var.port.set_which_port(1); //whatever methods I am making stuff up here...

from there, its clear you could make a function in B that just called A's function for you, hiding the details (you can do this in C too, nothing really earth shattering yet, using function pointers to add 'methods' to structs if you want to go down that ugly road). then, instead of var.port.function you just have var.wrapperfunction(). A little cleaner. I recommend you start at this level, get it working, then investigate the next level of OOP:

From there, c++ would let you inherit it more purely, so instead of having to write a wrapper function, B can simply inherit and use A's methods directly. It then looks like var.function() where function is really implemented in A! The syntax here starts to get a little odd when coming in from C, but you can hopefully see how it avoids an extra step of writing wrapper functions. At this point, you no longer have an A variable inside B; instead, B itself is a superset of the A object, adding to it. This is where C++ starts to really diverge from C, and is a challenging step in 'thinking in c++' instead of 'thinking in C'. This is the right way to do it in c++.
Last edited on
Thanks jonnin for reply.

Yes you are right. It is hard work to 'thinking in c++' instead of 'thinking in C'.

Only to ensure that I understand in the right way.
If class B inherit from class A all instances of class B have access to methods of class B and class A. So every class B object know the comunication protocol which is encapsulated in class A.

Is it a common way to have a client-server architecture? All class B objects are clients. They register at the server. The server is the one who send and receive messages. Because of the registration of the clients the server knows which receiving message belongs to which client.

I am right?
yes, you understood it.

Client server may not need that registration. The IP address of the clients is already unique, unless you have multiple clients on the same machine, in which case, I believe there are still ways to handle it without an extra registration, but that isn't something I have needed to study. Our stuff only had one to one IP connections. (you may still want to register for some reason, but its not required for the networking).
Last edited on
Topic archived. No new replies allowed.