1

I am doing a homework assignment for my summer OO class and we need to write two classes. One is called Sale and the other is called Register. I've written my Sale class; here's the .h file:

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

class Sale
{
public:
    Sale();         // default constructor, 
            // sets numerical member data to 0

    void MakeSale(ItemType x, double amt);  

    ItemType Item();        // Returns the type of item in the sale
    double Price();     // Returns the price of the sale
    double Tax();       // Returns the amount of tax on the sale
    double Total();     // Returns the total price of the sale
    void Display();     // outputs sale info 

private:
    double price;   // price of item or amount of credit
    double tax;     // amount of sales tax 
    double total;   // final price once tax is added in.
    ItemType item;  // transaction type
};

For the Register class we need to include a dynamic array of Sale objects in our member data. We cannot use the vector class. How is this done?

Here's my 'Register' '.h'

class Register{
public:

Register(int ident, int amount);
~Register();
int GetID(){return identification;}
int GetAmount(){return amountMoney;}
void RingUpSale(ItemType item, int basePrice);
void ShowLast();
void ShowAll();
void Cancel();
int SalesTax(int n);

private:

int identification;
int amountMoney;

};
2
  • We cannot use the vector class..., why? Anyhow, you can use new[] based on requirements. Commented Jun 22, 2012 at 3:16
  • This is a homework assignment for my OO class, he says we can't use vectors. If we could I'd be done by now... Commented Jun 22, 2012 at 3:19

1 Answer 1

1

All you need in the Register class is an array of Sale objects, and a item counter to remember how many sales were made.

For example if there are 10 items in the register you will need to do this:

int saleCount = 10;
Sale[] saleList = new Sale[saleCount];

To make the array dynamic you will need to create a new Sale array every time the sale count is incremented, and copy all the items in saleList into a new sale list. Finally add the new Sale at the end.

saleCount++;
Sale[] newSaleList = new Sale[saleCount];
//copy all the old sale items into the new list.
for (int i=0; i<saleList.length; i++){
  newSaleList[i] = saleList[i];
}
//add the new sale at the end of the new array
newSaleList[saleCount-1] = newSale;
//set the saleList array as the new array
saleList = newSaleList;
Sign up to request clarification or add additional context in comments.

1 Comment

I would recommend actually making the array bigger than you need to every time you enlarge it. Think of it this way, if you only ever increment the array size by 1, then once it's full, every new item means you have to rebuild the array. However, if you go from an array of, say, size 10, to 20, to 30, etc., then you only need to be rebuilding the array every 10 items added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.