0

This program uses inheritance and 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.  
// *   
// ******************************************************************  

Not sure why ringupsale is not working!

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

I am trying to show all the contents of the sales array

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

I am just trying to show the last content of the sales array

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
    If you're struggling with this then you should definitely be using vectors. Your RingUpSale function increments the current array size before copying old elements to new, so it copies one more element than ever existed. That may crash or corrupt things. If you traced through your program in a debugger, or added some cout statements, you'd probably notice this stuff yourself. – Tony Delroy Jun 23 '12 at 16:24

1 Answers1

1

This is a problem:

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

as the local variable Sales_Array in the constructor of Register hides the member variable Register::Sales_Array, meaning Register::Sales_Array is an unintialised pointer. Register::Sales_Array is then used, causing undefined behaviour and will be the reason that Register::RingUpSale() is not working. Remove the declarations in the constructor and use an initializer list instead:

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

Note that these lines introduced two new variables, unrelated to the class:

int Current_ArraySize = 0;
int Number_of_Records = 0;

Have also added these to the initializer list above.

Finally, you are violating the Rule of Three as you have dynamically allocated members but have not defined a copy constructor and assignment operator, or at least declared them private to make Register uncopyable.

As this is C++, use a std::vector<Sale> instead of explicitly managing dynamic memory.

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Sorry for the following dumb questions:I always thought that the constructor had to be within the brackets the colon after the parameters I have never seen before – user1475807 Jun 23 '12 at 17:11