C++ Program for Reading & Writing an Object in Single Program

Write a program for reading & writing an object in a single program.
- Write a C++ Program to Create a File
- Write a C++ Program to Read a File
- C++ Program to Implement Pre-increment and Post-increment Operators
- C++ Program to Implement Unary Operator Overloading
- C++ Program to Implement Binary Operator Overloading
Source Code
#include<fstream> #include<iostream> #include<conio.h> using namespace std; class emp { char empname[20]; int eno; float sal; public: void getdata() { cout<<"Enter name:";cin>>empname; cout<<"Enter employee no:";cin>>eno; cout<<"Enter salary: ";cin>>sal; } void showdata() { cout<<"Name: "<<empname<<endl; cout<<"Employee No: "<<eno<<endl; cout<<"Salary: "<<sal<<endl; } }; int main() { emp em; cout<<"Enter the detail of employee"<<endl; em.getdata(); ofstream fout("emp.dat"); fout.write ((char *)&em,sizeof(em)); fout.close(); ifstream fin ("emp.dat"); fin.read((char *)&em,sizeof(em)); fin.close(); em.showdata(); getch(); }
Output