Uploaded by Software Apps

Assignment no 4

advertisement
Assignment no: 04
Q: Distinguish between object and data
serialization.
Ans:
Serialization:
In computer science, in the context of data storage, serialization (or serialisation)
is the process of translating data structures or object state into a format that can
be stored (for example, in a file or memory buffer) or transmitted (for example,
across a network connection link) and reconstructed later (possibly in a different
computer environment).
1:Object Serialization:
Object serialization consists of saving the values that are part of an object,
mostly the value gotten from declaring a variable of a class. AT the current
standard, C++ doesn't inherently support object serialization. To perform this
type of operation, you can use a technique known as binary serialization.
When you decide to save a value to a medium, the fstream class provides the
option to save the value in binary format. This consists of saving each byte to
the medium by aligning bytes in a contiguous manner, the same way the
variables are stored in binary numbers.
To indicate that you want to save a value as binary, when declaring
the ofstream variable, specify the ios option as binary. Here is an example:
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
return 0;
}
Download