0

このプログラムは継承と動的配列を使用しています

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  
// *  
// ******************************************************************  

sales 配列のすべての内容を表示しようとしています

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  
// *  
// ******************************************************************  

sales 配列の最後のコンテンツを表示しようとしています

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

1 に答える 1

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)
{}

これらの行は、クラスに関係のない 2 つの新しい変数を導入していることに注意してください。

int Current_ArraySize = 0;
int Number_of_Records = 0;

これらも上記の初期化リストに追加しました。

最後に、メンバーを動的に割り当てたが、コピー コンストラクターと代入演算子を定義していないか、少なくともそれらをコピー不可にするように宣言していないため、3 つの規則に違反しています。privateRegister

これは C++ であるため、std::vector<Sale>動的メモリを明示的に管理する代わりに a を使用します。

于 2012-06-23T16:27:48.080 に答える