0

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:

  • Do I need to inherit from my Sale class into my Register class (and if so, how)?
  • Can I have a generic example of a dynamic array?

Edit: We cannot use vectors.

4
  • Is a Register a kind of Sale? Is a Sale a kind of Register? If no to both, then no inheritance between them is needed. Commented Jun 22, 2012 at 2:07
  • If I don't need to inherit, how do I use the Sale object from within my Register class? Commented Jun 22, 2012 at 2:11
  • Register should have Sale objects, not be a Sale object. Composition/aggregation is what you want. Implement something like std::vector or std::list that manages a dynamic allocation of Sale objects but meets the requirements that the course lays out. Commented Jun 22, 2012 at 2:18
  • Ok, I'm asking about the requirements right now. If vectors are allowed, vectors are the way to go. Commented Jun 22, 2012 at 2:26

3 Answers 3

3

No inheritance is required. A generic example:

std::vector<Sale> sales;

Gotta love templates.

Sign up to request clarification or add additional context in comments.

3 Comments

I think vectors would be a good use if I had no grading restrictions but he says this about the dynamic array we need to create: "There is no size limit to the sales list, so it should be implemented with a dynamically allocated array. (This means you'll need an array of Sale objects). 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)." Does this fit within the use of vectors?
std::vector is a fantastic implementation of a dynamic array. If you're not allowed to use it, take a look at the behavior it has and the principles behind it for guidance. But no, the <=5 unused spots is not something that std::vector does automatically.
@ConnorBlack, Have a look at the std::vector::reserve function.
2

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.

Comments

0

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.

4 Comments

This is a homework assignment. He can't just not do it according to the specifications because someone on the internet told him it's bad practice. Probably the teacher just wanted to make things simple, while still forcing the students to implement a dynamicly sized array on their own so that they will understand how it works.
The teacher is making things more complicated though. I understand that he has to do it according to specs, I'm just trying to make him aware that in 99% of cases in live code what the teacher wants him to do is pessimal.
True, but in 99% of programming classes, they make the students do it this way first before learning about vectors so that they understand the general idea of what vectors are doing.
OK. I'll concede that point. But IMNSHO it would be better to tell them to write their own dynamic array without using the STL instead of placing such subjective restrictions on the code they write.

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.