Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Employee Class that stores data in three objects and then display the data for each employee on the screen using C++


 

Download  file 

Not a member!
Create a FREE account here to get access and download this file 


 

Employee Class
Write a C++ class named Employee that has the following member variables:
• name. A string that holds the employee’s name.
• idNumber. An int variable that holds the employee’s ID number .
• department. A string that holds the name of the department where the employee
works.
• position. A string that holds the employee’s job title.
The class should have the following constructors:
• A constructor that accepts the following values as arguments and assigns them to
the appropriate member variables: employee’s name, employee’s ID number,
department, and position.
• A constructor that accepts the following values as arguments and assigns them to
the appropriate member variables: employee’s name and 10 number. The
department and position fields should be assigned an empty string (“”).
• A default constructor that assigns empty strings (” “) to the name, department,
and position member variables, and 0 to the idNumber member variable.
Write appropriate mutator functions that store values in these member variables and
accessor functions that return the values in these member variables. Once you have
written the class, write a separate program that creates three Employee objects to
hold the following data.

Name                    ID Number               Department                 Position
Susan Meyers        47899                        Accounting                  Vice President
Mark Jones            39119                        IT                               Programmer
Joy Rogers             81774                        Manufacturing             Engineer

The program should store this data in the three objects and then display the data for
each employee on the screen.

Answer:



// Employee class
#include <iostream>
#include <string>
using namespace std;

// Employee Class Declaration

class Employee
{
private:
   string name;        // Employee's name
   int idNumber;       // ID number
   string department;  // Department name
   string position;    // Employee's position
   
public:
   // Constructors
   Employee(string, int, string, string);
   Employee(string, int);
   Employee();

   // Mutators
   void setName(string n)
      { name = n; }
      
   void setIdNumber(int i)
      { idNumber = i; }
      
   void setDepartment(string d)
      { department = d; }
      
   void setPosition(string p)
      { position = p; }
      
   // Accessors
   string getName()
      { return name; }
      
   int getIdNumber()
      { return idNumber; }
      
   string getDepartment()
      { return department; }
      
   string getPosition()
      { return position; }
};

// Constructor #1
Employee::Employee(string n, int i, string d, string p)
{
   name = n;
   idNumber = i;
   department = d;
   position = p;
}

// Constructor #2
Employee::Employee(string n, int i)
{
   name = n;
   idNumber = i;
   department = "";
   position = "";
}

// Constructor #3, default constructor
Employee::Employee()
{
   name = "";
   idNumber = 0;
   department = "";
   position = "";
}

// Function prototype
void displayEmployee(Employee);

// Driver program to demonstrate the class
int main()
{
   // Create an Employee object to test constructor #1.
   Employee susan("Susan Meyers", 47899, "Accounting", "Vice President");
   
   // Create an Employee object to test constructor #2.
   Employee mark("Mark Jones", 39119);
   mark.setDepartment("IT");
   mark.setPosition("Programmer");

   // Create an Employee object to test constructor #3.
   Employee joy;
   joy.setName("Joy Rogers");
   joy.setIdNumber(81774);
   joy.setDepartment("Manufacturing");
   joy.setPosition("Engineer");
   
   // Display each employee's data.
   displayEmployee(susan);
   displayEmployee(mark);
   displayEmployee(joy);
   
   return 0;
}

//**************************************************
// The displayEmployee function displays the data  *
// in the Employee object passed as an argument.   *
//**************************************************

void displayEmployee(Employee e)
{
   cout << "Name: " << e.getName() << endl;
   cout << "ID Number: " << e.getIdNumber() << endl;
   cout << "Department: " << e.getDepartment() << endl;
   cout << "Position: " << e.getPosition() << endl << endl;
}


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!