Connor Black
Connor Black

Reputation: 7181

Dynamic Array & Class Inheritance

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.

So my two questions are:

Edit: We cannot use vectors.

Upvotes: 0

Views: 1396

Answers (3)

Robert Mason
Robert Mason

Reputation: 4039

If you CANNOT use vectors, then you can use a std::list. You really should use standard containers as much as possible - chances are that any home-rolled solution will be inferior. The standard library is extensively optimized and tested - do you really feel the need to make the same investment when you have better things to be doing than reinventing the wheel?

std::list should not allocate more space than necessary. However, it has some serious limitations. The fact that a vector and other forms of dynamic array is contiguous gives large performance advantages.

Not being able to use vectors seems like a very arbitrary limitation. The fact that they allocate more space than necessary is a feature, not a bug. It allows the container to amortize the expensive copy and/or move operations involved in a reallocation. An intelligent vector implementation should check for low memory situations and handle those gracefully. If it doesn't, you should submit patches to your standard library implementation or move to a new one. But imposing arbitrary constraints with no explanation, e.g.:

There should never be more than 5 unused slots in this array (i.e. the number of allocated spaces may be at most 5 larger than the number of slots that are actually filled with real data).

is bad practice. And where did the number FIVE come from? It's not even a power of two! If you want to hack around this limitation using std::vector::reserve, that's probably your best bet. But all that book-keeping should not need to be done.

And, agreed with everyone else: inheritance is not what you want here.

Upvotes: 0

Daniel
Daniel

Reputation: 6745

No, inheritance would not be appropriate in this case. You would want to keep track of the number of sales and the size of the array as fields in the Register class. The class definition would include this

class Register{
    private:
        int numSales;
        int arraySize;
        Sale* sales;
    public:
        Register();
        ~Register();
        void MakeSale(Sale);        
};

Register::Register(){
    numSales = 0;
    arraySize = 5;
    sales = new Sale[arraySize];
}
void Register::MakeSale(Sale s){
    if(numSales == arraySize){
        arraySize += 5;
        Sale * tempArray = new Sale[arraySize];
        memcpy(tempArray, sales, numSales * sizeof(Sale));
        delete [] sales;
        sales = tempArray;
    }
    sales[numSales] = s;
    ++numSales;
}

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

This doesn't include bounds checking or whatever other stuff you need to do when you make a sale, but hopefully this should help.

Upvotes: 2

Puppy
Puppy

Reputation: 146910

No inheritance is required. A generic example:

std::vector<Sale> sales;

Gotta love templates.

Upvotes: 3

Related Questions