Overload the insertion and extraction operator to take input for the lecturerData class
below. Your operator overload functions should be capable of writing statements that take
multiple inputs and display multiple outputs as below.
lecturerData L1,L2;
cin>>L1>>L2;
cout<<L1<<L2;
class dateType
{
int day,month,year;
};
class lecturerData
{
char firstName[10];
char lastName[10];
int empid;
char designation[10];
dateType DOH;
};
#include <iostream>
#include <fstream>
using namespace std;
class dateType
{
public:
int day,month,year; //declare DATE
};
class lecturerData
{
public:
char firstName[10];
char lastName[10];
int empid;
char designation[10];
dateType DOH;
public:
friend ostream & operator<< (ostream &out, const lecturerData &l); //overloaded << function
friend istream & operator>> (istream &in, lecturerData &l); //overloaded >> function
};
//display class members uisng overloaded<< function
ostream & operator<<(ostream &out, const lecturerData &l)
{
cout<<"\nDetails of Employee\n\n";
out << "Name:: "<<l.firstName <<" "<<l.lastName<<endl;
out << "ID:: "<<l.empid<<"\t"<<"Desigantio::"<<l.designation<<endl;
out << "DATE::"<<l.DOH.day<<"/"<<l.DOH.month<<"/"<<l.DOH.year<<endl;
return out;
}
//read class members using overloaded >>function
istream & operator>> (istream &in, lecturerData &l)
{
cout<<"Enter firstName:: ";
in >> l.firstName;
cout<<"Enter lastName:: ";
in >>l.lastName;
cout<<"Enter Empid:: ";
in >>l.empid;
cout<<"Enter designation:: ";
in >>l.designation;
cout<<"Enter DOH(dd,mm,year):: ";
in >>l.DOH.day>>l.DOH.month>>l.DOH.year;
return in;
}
int main()
{
lecturerData L1,L2,L3,L4;
fstream file;
cin>>L1>>L2;
//open file "file.txt" to write as binary
file.open("file.txt",ios::out|ios::binary);
file.write((char*)&L1,sizeof(L1));
file.write((char*)&L2,sizeof(L2));
file.close();
file.open("file.txt",ios::in|ios::binary);
file.read((char*)&L3,sizeof(L3));
file.read((char*)&L4,sizeof(L4));
cout<<L3<<L4;
file.close();
return 0;
}
Comments
Leave a comment