用c ++创建一个对象数组

时间:2011-01-06 07:16:43

标签: c++

我正在尝试用c ++创建一个对象数组。

我正在我公司类的构造函数中创建一个employee对象:

employee obj(int tempIdNum, double tempBase, double tempSales, double tempRate);

emp[tempcount]=obj;  (this doesn't work?)

Emp是此处定义的数组的名称,位于我公司的h文件中。

 Employee emp[4]; 
抱歉委托=员工..... 这是我的佣金(员工)班级cpp

    using namespace std;


//----------------------------------------
//Name: default constructor
//Purpose: create a new object with attributes set to zero
//Parameters: none
//Returns: none
//----------------------------------------
Commission::Commission()
{
    //cout << "In default constructor of Commission class" << endl;
    idNum = 0;
    base  = 0.0;
    rate  = 0.0;
    sales = 0.0;
}


//----------------------------------------
//Name: initializing constructor
//Purpose: initialize all attributes
//Parameters:
//  idNum - new value for id Num
//  base -  new value for base amount
//  sales - new value for sales amount
//  rate -  new value for rate fraction
//Returns:  none  
//----------------------------------------       
Commission::Commission(int idNum, double base, double sales, double rate)
{
    //cout << "In initializing constructor of Commission class" << endl;
    this->idNum = idNum;    
    this->base  = base;
    this->sales = sales;
    this->rate  = rate;
}

//----------------------------------------
//Name: copy constructor
//Purpose: constructor a new object from an existing object
//Parameters: 
//  emp - current employee object
//Returns: none
//----------------------------------------
Commission::Commission(const Commission& emp)
{
    //cout << "In copy constructor of Commission class" << endl;
    idNum = emp.idNum;
    base  = emp.base;
    sales = emp.sales;
    rate  = emp.rate;
}

//----------------------------------------
//Name: operator=
//Purpose: The copy assignment method copies the rhs object
//  into the lhs object
//Parameters: 
//  rhs - object on the right hand side of the = sign
//Returns: nothing
//----------------------------------------
void Commission::operator=(const Commission& rhs)
{
    //cout << "In copy assignment of Commission class" << endl; 
    idNum = rhs.idNum;
    base  = rhs.base;
    sales = rhs.sales;
    rate  = rhs.rate;
}

//----------------------------------------
//Name: destructor
//Purpose: destruct object and print id num
//Parameters: none
//Returns: 
//----------------------------------------
Commission::~Commission()
{
    //cout << "In destructor of Commission class for id num: " << idNum << endl;
}

//----------------------------------------
//Name: setIdNum
//Purpose: set id num to a new value
//Parameters: 
//  newIdNum - new value for id num
//Returns: true if new id num is between 0 and 999, inclusively
//   otherwise false
//----------------------------------------
bool Commission::setIdNum(int newIdNum)
{
    if (newIdNum >= 0 && newIdNum <= 999)
    {
        idNum = newIdNum;
        return true;
    }
    else
        return false;
}

//----------------------------------------
//Name: setBase
//Purpose: set base to a new value
//Parameters: 
//  newBase - new value for base
//Returns: true if new base is greater zero; otherwise false
//----------------------------------------
bool Commission::setBase(double newBase)
{
    if (newBase > 0.0)
    {
        base = newBase;
        return true;
    }
    else
        return false;
}

//----------------------------------------
//Name: setRate
//Purpose: set the commission rate
//Parameters: 
// newRate - new commission rate as a percentage
//Returns: true if rate greater than zero and less than or equal to 0.20
//----------------------------------------
bool Commission::setRate(double newRate) 
{
    if (newRate > 0.0 && newRate <= 0.20)
    {
        rate = newRate;
        return true;
    }
    else
        return false;
}

//----------------------------------------
//Name: setSales
//Purpose: set sales to a new amount
//Parameters:
//  newSales - new amount of sales
//Returns: true if sales is greater than or equal to zero; otherwise false
//----------------------------------------
bool Commission::setSales(double newSales) 
{
    if (newSales >= 0.0)
    {
        sales = newSales;
        return true;
    }
    else
        return false;
}

//----------------------------------------
//Name: getIdNum
//Purpose: get the current id num
//Parameters: none
//Returns: current id num
//----------------------------------------
int Commission::getIdNum()  
{
    return idNum;
}

//----------------------------------------
//Name: getBase
//Purpose: get the current base amount of salary
//Parameters: none
//Returns: current base salary
//----------------------------------------
double Commission::getBase()  
{
    return base;
}

//----------------------------------------
 //Name: getRate
//Purpose: get the current commission rate as fraction
//Parameters: none
//Returns: current commission rate
//----------------------------------------
double Commission::getRate()
{
    return rate;
}

//----------------------------------------        
//Name: getSales
//Purpose: get current amount of sales
//Parameters: none
//Returns: current amount of sales
//----------------------------------------
double Commission::getSales()
{
    return sales;
}

//----------------------------------------
//Name: calcSalary
//Purpose: calculate commission as base + sales x commission rate
//Parameters: none
//Returns: amount of commission
//----------------------------------------
double Commission::calcSalary() 
{
    return (base + sales * rate);
}





and here is its .h


    #include <string>
using namespace std;

#ifndef Commission_H
#define Commission_H

class Commission
{
    private:
        int    idNum;   //id number of employee
        double base;    //base salary
        double rate;    //rate of commission as fraction
        double sales;   //sales on which commission rate applies

    public:
        //constructors
        Commission();               //default constructor
        Commission(int idNum, double base, double sales, double rate);  //constructor with idNum
        Commission(const Commission& orig);  //copy constructor

        //destructor
        ~Commission( );

        //copy assignment
        void operator=(const Commission& rhs);

        //mutators
        bool setIdNum(int idNum);        //set id num
        bool setBase(double base);       //set base salary
        bool setRate(double rate);       //set rate of commission
        bool setSales(double sales);     //set amount of sales for commission

        //accessors
        int    getIdNum();    //get id num;
        double getBase();     //get base salary
        double getRate();     //get commission rate
        double getSales();    //get sales for commission

        //calculate salary
        double calcSalary();  //calculate commission  
};        

#endif

7 个答案:

答案 0 :(得分:1)

如错误消息所述:employee类必须具有可访问的operator=才能编译此代码。正如您可能已经知道的那样,如果您自己不提供复制赋值运算符,编译器将声明它。

至于错误的原因,我怀疑employeeconst个数据成员或其他任何使隐式定义的运算符格式错误的结果(12.8 / 12):

  

程序不正常   如果隐式定义了复制赋值运算符的类具有:

     
      
  • const类型的非静态数据成员,或
  •   
  • 引用类型的非静态数据成员,或
  •   
  • 具有无法访问的复制赋值运算符的类类型(或其数组)的非静态数据成员,或
  •   
  • 具有无法访问的复制赋值运算符的基类。
  •   

答案 1 :(得分:0)

我假设您将数组声明为

employee emp[MAX];

所以当你

employee obj(int tempIdNum, double tempBase, double tempSales, double tempRate);
emp[tempcount] = obj

这是你做的:

  1. 创建对象obj
  2. 将该对象复制到emp[tempcount]对象(想想赋值运算符)。
  3. 关于这一部分,我不完全确定,但是当你做的时候

    employee emp[MAX];
    

    您实际创建了MAX个员工对象(全部使用默认构造函数)。

答案 2 :(得分:0)

施工似乎错了。删除类型说明符:

Employee obj(tempIdNum, tempBase, tempSales, tempRate);

或尝试:

emp[tempcount]=Employee(tempIdNum, tempBase, tempSales, tempRate);

答案 3 :(得分:0)

也许,你需要实现一个“operator =”构造函数,如下所示:

Employee& Employee::operator =(const Employee& otheremp)
{
     // to be done ... 
}

答案 4 :(得分:0)

最后但并非最不重要。试试这个:

employee array[100];

employee e(1,1.1,1.5,2.5);

array[0] = e;

应该有效

答案 5 :(得分:0)

答案 6 :(得分:-2)

制作对象数组并不好。更好的方法是创建一个指向对象的数组。

employee ** array;
array = new employee[array_count]; // create an array of object pointers
for(int i=0; i<array_count; i++)
{
  array[i] = new employee( . . . your constructor parameters . . .);
  OR
  array[i] = *(anExistingEmployeeObject);
}

请不要忘记安全删除数组:

for(int i=0; i<array_count; i++) delete array[i];
delete array;