So I’m doing this project for my OO class. We need to create two classes, Sale and Register. I’ve written Sale and most of Register. My only problem (at the moment) is that I cannot seem to access the private member data of a sale object from my register class.
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 = 5;
int numSales;
Sale* sale;
};
So I’m trying to write the RingUpSale() function right now but I can’t seem to be able to access the private field. Here’s my code:
void Register::RingUpSale(ItemType item, int basePrice){
if(numSales == listSize){
listSize += 5;
Sale * tempArray = new Sale[listSize];
memcpy(tempArray, sale, numSales * sizeof(Sale));
delete [] sale;
sale = tempArray;
}
sale[numSales]->item = item; //this works for some reason
sale[numSales]->total = basePrice; // this doesn't
if(item == 'CREDIT'){
sale[numSales]->tax = 0; // and this doesn't
sale[numSales]->total = basePrice; // and neither does this
amountMoney -= basePrice;
}
++numSales;
}
Trying to set the total and tax fields of the sale object are getting errors in Eclipse:
"Field 'total' cannot be resolved"
I’m not sure why this is or how to fix it. Any help would be appreciated. And yes I’ve added the #include "sale.h" and #include "register.h" where necessary.
In your last snippet, you say
if(item == 'CREDIT')but this is wrong for many reasons. Single quotes are used only for single characters. ‘item’ is of type ‘ItemType’ which is an enum. You should useif(item == CREDIT)because CREDIT is defined as an enum element. That statement otherwise results in behavior that has nothing to do with what you want it to do.I think it’s best to keep initialization to the constructor (see listSize).
You cannot access private members from outside the class. You should either externalize some portions of the code to another class, make public functions available that will do that processing with parameters if needed or make friend classes (which I think is frowned upon in most cases since it’s a sign of bad design (aka “I don’t know how to do X so I resort to Y”).
If you need more help maybe I can come up with some code for you tomorrow, it’s a bit late for today.