ostream
for output and istream
for input.<<
) for output>>
) for inputHere’s a simple example that demonstrates how to create a class, overload the stream operators, and use object streams for serialization and deserialization.
First, define a class with some data members.
#include <iostream>
#include <string>
#include <fstream>
class Person {
public:
std::string name;
int age;
Person() : name(""), age(0) {} // Default constructor
Person(const std::string& name, int age) : name(name), age(age) {} // Parameterized constructor
};
Overload the <<
operator for output (serialization) and the >>
operator for input (deserialization).
// Overload the insertion (<<) operator
std::ostream& operator<<(std::ostream& os, const Person& person) {
os << person.name << " " << person.age;
return os;
}
// Overload the extraction (>>) operator
std::istream& operator>>(std::istream& is, Person& person) {
is >> person.name >> person.age;
return is;
}
Now you can use the overloaded operators to write to and read from streams.
int main() {
// Create a Person object
Person p1("John Doe", 30);
// Serialize the object to a file
std::ofstream outFile("person.txt");
if (outFile.is_open()) {
outFile << p1;
outFile.close();
}
// Deserialize the object from the file
Person p2;
std::ifstream inFile("person.txt");
if (inFile.is_open()) {
inFile >> p2;
inFile.close();
}
// Display the deserialized object
std::cout << "Deserialized Person: " << p2 << std::endl;
return 0;
}
Person
class has two members: name
and age
.<<
operator is overloaded to write the name
and age
of a Person
object to an ostream
.>>
operator is overloaded to read the name
and age
from an istream
into a Person
object.main
function, a Person
object p1
is created and written to a file person.txt
using the overloaded <<
operator.Person
object p2
is created and its state is read from the file person.txt
using the overloaded >>
operator.Person
object p2
is displayed to verify that the data was correctly read from the file.This example demonstrates how to use object streams in C++ for simple serialization and deserialization of objects. For more complex objects, you might need to handle additional aspects such as pointers, dynamic memory, and custom data formats.