Link to home
Start Free TrialLog in
Avatar of SStory
SStoryFlag for United States of America

asked on

C++ Class Serialization, Encapsulation and Formatted Output

With just the std library, and being a C++ noob, I am wondering the following:

  1. Serialization:  What is the best way to serialize my class?  I have a Date class and was thinking of using << to serialize it to something in the format of YYYYMMDD. I would deserialize it by one of the constructors taking that format for a Date and creating the same valued object.
  2. Encapsulation & Formatted Output: I've been reading about true encapsulation in classes (have read about it for years) and some suggest not exposing anything in terms of the innards so it won't be tightly coupled. So for example, if I do use << to serialize, how do I offer the ability to output the date to the user in various formats? If I didn't use << for serialization, how would allow different output formats of the date also? What is the normal best practice for that?

While EE will want inline answers, Any links or books recommendations (in addition) would also be appreciated.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SStory

ASKER

sarabande,

Thanks for your responses and for bearing with these noob questions. Of course if I were clear on it I wouldn't have asked to start with ;)

Contact(SerialBuffer & buf);

Open in new window

Is that a constructor that takes a SerialBuffer to deserialize?

SerialBuffer & operator<<(SerialBuffer & buf, const Gender & gender)
{
       buf << gender.GetChar();
       return buf;
}

Open in new window

If I understand this correctly, for every class  known to man I'd need to open SerialBuffer and define operator << for that class.  Do I misunderstand? is there not a better way that avoids this?
If I am correct, wouldn't it be better if the SerialBuffer class only serialized the basic built in types and when passed to a class it used that to serialize?

I know if vb.net there is an idea called an interface where a class implements x.
Example:
Implements ISerializable

I'm trying to figure it out before going too far. At present I have only written 3 classes, and wanted to know best practice before trying to serialize them.  


This is a contract that says that any class that states that must implement all methods in the contract (interface).  Is there anything equivalent to this in C++?

std::string datestr = val.Format("YYYYMMDD");

Open in new window

Is this Format function using BOOST or something? I was trying to stay std only at least for the near term.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SStory

ASKER

Sara, I am trying to get my head around this, so bare with me just a bit more:
class Person
{
     ...
public:
     friend SerialBuffer & operator<<(SerialBuffer & buf, const Person & p)
     {
           buf << p.firstName << p.lastName << ....;
           return buf;
     }
     friend SerialBuffer & operator>>(SerialBuffer & buf, const Person & p)
     {
           buf >> p.firstName >> p.lastName << ....;
           return buf;
     }
};

Open in new window

In the above code, how do I call that? Is it like below???

SerialBuffer buf;
Person person(whatever to init);

//is this how it would look to serialize
buf << person; ???

//and to deserialize??

Open in new window

I am a bit new to operator overloading and the syntax of it.
If I am understanding correctly the << >> operator overloading would be on the SerialBuffer class
and not on Person, yet defined in Person for use with SerialBuffer as you have shown.

Then the normal << that can be used by cout or whatever would still function because it using the definition given by an ostream?

As to the .Format()
I guess my real question was, since from examples I'd seen output being like
cout << mydate;
I wondered how I would do that and get formatted output.  So apparently I'd just not use the default << for mydate.  With .format, how would I get it to cout?  Would I somehow overload << to be able do some sort of thing like
cout << mydate.formatted("YYYYMMDD") or something like that? If so would you mind showing me an example?

I sincerely appreciate your time and help as I try to understand and learn this.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SStory

ASKER

Sara,

What do you mean by global function?  Public method on Person? or what?  If not where does this global function go?

Also, when you answer the global question above, I will award you the points. I have decided to ask more questions as a new question. In case you wish to participate the link is below:

https://www.experts-exchange.com/questions/28970873/Better-understanding-on-C-Class-serialization-and-formats.html28970873
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SStory

ASKER

Thank you so much for your time and patience! BTW the other link doesn't work. Here is the link to the new questions:
HERE