Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从相关类访问私有成员数据_C++_Inheritance - Fatal编程技术网

C++ 从相关类访问私有成员数据

C++ 从相关类访问私有成员数据,c++,inheritance,C++,Inheritance,我正在尝试为我的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

我正在尝试为我的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;
int numSales;
Sale* sale;
};
在Register类中,我需要保存Sale对象的动态数组。我能做到这一点。我的问题是“Register”中的RingUpSale()函数。我需要能够从该功能访问和修改“销售”的私人会员数据。例如:

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;
    }
我不知道如何才能让这个访问成为可能。可能是通过继承或朋友结构

在你讨论这个设计之前,请记住这是家庭作业,所以有一些愚蠢的限制。其中一个问题是,我无法根据我所写的内容修改“Sale.h”。我只能在Register.h中添加更多的私有函数

函数说明:

  • 环售 此函数允许将销售的项目类型和基价作为参数传入。这 函数应该将销售存储在销售列表中,并且应该更新销售列表中的金额 收银机。购买的物品会在登记簿上增加金额。记得吗 销售税必须加到任何售出物品的底价上。如果销售类型为信用,则您 应从登记簿中扣除该金额
还包括:

-(提示:请记住,在注册表中,您保留了一个动态的销售对象数组。这 这意味着这些函数中的大多数将使用这个数组来完成它们的工作——它们也可以
调用销售类成员函数)

制作getter和setter:

intgetx(){return\ux;}
void setX(int x_ux){ux=x_;}
私人:
int_x;
};


WARE x是您想要的变量

生成getter和setter:

intgetx(){return\ux;}
void setX(int x_ux){ux=x_;}
私人:
int_x;
};


WARE x是您想要的变量

看起来
Sale::MakeSale()
函数用于处理这些税务计算细节。给定一个项目和一个基价,它将计算税收(如果需要)并更新
总值


(我假设,尽管您不能修改
Sale.h
,但可以实现
Sale.cpp

看起来
Sale::MakeSale()
函数旨在处理这些税务计算细节。给定一个项目和一个基价,它将计算税收(如果需要)并更新
总值



(我假设虽然您不能修改
Sale.h
,但可以实现
Sale.cpp

我不允许添加更多的公共函数。@ConnorBlack,不允许使用getter和setter?这真的很奇怪。那么你不能这么做。我很震惊,这就是你需要的课堂设计吗?@Arno,显然你能做到。是的,他确切地说明了我们应该写什么函数。@ConnorBlack:不,你不能。我猜您误解了约束条件或要解决的问题。我不允许添加更多公共函数。@ConnorBlack,不允许使用getter和setter?这真的很奇怪。那么你不能这么做。我很震惊,这就是你需要的课堂设计吗?@Arno,显然你能做到。是的,他确切地说明了我们应该写什么函数。@ConnorBlack:不,你不能。我的猜测是,您误解了约束条件或要解决的问题。如果不允许您修改
Sale
类的代码,那么这将是不可能的。如果不允许您修改
Sale
类的代码,那么这将是不可能的。所以您是说使用MakeSale()RingUpSale()函数中的函数?我正在尝试执行此操作,但无法从Register.cpp中访问MakeSale()函数。我不能使用继承来让这两个类共享所有东西吗?不。继承在这里不合适,因为
Sale
不是一种
Register
,而
Register
也不是一种
Sale
(我觉得我们以前已经讨论过了)。相反在
Register::RingUpSale()
中,创建一个新的
Sale
对象,然后调用
Sale.MakeSale(item,price)。我已经实现了继承,所有的错误都消失了。我现在可以从我的注册表中访问Sale的私人成员数据。cppOk,当然,如果你不想听,那没关系。你是说在RingUpSale()函数中使用MakeSale()函数?我正在尝试这样做,但我无法从Register.cpp中访问MakeSale()函数。我不能使用继承来让这两个类共享所有东西吗?不。继承在这里不合适,因为
Sale
不是一种
Register
,而
Register
也不是一种
Sale
(我觉得我们以前已经讨论过了)。相反在
Register::RingUpSale()
中,创建一个新的
Sale
对象,然后调用
Sale.MakeSale(item,price)。我已经实现了继承,所有的错误都消失了。我现在可以从我的注册表中访问Sale的私人会员数据。cppOk,当然,如果你不想听,那没关系。