Program:
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
int id;
string name;
string position;
double salary;
public:
Employee(int i, string n, string p, double s) {
id = i;
name = n;
position = p;
salary = s;
}
void display() {
cout << "Employee ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Position: " << position << endl;
cout << "Salary: " << salary << endl;
}
};
int main() {
Employee emp1(101, "John Smith", "Manager", 50000.0);
Employee emp2(102, "Mary Johnson", "Sales Associate", 35000.0);
Employee emp3(103, "Bob Davis", "Customer Service", 30000.0);
emp1.display();
cout << endl;
emp2.display();
cout << endl;
emp3.display();
cout << endl;
return 0;
}
#include <string>
using namespace std;
class Employee {
private:
int id;
string name;
string position;
double salary;
public:
Employee(int i, string n, string p, double s) {
id = i;
name = n;
position = p;
salary = s;
}
void display() {
cout << "Employee ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Position: " << position << endl;
cout << "Salary: " << salary << endl;
}
};
int main() {
Employee emp1(101, "John Smith", "Manager", 50000.0);
Employee emp2(102, "Mary Johnson", "Sales Associate", 35000.0);
Employee emp3(103, "Bob Davis", "Customer Service", 30000.0);
emp1.display();
cout << endl;
emp2.display();
cout << endl;
emp3.display();
cout << endl;
return 0;
}
In this program, the Employee class has four data members: id, name, position, and salary. The class also has a constructor that takes four arguments and initializes the data members. The display function is used to print the information of an Employee object.
In the main function, we create three Employee objects using the constructor and initialize them with appropriate values. Then we call the display function for each object to print its information.
No comments:
Post a Comment
If you have any doubts, please let me know