In C++, object streams refer to the mechanisms that allow the serialization and deserialization of objects to and from various forms of storage, such as files or network streams. Serialization is the process of converting an object's state into a format that can be stored or transmitted, while deserialization refers to the process of reconstructing the object from that format.
C++ provides mechanisms for data serialization (storing primitive types) and object serialization (storing entire objects). Object serialization and deserialization are useful for saving the state of objects to files or transferring objects over networks.
Data serialization refers to saving primitive data types or simple data structures (like arrays or lists) to a stream. This can be achieved using I/O streams in C++.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int a = 10;
double b = 3.14;
char c = 'X';
// Creating an output file stream to serialize the data into a file
ofstream outFile("data.txt");
// Serializing primitive data types to the file
outFile << a << endl;
outFile << b << endl;
outFile << c << endl;
outFile.close(); // Close the file
return 0;
}
Explanation:
a, b, and c are written to the file data.txt using ofstream. Each value is written to a new line.<< operator is overloaded in C++ streams to handle the writing of data.Object serialization involves converting an entire object (which can contain multiple members, including other objects) into a format that can be stored or transmitted.
For serializing objects, we use output file streams (ofstream) for storing data and input file streams (ifstream) for reading the data back.
To implement object serialization, we need to:
#include <iostream>
#include <fstream>
using namespace std;
class Person {
private:
string name;
int age;
public:
// Constructor
Person(string name = "", int age = 0) : name(name), age(age) {}
// Function to serialize the object (write to file)
void serialize(ofstream& out) {
out << name << endl;
out << age << endl;
}
// Function to deserialize the object (read from file)
void deserialize(ifstream& in) {
getline(in, name);
in >> age;
in.ignore(); // Ignore newline character after reading age
}
// Function to display object data
void display() const {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
// Create a Person object
Person p1("Alice", 30);
// Serialize the object to a file
ofstream outFile("person.dat");
p1.serialize(outFile);
outFile.close();
// Deserialize the object from the file
Person p2;
ifstream inFile("person.dat");
p2.deserialize(inFile);
inFile.close();
// Display the deserialized object data
p2.display();
return 0;
}
Explanation:
Person class has two private data members: name (a string) and age (an integer).serialize function writes the name and age to the output file stream.deserialize function reads data from the input file stream and reconstructs the object.main() function:
Person object p1 is created and serialized to the file person.dat.Person object p2 is created, and its data is deserialized from the file.p2 is displayed using the display method.Name: Alice, Age: 30
The above example uses text-based serialization, where data is stored as human-readable text. Another method is binary serialization, where objects are stored in a binary format, which can be more compact and efficient.
#include <iostream>
#include <fstream>
using namespace std;
class Person {
private:
string name;
int age;
public:
// Constructor
Person(string name = "", int age = 0) : name(name), age(age) {}
// Function to serialize the object in binary format
void serialize(ofstream& out) {
size_t nameLength = name.length();
out.write(reinterpret_cast<char*>(&nameLength), sizeof(nameLength)); // Write the length of the name
out.write(name.c_str(), name.length()); // Write the name
out.write(reinterpret_cast<char*>(&age), sizeof(age)); // Write age
}
// Function to deserialize the object in binary format
void deserialize(ifstream& in) {
size_t nameLength;
in.read(reinterpret_cast<char*>(&nameLength), sizeof(nameLength)); // Read the length of the name
char* buffer = new char[nameLength + 1];
in.read(buffer, nameLength); // Read the name
buffer[nameLength] = '\0'; // Null-terminate the name string
name = buffer;
delete[] buffer;
in.read(reinterpret_cast<char*>(&age), sizeof(age)); // Read age
}
// Function to display object data
void display() const {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
// Create a Person object
Person p1("Bob", 25);
// Serialize the object to a binary file
ofstream outFile("person.dat", ios::binary);
p1.serialize(outFile);
outFile.close();
// Deserialize the object from the binary file
Person p2;
ifstream inFile("person.dat", ios::binary);
p2.deserialize(inFile);
inFile.close();
// Display the deserialized object data
p2.display();
return 0;
}
serialize method, we first write the length of the name (so that we can correctly read it during deserialization), followed by the name itself and then the age.deserialize method reads the length of the name, allocates a buffer to hold the name string, and then reads the name and age into the object.ios::binary in both the input and output file streams.Serialization in C++ is a powerful mechanism for saving and loading objects to and from storage or across network boundaries. By converting an object's state into a format that can be stored or transmitted, you can make objects persistent and shareable. Whether using text-based or binary serialization, it is important to handle the process carefully to ensure data integrity and portability across different systems.
Open this section to load past papers