0% found this document useful (0 votes)
17 views

Class Private Int Public Int: String String String String

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Class Private Int Public Int: String String String String

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Serialization

• Serialization is a process of string and retrieving state of an object


• Class must have a default constructor
class Student
{
private:
string name;
int roll;
string branch;
public:
Student(){}
Student(string n,int r,string b)
{
name=n;
roll=r;
branch=b;
}
friend ofstream & operator<<(ofstream &ofs,Student s);
friend ifstream & operator>>(ifstream &ifs,Student &s);
friend ostream & operator<<(ostream &os,Student &s);
};
ofstream & operator<<(ofstream &ofs,Student s)
{
ofs<<s.name<<endl;
ofs<<s.roll<<endl;
ofs<<s.branch<<endl;
return ofs;
}
ifstream & operator>>(ifstream &ifs,Student &s)
{
ifs>>s.name;
ifs>>s.roll;
ifs>>s.branch;
return ifs;
}
ostream & operator<<(ostream &os,Student &s)
{
os<<"Name "<<s.name<<endl;
os<<"Roll "<<s.roll<<endl;
os<<"Branch "<<s.branch<<endl;
return os;
}
int main()
{
ofstream ofs("Test.txt");
Student s1("John",10,"CS");
ofs<<s1;
ofs.close();
Student s2;
ifstream ifs("Test.txt");
ifs>>s1;
cout<<s1;

You might also like