OOP Lab 15 Solution 02022023 044648pm
OOP Lab 15 Solution 02022023 044648pm
OBJECT ORIENTED
PROGRAMMING LAB
Lab Journal -15
Question:01
Create a class Employee with name, Id and salary as data members. Provide appropriate constructors, set,
get and display methods in the class. In the main program, use a do-while loop to enter data for
employees as long as the user desires and save all data to a file.
Once the user is done with data entry, read the data for employees from the file
and display the information of each employee
CODE:
/*1. Create a class Employee with name, Id and salary as data members.
Provide appropriate constructors, set, get and display methods in the
class.In the main program, use a do - while loop to enter data for
employees
as long as the user desires
and save all data to a file.
Once the user is done with data entry, read the data for employees from
the file
and display the information of each employee*/
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <string.h>
#include <cstring>
using namespace std;
class Employee
{
private:
string name;
int id;
float salary;
public:
Employee() :name(" "), id(0), salary(0){}
Employee(string n, int i, float s) :name(n), id(i), salary(s){}
void input(){
cout << "Enter Name : ";
cin >> name;
cout << "Enter id : ";
cin >> id;
cout << "Enter salary : ";
cin >> salary;
1|Page
}
void display(){
cout << "Name : " << name << endl << "Id : " << id << endl <<
"Salary : " << salary;
}
string getname(){
return name;
}
int getid(){
return id;
}
float getsalary(){
return salary;
}
};
int main()
{
Employee e;
char ch;
int j = 0;
fstream f;
do
{
cout << "Enter Employee Data:" << endl;
e.input();
f.write(reinterpret_cast<char*>(&e), sizeof(e));
f.seekg(0, ios::end);
int endPos = f.tellg();
f.seekg(0);
int numberOfRecords = endPos / sizeof(Employee);
2|Page
e.display();
}
f.close();
_getch();
return 0;
}
OUTPUT:
3|Page