Objective: Create a class called 'EMPLOYEE' that has - EMPCODE and EMPNAME as data members - member function getdata( ) to input data - member function display( ) to output data Write a main function to create EMP, an array of EMPLOYEE objects. Accept and display the details of the employee.
Code:
#include <iostream>
#include <string>
using namespace std;
class EMPLOYEE
{
private:
int EMPCODE;
string EMPNAME;
public:
// Function to input data
void getdata()
{
cout << "Enter Employee Code: ";
cin >> EMPCODE;
cin.ignore(); // To clear the newline character from the input buffer
cout << "Enter Employee Name: ";
getline(cin, EMPNAME);
}
// Function to display data
void display() const
{
cout << "Employee Code: " << EMPCODE << endl;
cout << "Employee Name: " << EMPNAME << endl;
}
};
int main() {
int n;
cout << "Enter the number of employees: ";
cin >> n;
cin.ignore(); // To clear the newline character from the input buffer
EMPLOYEE* EMP = new EMPLOYEE[n]; // Dynamically allocate array of EMPLOYEE objects
// Input data for each employee
for (int i = 0; i < n; ++i) {
cout << "\nEnter details for employee " << i + 1 << ":\n";
EMP[i].getdata();
}
// Display data for each employee
cout << "\nEmployee Details:\n";
for (int i = 0; i < n; ++i) {
cout << "\nEmployee " << i + 1 << ":\n";
EMP[i].display();
}
delete[] EMP; // Free the allocated memory
return 0;
}
No comments:
Post a Comment