-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray-of-object-code.cpp
62 lines (57 loc) · 1.5 KB
/
Array-of-object-code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @brief This program demonstrates the implementation of an array of objects in C++.
*
* The program defines a class `emp` to represent an employee with private attributes `id`, `name`, and `salary`.
* The class provides public member functions `getdata` and `putdata` to set and display the employee data, respectively.
*
* In the `main` function, an array of `emp` objects `e1` is declared with a size of 23.
* The user is prompted to enter the number of employee data to be stored.
* Then, a loop is used to get and display the employee data using the `getdata` and `putdata` functions.
*
*/
// Program in C++ to implement array of object
// Including Header Files
#include <iostream>
// Using namespace
using namespace std;
// Employee class
class emp
{
private:
int id;
public:
string name;
float salary;
void getdata(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void putdata()
{
cout << "Employee details are:" << endl;
cout << "Id is:" << id << "" << endl;
cout << "Name is:" << name << "" << endl;
cout << "Salary is:" << salary << "" << endl;
}
};
// Main function
int main()
{
emp e1[23];
int n;
cout << "Enter number of employees data to be stored:";
cin >> n;
int id, i;
char name[100];
float sa;
for (i = 0; i < n; i++)
{
cout << "Enter id,name and salary:\n";
cin >> id >> name >> sa;
e1[i].getdata(id, name, sa);
e1[i].putdata();
}
return 0;
}