0% found this document useful (0 votes)
24 views4 pages

OOP Lab 15 Solution 02022023 044648pm

The document describes an object oriented programming lab assignment to create an Employee class with name, ID, and salary data members. Getter and setter methods are included to access the data members. The main program uses a do-while loop to allow the user to enter multiple employee data, which is saved to a file. After data entry is complete, the program reads from the file and displays the information for each employee. Sample code is provided to implement the Employee class with appropriate methods and use file input/output streams in the main program to fulfill the assignment requirements.

Uploaded by

Nazia butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

OOP Lab 15 Solution 02022023 044648pm

The document describes an object oriented programming lab assignment to create an Employee class with name, ID, and salary data members. Getter and setter methods are included to access the data members. The main program uses a do-while loop to allow the user to enter multiple employee data, which is saved to a file. After data entry is complete, the program reads from the file and displays the information for each employee. Sample code is provided to implement the Employee class with appropriate methods and use file input/output streams in the main program to fulfill the assignment requirements.

Uploaded by

Nazia butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

OBJECT ORIENTED PROGRAMMING LAB

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;

f.open("std.txt", ios::app | ios::out | ios::in | ios::binary);

do
{
cout << "Enter Employee Data:" << endl;
e.input();

f.write(reinterpret_cast<char*>(&e), sizeof(e));

cout << "Enter another record (y / n) ? ";


cin >> ch;
} while (ch == 'y');

f.seekg(0, ios::end);
int endPos = f.tellg();
f.seekg(0);
int numberOfRecords = endPos / sizeof(Employee);

for (int i = 0; i < numberOfRecords; i++) {


f.read(reinterpret_cast<char *>(&e), sizeof(e));
cout << endl << endl;

2|Page
e.display();
}

f.close();

_getch();
return 0;
}

OUTPUT:

3|Page

You might also like