This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Monday, February 27, 2023

Create a class named Employee with four data members (ID, name, position, and salary). Using member functions initialize the data members for 3 objects and display information of all three of them. Assume appropriate data types.

 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;
}

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

Slider Widget