无法在受保护的类中显示变量

can't display variable inside protected class

本文关键字:显示 变量 受保护      更新时间:2023-10-16

尝试将其打印出来时无法访问变量" shipcost"。我要么获得0.00,要么在十六进制中获得地址。不确定为什么会发生这种情况,我将其声明在主类Package中的保护中,然后在私有Video Games子级中专门为4.99。在其他子类中,它们与众不同...我应该注意,这是它受到保护的全部原因。因此,4.99不是一个常数。

,但请注意,在计算中使用成本和总成本时,它会返回正确的美元金额。因此,它被正确使用,但我尝试使用" cout<&lt shipcost< endl;"在类软件包内部,它无法输出0.00以外的任何内容。那是我的问题。

class Package
{
protected:
    string name_and_address = "?";
    double cost = 0.0;
    double discount = 0.0;
    double discount_rate = 0.0;
    bool overnight_delivery = false;
    bool insured = false;
    string package_contents = "?";
    double shipcost = 0.0;
};
class Video_Games :public Package
{
private:
    int num_games = 0;
    double shipcost = 4.99; 
public:
    Video_Games(string location, int number_of_games, bool express, bool insurance)
    {
        num_games = number_of_games;
        name_and_address = location;
        overnight_delivery = express;
        insured = insurance;
        package_contents = to_string(num_games) + " Video Game(s)";
        cost = calculate_cost();
        discount = calculate_discount();
    }
    ~Video_Games() {};
protected:
    double calculate_cost()
    {
        cost = num_games * 19.99;
        if (overnight_delivery) { cost += shipcost; }
        if (insured) { cost *= 1.06; }
        return cost;
    }
};

我不确定您如何尝试访问该变量,但是简单的公共功能将打印正确的值:

class Video_Games :public Package
{
private:
    int num_games = 0;
    double shipcost = 4.99;
public:
    Video_Games( string location , int number_of_games , bool express , bool insurance )
    {
        num_games = number_of_games;
        name_and_address = location;
        overnight_delivery = express;
        insured = insurance;
        package_contents = to_string( num_games ) + " Video Game(s)";
        cost = calculate_cost();
        //discount = calculate_discount();
    }
    ~Video_Games() {};
    void PrintVars( ostream& out )
    {
        out << num_games << 't' << shipcost << 'n';
    }
protected:
    double calculate_cost()
    {
        cost = num_games * 19.99;
        if ( overnight_delivery ) {
            cost += shipcost;
        }
        if ( insured ) {
            cost *= 1.06;
        }
        return cost;
    }
};

Video_Games vg("abcde",10,true,true);
vg.PrintVars( cout );

我仍然不确定是否得到。在我看来,设置船舶的值而不是声明新的值,将允许每个派生的类访问自己的价值:

class Package
{
protected:
    string name_and_address = "?";
    double cost = 0.0;
    double discount = 0.0;
    double discount_rate = 0.0;
    bool overnight_delivery = false;
    bool insured = false;
    string package_contents = "?";
    double shipcost = 0.0;
    void PrintVars( ostream& out )
    {
        cout << "Cost = " << cost << 'n'
            << "Discount = " << discount << 'n'
            << "Discount Rate = " << discount_rate << 'n'
            << "Ship Cost = " << shipcost << 'n';
    }
};
class Video_Games :public Package
{
private:
    int num_games = 0;
public:
    Video_Games( string location , int number_of_games , bool express , bool insurance )
    {
        shipcost = 4.99;
        num_games = number_of_games;
        name_and_address = location;
        overnight_delivery = express;
        insured = insurance;
        package_contents = to_string( num_games ) + " Video Game(s)";
        cost = calculate_cost();
        //discount = calculate_discount();
    }
    ~Video_Games() {};
    void Print( ostream& out )
    {
        PrintVars( out );
    }
protected:
    double calculate_cost()
    {
        cost = num_games * 19.99;
        if ( overnight_delivery ) {
            cost += shipcost;
        }
        if ( insured ) {
            cost *= 1.06;
        }
        return cost;
    }
};
class Board_Games :public Package
{
private:
    int num_games = 0;
public:
    Board_Games( string location , int number_of_games , bool express , bool insurance )
    {
        shipcost = 3.99;
        num_games = number_of_games;
        name_and_address = location;
        overnight_delivery = express;
        insured = insurance;
        package_contents = to_string( num_games ) + " Video Game(s)";
        cost = calculate_cost();
        //discount = calculate_discount();
    }
    ~Board_Games() {};
    void Print( ostream& out )
    {
        PrintVars( out );
    }
protected:
    double calculate_cost()
    {
        cost = num_games * 19.99;
        if ( overnight_delivery ) {
            cost += shipcost;
        }
        if ( insured ) {
            cost *= 1.06;
        }
        return cost;
    }
};
Video_Games vg("abcde",10,true,true);   
vg.Print( cout );
cout << 'n';
Board_Games bg( "cdefg" , 20 , false , false );
bg.Print( cout );

这产生了此返回:

Cost = 217.183
Discount = 0
Discount Rate = 0
Ship Cost = 4.99
Cost = 399.8
Discount = 0
Discount Rate = 0
Ship Cost = 3.99

您没有问一个问题。但是,因为您说:

"不确定为什么会发生这种情况"

以及,

"我尝试使用 cout << shipcost << endl;"在class Package内,除了0.00以外,它都无法输出任何内容。那是我的问题"

我必须将您的预期问题解释为:

当我在class Package中添加"cout << shipcost << endl;"时,以下代码为什么不输出值4.99

我至少看到一种可能性:4.99的值在class Video_Games中。尝试在class Video_Games中添加"cout << shipcost << endl;",而不是将此语句放入class Package的内部。另外,您将两次使用"double"单词宣布两次造船厂。尝试在class Video_Games(删除" double")中使用Setter函数