简体   繁体   中英

C++: Class Issues

I'm attempting to make my class do the following...

  1. EmployeeHandler : Initializes m_employeeCount to zero.

  2. AddEmployee : Invoked by menu option 1. Displays "NEW EMPLOYEE". Prompts the user for the employee's first name, last name, and pay rate, one at a time. Uses Employee.Setup to add an employee to m_lstEmployee. Displays "Employee m_employeeCount added". Increments m_employeeCount.

  3. EmployeeSelection : Displays the list of employees, by index; prompts the user for an employee index and returns the index.

  4. EditEmployee : Invoked by menu option 2. Uses EmployeeSelection to get the index of the employee to edit. Verifies if the index is valid and displays an error message if it is not. Uses Employee.Output to display the selected employee's current information. Prompts the user for the employee's new first name, last name, and pay rate, one at a time. Uses Employee.Setup to change the employee's information in m_lstEmployee. Displays “** Employee index updated”, where index is the user selected index.

  5. LayoffEmployee : Invoked by menu option 3. Uses EmployeeSelection to get the index of the employee to lay-off. Uses Employee.Output to display the selected employee's first name, last name, and pay rate. Uses Employee.LayOff to lay the employee off. Displays "Employee index laid off", where index is laid off employee's index.

  6. DisplayEmployeeList : Invoked by menu option 4. Displays "EMPLOYEES". Then uses Employee.Output to display every employee record something like this, "[1] David Johnson, PAY: $5.00 (CURRENT EMPLOYEE)" and a former employee record something like this, "[2] David Johnson, PAY: $5.00 (FORMER EMPLOYEE)", where the number in the brackets is the employee's index in m_lstEmployee.

  7. GetEmployee : Returns the address of the selected employee record in m_lstEmployee.

  8. GetEmployeeCount : Returns the number of employees in m_employeeCount.

So far I have...

#ifndef _EMPLOYEEHANDLER
#define _EMPLOYEEHANDLER

#include "Employee.h"

class EmployeeHandler
{
    public:
    EmployeeHandler()
    {
        m_employeeCount = 0; //undefined?
    };

    void AddEmployee()
        {
            string firstName;
            string lastName;
            float payRate;

            cout<<"NEW EMPLOYEE"<<endl;
            cout<<"First Name:"<<endl;
            cin>>firstName;
            cout<<"Last Name:"<<endl;
            cin>>lastName;
            cout<<"Pay Rate:"<<endl;
            cin>>payRate;
            Employee.Setup(firstName,lastName,payRate); //Problem here
            cout<<"**Employee m_employeeCount added"<<endl;
            m_employeeCount+=1; //m_employeeCount undefined?
        }

    void EditEmployee()
        {
            int indexEdit;
            string newFirst;
            string newLast;
            float newPay;
            cout<<"Which employee would you like to edit"<<endl;
            cin>>indexEdit;
            EmployeeSelection(indexEdit); //undefined?
            Employee.Output(); //
            cout<<"Employee new first name:"<<endl;
            cin>>newFirst;
            cout<<"Employee new last name:"<<endl;
            cin>>newLast;
            cout<<"Employee new pay rate:"<<endl;
            cin>>newPay;
            Employee.Setup(newFirst,newLast,newPay); ///
            cout<<"** Employee index updated"<<endl;
        }


    void LayoffEmployee()
        {
            EmployeeSelection();
            Employee.Output(EmployeeSelection); //Problems here
            Employee.LayOff(EmployeeSelection);
            cout<<"Employee laid off"<<endl;
        }
    void DisplayEmployeeList()
        {
            cout<<"EMPLOYEES"<<endl;
            for (int i=0; i<50; i++)
                cout<<[i]<<Employee.Output(m_1stEmployee)<<endl; //
        }

    int EmployeeSelection()
        {
            int indexNumber;
            for (int i= 0; i <50; i++)
                cout<<[i]m_1stEmployee<<endl; //
            cout<<"Which Employee Index would you like to select?"<<endl;

            cin>>indexNumber;
            for (int i = 0; i <50; i++)
                if ([i]=indexNumber) //
                    return [i]
        }


    Employee& GetEmployee( int index )
        {if (index=;                             // completely confused here
    }
    int GetEmployeeCount()
        {
            return m_employeeCount;
        };

    private:
    Employee m_lstEmployee[50];
    int m_employeeCount;
};

#endif

The employee.h file is as follows...

#ifndef _EMPLOYEE
#define _EMPLOYEE
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;

class Employee
{
    public:
    void Setup( const string& first, const string& last, float pay );
    {
        m_firstName = first;
        m_lastName = last;
        m_payPerHour = pay;
        m_activeEmployee = true;
    }

    string GetName()
    {
        return m_firstName+""+m_lastName
    };

    bool GetIsActive()
    {
        return m_activeEmployee;
    };

    void LayOff()
    {
        m_activeEmployee= false;
    };
    void Output()
        cout<<GetName()<<",PAY:$"<<fixed<<setprecision(2)<<m_payPerHour<<endl;

    private:
    string m_firstName;
    string m_lastName;
    float m_payPerHour;
    bool m_activeEmployee;
};

#endif

I've been stuck writing this class for the last two days trying to figure out what I'm doing wrong. This is the first time I've attempted to write classes in C++. Any and all help is much, much appreciated. I have marked places where I'm having problems with // .

Your code has many, many problems...

I'll start by providing compilable code that more or less does what you want. I'm not sure how you can go about asking questions, but compare it to your own code and read a good c++ book...

I've replaced your array with a vector. I've used a constructor to initialize Employee. I've (to my own dismay) added std, mainly because Employee shall live in its own header and it's not good to use a namespace in a header.

In c++ the operator[] is postfix (after the indexed expression).

Also, under normal circumstances I'll try and keep interface and implementation seperate where possible. At the least I would not use inline functions if not absolutely necessary.

The new code...:

#include <iostream>
#include <iomanip>
#include <vector>
#include <iterator>
#include <string>

class Employee
{
    public:
      //This is a constructor....
      Employee( const std::string& first, const std::string& last, float pay )
      //The members can be initialized in a constructor initializer list as below.
      : m_firstName( first ), m_lastName( last ), m_payPerHour( pay ),
        m_activeEmployee() //Scalars are initialized to zero - bool to false...
      {
      }

      std::string GetName() const
      {
        return m_firstName+" "+m_lastName;
      }

      bool GetIsActive() const
      {
        return m_activeEmployee;
      };

      void LayOff()
      {
        m_activeEmployee = false;
      }

      std::ostream& Output( std::ostream& out ) const
      {
        return out << GetName() <<",PAY:$" 
                   << std::fixed << std::setprecision(2) << m_payPerHour;
      }

    private:
      std::string m_firstName;
      std::string m_lastName;
      float m_payPerHour;
      bool m_activeEmployee;
};

inline std::ostream& operator << ( std::ostream& os, const Employee& employee )
{
  return( employee.Output( os ) );
}


class EmployeeHandler
{
  public:
    void AddEmployee()
    {
      std::string firstName;
      std::string lastName;
      float payRate;

      std::cout<<"NEW EMPLOYEE"<<std::endl;
      std::cout<<"First Name:"<<std::endl;
      std::cin>>firstName;
      std::cout<<"Last Name:"<<std::endl;
      std::cin>>lastName;
      std::cout<<"Pay Rate:"<<std::endl;
      std::cin>>payRate;
      employees_.push_back( Employee( firstName,lastName,payRate ) );
      std::cout<<"**Employee m_employeeCount added"<<std::endl;
    }

    void EditEmployee()
    {
      std::string newFirst;
      std::string newLast;
      float newPay;
      std::cout<<"Which employee would you like to edit"<<std::endl;
      int indexEdit = GetSelection();
      Employee& employee = employees_[indexEdit];
      std::cout << employee << std::endl;
      std::cout<<"Employee new first name:"<<std::endl;
      std::cin>>newFirst;
      std::cout<<"Employee new last name:"<<std::endl;
      std::cin>>newLast;
      std::cout<<"Employee new pay rate:"<<std::endl;
      std::cin>>newPay;
      employee = Employee( newFirst, newLast, newPay );
      std::cout<<"** Employee index updated"<<std::endl;
    }

    void LayoffEmployee()
    {
      int index = GetSelection();
      if( employees_[index].GetIsActive() )
      {
        std::cout << "Laying off employee:\n" << employees_[index] << std::endl;
        employees_[index].LayOff();
      }
      else
      {
        std::cerr << "Already layed off employee:" << employees_[index] << std::endl;
      }
    }

    void DisplayEmployeeList()
    {
      std::copy( employees_.begin(), employees_.end(), std::ostream_iterator<Employee>( std::cout, "\n" ) );
    }

    int GetSelection()
    {
        std::size_t indexNumber;
        std::cout << "Select an employee from the list below by specifying its number:" << std::endl;
        DisplayEmployeeList();

        do{
          while( !std::cin >> indexNumber )
          {
            std::cin.clear(); 
            std::cin.ignore();
            std::cerr << "Select a number..." << std::endl;
          }
          if( indexNumber >= employees_.size() )
          {
            std::cerr << "Select a number within range of list below:" << std::endl;
            DisplayEmployeeList();
          }
        }
        while( indexNumber >= employees_.size() );
        return indexNumber;
    }

    Employee& operator[]( std::size_t index )
    {
      return employees_[index];
    }

    const Employee& operator[]( std::size_t index ) const
    {
      return employees_[index];
    }

    std::size_t EmployeeCount() const
    {
      return employees_.size();
    }

  private:
    std::vector<Employee> employees_;
};


int main( int argc, char* argv[] )
{
  return 0;
}

Finally - the code is merely compiled, not tested. I suspect I might have made a mistake, but alas, time!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM