C++ can';t显示受保护类内的变量

C++ can';t显示受保护类内的变量,c++,C++,尝试打印时无法访问变量“shipcost”。我要么得到0.00,要么得到十六进制的地址。不知道为什么会发生这种情况,我在主类包中声明了它在内部受保护,然后在私有视频游戏子类中再次明确声明为4.99。在其他子类中,它们是不同的…我应该注意到,这是它受到保护的全部原因。所以4.99不是一个常数 但请注意,在计算成本和总成本时,它会返回正确的美元金额。所以它被正确地使用了,但我尝试使用“cout我不确定您是如何尝试访问变量的,但是一个简单的公共函数将打印正确的值: class Video_Games

尝试打印时无法访问变量“
shipcost
”。我要么得到0.00,要么得到十六进制的地址。不知道为什么会发生这种情况,我在主类
包中声明了它在内部受保护,然后在私有
视频游戏子类中再次明确声明为
4.99
。在其他子类中,它们是不同的…我应该注意到,这是它受到保护的全部原因。所以4.99不是一个常数


但请注意,在计算成本和总成本时,它会返回正确的美元金额。所以它被正确地使用了,但我尝试使用“cout我不确定您是如何尝试访问变量的,但是一个简单的公共函数将打印正确的值:

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 );

您没有提出问题。但是,因为您声明:

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

以及


“我试着使用
cout”所以它被正确地使用了…-事实上不是。你不能“重写”C++中的成员变量的值。使用参数初始化构造函数中的成员变量,或者创建一个返回装运成本的虚拟成员函数。看起来基类和派生类中都有“shipcost”。那会让你今天过得很糟糕。哈哈,谢谢你澄清这一点。我有一种预感,shipcost不返回任何东西将是一件令人头痛的事。我不知道如何创建一个基于派生类标识符进行区分的虚拟函数。所以我想我会坚持使用变量,为每个派生类使用一个参数?我很难理解这意味着什么re:我的代码。我可以打印值,这不是问题。我试图创建一个变量,该变量响应所使用的类。因此,如果我在Package类中使用“shipcost”作为变量,它应该响应每个子类,并根据需要更改为适当的成本。因此,对于视频游戏,它将是4.99,对于手机,它将是9.99,等等。同样,我的“成本”不仅仅是一个单一的“成本”,它是一个取决于输入的变量。我在弥合通过变量传递的常量数和通过main中的参数传递的数字之间的差距时遇到了困难。我正试图将其与位于顶部的Package类中的所有其他数据一起打印。它是包含所有其他类的大类。@多态性-我添加了更多代码供您查看。
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