Question

In header file (.h) and c++ file format (.cpp). A local company has asked you to...

In header file (.h) and c++ file format (.cpp).

A local company has asked you to write a program which creates an Employee class, a vector of Employee class objects, and fills the objects with employee data, creating a "database" of employee information. The program allows the user to repeatedly search for an employee in the vector by entering the employee's ID number and if found, display the employee's data.

The Employee_C class should have the following data and in header file format:

-employee full name

-employee ID number (int of 6 digits)

-employee salary

-employee gender (char)

-static variable to hold the total of all employees' salaries

Also create the appropriate constructors and getter and setter member functions for the class variables (hint: an employee's salary should be set, not accumulated; and before a salary is set and added to the static variable, the prior salary should be removed from the static variable).

In main, declare two vectors: one for Employee_C class objects, and one for ID numbers (int).

The program will have three functions:

1) Fill_Vector: have the user repeatedly enter employees' data into Employee objects (unknown number of employees), and place the objects into the Employee vector (hint: fill an object, then place it into the vector).

2) Extract_ID: retrieve the employees' IDs from the Employee vector and place them into the ID vector, i. e., create a vector of employee IDs.

3) Find_Employee: in main the user will repeatedly enter employee IDs and call this function, which will search the ID vector with the entered ID (must use STL algorithm "binary_search") and return either true or false via the function's return type.

Then in main: if the entered ID was found display the employee's data (from the employee vector) plus the total of all employee salaries (using the static variable), or if not found display an error message along with the erroneous ID number.

Enter the following data (full name, ID, salary, gender):

-Joe Jones, 123456, $60,000, M

-Jill James, 987654, $65,000, F

-Leonardo DeApe, 567890, $20,000, M

Then have the user enter the following IDs to search for an employee:

-987654

-123456

-135790 (should produce an error)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code:

File: Employee_C.h

#ifndef EMPLOYEE_C_H_INCLUDED
#define EMPLOYEE_C_H_INCLUDED

#include <iostream>

using namespace std;

//Class definition
class Employee_C
{
    //Private member variables
    private:
        string fullName;
        int id;
        double salary;
        char gender;

    public:
        //Public methods
        static double totalSalaries;
        Employee_C();
        Employee_C(string tName, int tId, double tSal, char tGender);
        string getName();
        int getId();
        double getSalary();
        char getGender();
        void setName(string tName);
        void setId(int id);
        void setSalary(double tSal);
        void setGender(char tGender);
};

#endif // EMPLOYEE_C_H_INCLUDED

File: Employee_C.cpp

#include <iostream>
#include "Employee_C.h"

using namespace std;

//Default Constructor
Employee_C::Employee_C()
{
    fullName = "";
    salary = 0.0;
    id = 0;
    gender = ' ';
}

double Employee_C::totalSalaries = 0;

//Argument Constructor
Employee_C::Employee_C(string tName, int tId, double tSal, char tGender)
{
    fullName = tName;
    salary = tSal;
    id = tId;
    gender = tGender;
    totalSalaries += tSal;
}

//Setter for name
void Employee_C::setName(string tName)
{
    fullName = tName;
}

//Setter for id
void Employee_C::setId(int tId)
{
    id = tId;
}

//Setter for salary
void Employee_C::setSalary(double tSal)
{
    salary = tSal;
    totalSalaries += salary;
}

//Setter for gender
void Employee_C::setGender(char tGender)
{
    gender = tGender;
}

//Getter for name
string Employee_C::getName()
{
    return fullName;
}

//Getter for id
int Employee_C::getId()
{
    return id;
}

//Getter for salary
double Employee_C::getSalary()
{
    return salary;
}

//Getter for gender
char Employee_C::getGender()
{
    return gender;
}

File: main.cpp

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

#include "Employee_C.h"

using namespace std;

//Function that fills the vector with given data
void FillVector(vector<Employee_C> &emps)
{
    string fullName;
    int id;
    double salary;
    char gender;
    char ch;

    do
    {
        //Reading employee data
        cout << "\n Enter Employee data: \n";

        cout << "\n Enter Name: ";
        getline(cin, fullName);

        //Reading employee id
        cout << "\n Enter Id: ";
        cin >> id;

        //Reading employee id
        cout << "\n Enter Salary: ";
        cin >> salary;


        //Reading employee gender
        cout << "\n Enter gender: ";
        cin >> gender;

        //Creating an employee object
        Employee_C empObj(fullName, id, salary, gender);

        //Storing into vector
        emps.push_back(empObj);

        cout << "\n Do you want to add more? (y/n): ";
        cin >> ch;

        cin.ignore();

    }while(ch=='y' || ch=='Y');
}

//Function that extracts ids
void Extract_ID(vector<Employee_C> emps, vector<int> &ids)
{
    int i;

    //Iterating over emps vector
    for(i=0; i<emps.size(); i++)
    {
        //Storing id's
        ids.push_back(emps.at(i).getId());
    }
}

//Compare function for searching
bool compare(int i,int j) { return (i<j); }

//Finding an employee
bool Find_Employee(vector<int> ids, int id)
{
    //Sorting ids
    sort(ids.begin(), ids.end());

    //Searching for id
    if (binary_search(ids.begin(), ids.end(), id, compare))
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Main function
int main()
{
    //Vector of employees
    vector<Employee_C> emps;

    //Vector of ids
    vector<int> ids;

    int id, i;

    //Fills vector with given data
    FillVector(emps);

    //Extracting ids
    Extract_ID(emps, ids);

    //Searching
    while(1)
    {
        //Reading id
        cout << "\n\n Enter employee id to search(-1 to stop): ";
        cin >> id;

        if(id == -1)
        {
            return 0;
        }

        //If found
        if(Find_Employee(ids, id))
        {
            //Searching for info
            for(i=0; i<emps.size(); i++)
            {
                //Comparing ids
                if(emps.at(i).getId() == id)
                {
                    //Printing data
                    cout << "\n Name: " << emps.at(i).getName() << " \t Gender: " << emps.at(i).getGender() << " \t Salary: " << emps.at(i).getSalary() << "\n";
                }
            }

            //Printing total salary
            cout << "\n\n Total Salary: $" << Employee_C::totalSalaries << " \n";
        }
        else
        {
            cout << "\n Invalid ID.... \n";
        }
    }

    cout << "\n\n" << endl;
    return 0;
}

_____________________________________________________________________________________________

Sample Output:

Add a comment
Know the answer?
Add Answer to:
In header file (.h) and c++ file format (.cpp). A local company has asked you to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++ MUST INCLUDE HEADER FILE, IMPLEMENTATION FILE, AND DRIVER FILE. IN C++ Create a header...

    IN C++ MUST INCLUDE HEADER FILE, IMPLEMENTATION FILE, AND DRIVER FILE. IN C++ Create a header and implementation file to define an apartment class. Create a driver program to test the class, and create a make file to compile the driver program. Create two files called apartment.h and appartmentImp.cpp along with creating a driver program named testApartment.cpp containing the main function. Program Requirements: • Class attributes should include integers for number of rooms, monthly rent, and square feet, as well...

  • C++ Linked Lists You have been hired by Employees. Inc to write an employee management system....

    C++ Linked Lists You have been hired by Employees. Inc to write an employee management system. The following are your specifications: Write a program that uses the following linked lists: bullet empId: a linked list of seven long integers to hold employee identification numbers. The array should be initialized with the following numbers: 5658845 4520125 7895122 8777541 8451277 1302850 7580489 bullet hours: a linked list of seven integers to hold the number of hours worked by each employee bullet payRate:...

  • The manager of the Telemarketing Group has asked you to write a program that will help...

    The manager of the Telemarketing Group has asked you to write a program that will help order- entry operators look up product prices. The program should prompt the user to enter a product number, and will then display the title, description, and price of the product. The program will consist of the following functions. getProdNum: it asks the user to enter a product number. Please be sure to reject any value out of the range of correct product numbers. binarySearch:...

  • The manager of the Telemarketing Group has asked you to write a program that will help...

    The manager of the Telemarketing Group has asked you to write a program that will help order- entry operators look up product prices. The program should prompt the user to enter a product number, and will then display the title, description, and price of the product. The program will consist of the following functions. getProdNum: it asks the user to enter a product number. Please be sure to reject any value out of the range of correct product numbers. C++...

  • Write a C++ program that includes the following: Define the class Student in the header file Stu...

    Write a C++ program that includes the following: Define the class Student in the header file Student.h. #ifndef STUDENT_H #define STUDENT_H #include <string> #include <iostream> using namespace std; class Student { public: // Default constructor Student() { } // Creates a student with the specified id and name. Student(int id, const string& name) { } // Returns the student name. string get_name() const { } // Returns the student id. int get_id () const { } // Sets the student...

  • Create a class “Person” which includes first name, last name, age, gender, salary, and haveKids (Boolean)...

    Create a class “Person” which includes first name, last name, age, gender, salary, and haveKids (Boolean) variables. You have to create constructors and properties for the class. Create a “MatchingDemo” class. In the main function, the program reads the number of people in the database from a “PersonInfo.txt” file and creates a dynamic array of the object. It also reads the peoples information from “PersonInfo.txt” file and saves them into the array. In C# Give the user the ability to...

  • Write a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".

    Project DescriptionWrite a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".Project SpecificationsInput for this project:the user must enter the number of employees in the company.the user must enter as integers for each employee:the employee number (ID)the number of days that employee missed during the past year.Input Validation:Do not accept a number less than 1 for the number of employees.Do not accept a negative...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

  • C++ Simple Employee Tracking System

    This assignment involves creating a program to track employee information.  Keep the following information on an employee:1.     Employee ID (string, digits only, 6 characters)2.     Last name (string)3.     First Name (string)4.     Birth date (string as MM/DD/YYYY)5.     Gender (M or F, single character)6.     Start date (string as MM/DD/YYYY)7.     Salary per year (double)Thus you must create a class that has all of this, and get/set methods for each of these fields.  Note: The fields that are designated as string should use the string...

  • J Inc. has a file with employee details. You are asked to write a C++ program...

    J Inc. has a file with employee details. You are asked to write a C++ program to read the file and display the results with appropriate formatting. Read from the file a person's name, SSN, and hourly wage, the number of hours worked in a week, and his or her status and store it in appropriate vector of obiects. For the output, you must display the person's name, SSN, wage, hours worked, straight time pay, overtime pay, employee status and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT