I’m trying to write two classes, Sale and Register, for my OO class. Here are the two headers.
Sale header:
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 (described below)
private:
double price; // price of item or amount of credit
double tax; // amount of sales tax (does not apply to credit)
double total; // final price once tax is added in.
ItemType item; // transaction type
};
Register header:
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;
int listSize;
int numSales;
Sale* sale;
};
In the Register class I need to hold a dynamic array of Sale objects. I’m able to do this. My problem is with the RingUpSale() function in ‘Register’. I need to be able to access and modify the private member data of ‘Sale’ from that function. For instance:
sale[numSales]->item = item;
sale[numSales]->total = basePrice; // Gets an error
if(item == CREDIT){
sale[numSales]->tax = 0; // Gets an error
sale[numSales]->total = basePrice; // Gets an error
amountMoney -= basePrice;
}
else {
sale[numSales]->tax = 0.07*basePrice; // Gets an error
sale[numSales]->total = (0.07*basePrice)+basePrice; // Gets an error
amountMoney += basePrice;
}
I don’t know how to make this access possible. Maybe through inheritance or friend structure?
And before you rag on the design of this please keep in mind this is for homework, so there are idiotic restrictions. One of them being I can’t modify the ‘Sale.h’ from what I’ve written. And I can only add more private functions in the ‘Register.h’.
RingUpSale() function description:
- RingUpSale
This function allows the item type and base price of a sale to be passed in as parameters. This
function should store the sale in the sale list, and it should update the amount of money in the
cash register appropriately. Items that are bought will add money to the register. Remember that
sales tax must be added to the base price of any item sold. If the sale type is CREDIT, then you
should deduct the amount from the register.
Also this:
-(Hint: keep in mind that inside the register, you are keeping a dynamic array of Sale objects. This
means that most of these functions will be using this array to do their work — and they can also
call upon Sale class member functions).
It looks like the
Sale::MakeSale()function is intended to take care of these tax calculation details. Given an item and a base price, it would compute the tax (if necessary) and update thetotalvalue.(I’m assuming that although you can’t modify
Sale.h, you can implementSale.cpp.)