所以我正在爲我的OO類做這個項目。我們需要創建兩個類,銷售和註冊。我寫了Sale和大部分Register。我唯一的問題(目前)是我似乎無法從我的註冊類訪問銷售對象的私有成員數據。無法訪問私人對象數據
售頭:
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
};
註冊標題:
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;
};
所以我想現在寫RingUpSale()
功能,但我似乎無法能夠訪問私有字段。這裏是我的代碼:
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;
}
嘗試設置銷售目標的總和稅務領域越來越在Eclipse中的錯誤:
"Field 'total' cannot be resolved"
我不知道這是爲什麼或如何解決它。任何幫助,將不勝感激。是的,我已經在必要時添加了#include "sale.h"
和#include "register.h"
。
這就是讓他們成爲'私人'的想法,這樣別人就不會混淆他們了。'MakeSale'函數不能幫助你嗎? –