簡體   English   中英

C ++:類問題

[英]C++: Class Issues

我正試圖讓我的班級做以下事情......

  1. EmployeeHandler :將m_employeeCount初始化為零。

  2. AddEmployee :由菜單選項1調用。顯示“NEW EMPLOYEE”。 提示用戶輸入員工的名字,姓氏和工資率,一次一個。 使用Employee.Setup將員工添加到m_lstEmployee。 顯示“已添加員工m_employeeCount”。 增加m_employeeCount。

  3. EmployeeSelection :按索引顯示員工列表; 提示用戶輸入員工索引並返回索引。

  4. EditEmployee :由菜單選項2調用。使用EmployeeSelection獲取要編輯的員工的索引。 驗證索引是否有效,如果不是,則顯示錯誤消息。 使用Employee.Output顯示所選員工的當前信息。 提示用戶輸入員工的新名字,姓氏和工資率,一次一個。 使用Employee.Setup更改m_lstEmployee中的員工信息。 顯示“** Employee index updated”,其中index是用戶選擇的索引。

  5. LayoffEmployee :由菜單選項3調用。使用EmployeeSelection獲取要裁員的員工的索引。 使用Employee.Output顯示所選員工的名字,姓氏和工資率。 使用Employee.LayOff關閉員工。 顯示“員工索引下崗”,其中索引是員工索引的下放。

  6. DisplayEmployeeList :由菜單選項4調用。顯示“EMPLOYEES”。 然后使用Employee.Output顯示這樣的每個員工記錄,“[1] David Johnson,PAY:$ 5.00(CURRENT EMPLOYEE)”和前雇員記錄這樣的事情,“[2] David Johnson,PAY:$ 5.00(前者) EMPLOYEE)“,括號中的數字是m_lstEmployee中員工的索引。

  7. GetEmployee :返回GetEmployee中所選員工記錄的地址。

  8. GetEmployeeCount :返回GetEmployeeCount中的員工數。

到目前為止我有......

#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

employee.h文件如下......

#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

在過去的兩天里,我一直在努力想弄清楚我做錯了什么。 這是我第一次嘗試用C ++編寫類。 任何和所有的幫助都非常感謝。 我標記了我遇到問題的地方//

你的代碼有很多很多問題......

我將首先提供可編輯的代碼,或多或少地做你想要的。 我不確定你如何提問,但將它與你自己的代碼進行比較並閱讀一本好的c ++書......

我用矢量替換了你的數組。 我使用了構造函數來初始化Employee。 我(對我自己的沮喪)添加了std,主要是因為Employee應該生活在自己的頭文件中,並且在頭文件中使用命名空間並不好。

在c ++中,operator []是后綴(在索引表達式之后)。

此外,在正常情況下,我會嘗試在可能的情況下保持接口和實現分離。 至少我不會使用內聯函數,如果不是絕對必要的話。

新代碼......:

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

最后 - 代碼只是編譯,而不是測試。 我懷疑我可能犯了一個錯誤,但唉,時間!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM