我的ringupsale,showlast和取消功能有问题

时间:2012-06-23 16:21:14

标签: c++ dynamic-arrays

该程序使用继承和动态数组

enum ItemType {BOOK, DVD, SOFTWARE, CREDIT};  

class Sale  
{  
public:  

Sale();  
void MakeSale(ItemType x, double amt);  
ItemType Item();  
double Price();  
double Tax();  
double Total();  
void Display();  

private:  

double price;  
double tax;  
double total;  
ItemType item;  

}  

//****************************SALE.CPP*******************************  


#include<iostream>  
#include<cmath>  
#include "sales.h"  
using namespace std;  


Sale::Sale()  
{  
    price = 0;  
    tax = 0;  
    total = 0;  
}  

// Credit are suppose to have no tax all other purchases have 

void Sale::MakeSale(ItemType x, double amt)  
{  
    item = x;  
    if(item == CREDIT)  
       total = amt;  
    else  
       total = (amt * tax);  
}  

ItemType Sale::Item()  
{  
    return item;  
}  

double Sale::Price()  
{  
    return price;  
}  

double Sale::Tax()  
{  
    return tax;  
}  

double Sale::Total()  
{  
    return total;  
}  

// display function 

void Sale::Display()  
{  
if(item = BOOK)  
    cout<<"BOOK"<<"    "<<'$'<<price<<"    ";  
    cout<<"Tax"<<"  "<<'$'<<tax<<"    "<<"Total:";  
    cout<<"  "<<'$'<<total<<endl;  
if(item = DVD)  
    cout<<"DVD"<<"    "<<'$'<<price<<"    ";  
    cout<<"Tax"<<"  "<<'$'<<tax<<"    "<<"Total:";  
    cout<<"  "<<'$'<<total<<endl;  
if(item = SOFTWARE)  
    cout<<"SOFTWARE"<<"    "<<'$'<<price<<"    ";  
    cout<<"Tax"<<"  "<<'$'<<tax<<"    "<<"Total:";  
    cout<<"  "<<'$'<<total<<endl;  
if(item = CREDIT)  
    cout<<"CREDIT" <<"    "<<'$'<<price<<"    ";  
    cout<<"Tax"<<"  "<<'$'<<tax<<"    "<<"Total:";  
    cout<<"  "<<'$'<<total<<endl;  
  }
//****************************Register.h*******************************  

#include<iostream>  
#include "sales.h"  
using namespace std;  


const int BLOCKSIZE = 5;  

class Register : Sales  
{  
public:  

Register(int id, double start_amount);  
~Register();  
int GetID();  
double GetAmount();  
void RingUpSale(enum ItemType Saletype, double amount);  
void ShowLast();  
void ShowAll();  
void Cancel();  
double SalesTax(int n);  

private:  

int Register_ID;  
int Current_ArraySize;  
int Number_of_Records;  
double Register_Amount  
Sale *Sales_Array;   
Sale *Temp_Array;  
void expandlist();  
void shrinklist();  
}
//******************************Register.cpp*********************************  

#include<iostream>  
#include "register.h"  
using namespace std;  

// *******************************************************************  
// * Initializing these values for a starting value  
// *******************************************************************  

int Current_ArraySize = 0;  
int Number_of_Records = 0;  

// ******************************************************************  
// *  
// * Function: Constructor for Register  
// * Parameters: ID number and starting amount  
// *  
// * Description: Initializes the ID number and the register amount  
// *   
// ******************************************************************  

Register::Register(int id, double amount)  
{  
    int * Sales_Array;  
    Sales_Array = new int[BLOCKSIZE];  
    int Register_ID = id;  
    double Register_amount = amount;  
} 

// ******************************************************************  
// *  
// * Function: Destructor for Register  
// * Parameters:  
// *  
// * Description: Deletes Sales Array  
// *  
// ******************************************************************  

Register::~Register()  
{
    delete[] Sales_Array;  
}

// ******************************************************************  
// *  
// * Function: GetID  
// * Parameters:  
// *  
// * Description: returns ID  
// *  
// ******************************************************************  

int Register::GetID()  
{  
    return Register_ID;  
}  

// ******************************************************************  
// *  
// * Function: GetAmount  
// * Parameters:  
// *  
// * Description: returns amount  
// *  
// ******************************************************************  

double Register::GetAmount()  
{  
    return Register_Amount;  
}  

// ******************************************************************  
// *  
// * Function: RingUpSale  
// * Parameters: enum ItemType amd amount  
// *   
// * Description: Takes in items sold and amount then adds them   
// *     to the previous list.  
// *   
// ******************************************************************  

不确定为什么ringupsale不起作用!

void Register::RingUpSale(enum ItemType Saletype, double amount)  
{  
Current_ArraySize++;  

newSale_Array = new Sales_Array[Current_ArraySize];  

//copy all the old sale items into the new list.  

for (int i=0; i<Current_ArraySize; i++)  
{  
newSale_Array[i] = Sales_Array[i];    
}  

//add the new sale at the end of the new array  

newSale_Array[Current_ArraySize-1] = newSale;  

//set the sale array as the new array  

Sales_Array = newSale_Array;  

Number_of_Records++;  
}
// ******************************************************************  
// *   
// * Function: ShowAll  
// * Parameters:  
// *   
// * Description: Lists the contents of the register  
// *  
// ******************************************************************  

我正在尝试显示销售数组的所有内容

void Register::ShowAll()  
{  
    int i;  
    cout << "the elements int the array are:" <<endl;  
//  for(i=0; i<Current_ArraySize; i++)
//  cout << Sales_Array(Sales_Array+i) << endl;  
}  

// ******************************************************************  
// *  
// * Function: ShowLast  
// * Parameters:  
// *  
// * Description: Show the last transaction  made  
// *  
// ******************************************************************  

我只是想显示销售数组的最后一个内容

void Register::ShowLast()  
{  
    cout << "The last sale was:" << endl;  
//    cout << Sale[Current_ArraySize - 1] << endl;  
}  

// ******************************************************************  
// *  
// * Function: Salestax  
// * Parameters:  
// *  
// * Description: returns the amount of salestax  
// *  
// ******************************************************************  

double Register::SalesTax(int n)  
{
    double  RunTotal=0;  
    if (n<0)  
    {  
        cout<<"Invalid entry.";  
        return 0;  
    }  
    else if (n>Number_of_Records)  
    {  
        for(int i=0; i<=Number_of_Records; i++)  
        {
            RunTotal=RunTotal+Sales_Array[i].Tax();  
            return RunTotal;  
        }  
    }  
    else  
    {  
        for(int i=0; i<=n; i++)  
            {  
                    RunTotal=RunTotal+Sales_Array[i].Tax();  
                    return RunTotal;  
            }  
    }  
}  

1 个答案:

答案 0 :(得分:1)

这是一个问题:

Register::Register(int id, double amount)  
{  
    int * Sales_Array;  
    Sales_Array = new int[BLOCKSIZE];  
    int Register_ID = id;  
    double Register_amount = amount;  
}

因为Sales_Array的构造函数中的局部变量Register隐藏了成员变量Register::Sales_Array,意味着Register::Sales_Array是一个未初始化的指针。然后使用Register::Sales_Array,导致未定义的行为,这将是Register::RingUpSale()无效的原因。删除构造函数中的声明并改为使用初始化列表:

Register::Register(int id, double amount) :
    Current_ArraySize(0),
    Number_of_Records(0),
    Sales_Array(0),
    Register_ID(id),
    Register_Amount(amount)
{}

请注意,这些行引入了两个与该类无关的新变量:

int Current_ArraySize = 0;
int Number_of_Records = 0;

还将这些添加到上面的初始化列表中。

最后,您违反了Rule of Three,因为您已动态分配成员,但尚未定义复制构造函数和赋值运算符,或者至少声明它们private以使Register不可复制。

由于这是C ++,请使用std::vector<Sale>而不是显式管理动态内存。