Sunday, August 13, 2017

Programming Challenge 13.2 - Employee Class

Example Files: Employee.h
                          Employee.cpp
                          EmployeeDemo.cpp

Employee.h


/* Employee.h - Specification file for the Employee class */

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>

using std::string;

class Employee
{
    private:
        string name;            // Holds the employee's name
        int idNumber;            // Holds the employee's ID number
        string department;    // Holds the name of the deparment where the employee works
        string position;        // Holds the employee's job title

    public:
        Employee(string, int, string, string);
        Employee(string, int);
        Employee();

        void setName(string);
        void setIdNumber(int);
        void setDepartment(string);
        void setPosition(string);

        string getName() const;
        int    getIdNumber() const;
        string getDepartment() const;
        string getPosition() const;
};

#endif

Employee.cpp


/* Employee.cpp - Implementation file for the Employee class */

#include "Employee.h"

/* **********************************************************
                Employee::Employee
    The constructor accepts arguments for name, ID, department
    and position.
   ********************************************************** */

Employee::Employee(string empName, int empID, string empDept, string empPos)
{
    name = empName;
    idNumber = empID;
    department = empDept;
    position = empPos;
}

/* **********************************************************
                Employee::Employee
    The constructor accepts arguments for name and ID. Empty
    strings are assigned to department and position members.
   ********************************************************** */

Employee::Employee(string empName, int empID)
{
    name = empName;
    idNumber = empID;
    department = " ";
    position = " ";
}

/* **********************************************************
                Employee::Employee
    The default constructor assigns empty strings to name,
    department, and position, and 0 to idNumber.
   ********************************************************** */

Employee::Employee()
{
    name = " ";
    idNumber = 0;
    department = " ";
    position = " ";
}

/* **********************************************************
                Employee::setName
    Assigns a value to the name member variable.
   ********************************************************** */

void Employee::setName(string empName)
{
    name = empName;
}

/* **********************************************************
                Employee::setIdNumber
    Assigns a value to the idNumber member variable.
   ********************************************************** */

void Employee::setIdNumber(int empID)
{
    idNumber = empID;
}

/* **********************************************************
                Employee::setDepartment   
    Assigns a value to the department member variable.
   ********************************************************** */

void Employee::setDepartment(string empDep)
{
    department = empDep;
}

/* **********************************************************
                Employee::setPosition
    Assigns a value to the position member variable.
   ********************************************************** */

void Employee::setPosition(string empPos)
{
    position = empPos;
}

/* **********************************************************
                Employee::getName
    Returns the employee's name.
   ********************************************************** */

string Employee::getName() const
{
    return name;
}

/* **********************************************************
                Employee::getIdNumber
    Returns the employee's ID-number.
   ********************************************************** */

int Employee::getIdNumber() const
{
    return idNumber;
}

/* **********************************************************
                Employee::getDepartment
    Returns the name of the department the employee is working
    for.
   ********************************************************** */

string Employee::getDepartment() const
{
    return department;
}

/* **********************************************************
                Employee::getPosition
    Returns the employee's job title.
   ********************************************************** */

string Employee::getPosition() const
{
    return position;
}

EmployeeDemo.cpp


/* Employee Class Demo - This program demonstrates the Employee class. */

#include <iostream>
#include <iomanip>
#include "Employee.h"

void updateEmpData(Employee &, Employee &);
void displayEmpData(const Employee);

int main()
{
    Employee susanM("Susan Mayers", 47899, "Accounting", "Vice President");
    Employee markJ("Mark Jones", 39119);
    Employee joyR;

    std::cout << "ISHIKAWA FRUIT COMPANY - EMPLOYEE DATABASE\n\n";

    // Display the initial data about the three employee's
    std::cout << "\nCurrent Employee Data:\n"
               << "----------------------------------------";
    displayEmpData(susanM);
    displayEmpData(markJ);
    displayEmpData(joyR);

    // Update and set data for the 'markJ' and 'joyR' objects
    updateEmpData(markJ, joyR);

    // Display the updateded employee data for all employee objects
    std::cout << "\n\nUpdated Employee Data:\n"
               << "----------------------------------------";
    displayEmpData(susanM);
    displayEmpData(markJ);
    displayEmpData(joyR);

    std::cout << "\n\nISHIKAWA FRUIT COMPANY - Your number one fruits supplier!";

    std::cin.ignore();
    std::cin.get();

    return 0;
}

/* **********************************************************
                setEmpData (accepts two Employee objects)
    This function updates the employee data for the 'markJ'
    object, which only holds the name and ID number. It also
    sets the complete data for the 'joyR' object, holding only
    the default values.
   ********************************************************** */

void updateEmpData(Employee &markJ, Employee &joyR)
{
    // Update the data for the markJ object
    markJ.setDepartment("IT");
    markJ.setPosition("Programmer");

    // Set data for the joyR object
    joyR.setName("Joy Rogers");
    joyR.setIdNumber(81774);
    joyR.setDepartment("Manufacturing");
    joyR.setPosition("Engineer");
}

/* **********************************************************
                displayEmpData (accepts an Employee object)
    This function displays the data about each of the three
    employee's.
   ********************************************************** */

void displayEmpData(const Employee empData)
{
    std::cout << "\nEmployee Name: " << std::setw(25) << empData.getName() << "\n"
                 << "ID Number:     "    << std::setw(25) << empData.getIdNumber() << "\n"
                 << "Department:    "   << std::setw(25) << empData.getDepartment() << "\n"
                 << "Position:      "   << std::setw(25) << empData.getPosition() << "\n";
}

Example Output:



No comments:

Post a Comment