无法在受保护的类

时间:2017-02-24 03:08:29

标签: c++

无法访问变量" shipcost"当试图打印出来时。我得到0.00或十六进制的地址。不确定为什么会发生这种情况,我已在主要类Package中将其声明为受保护,然后在私有4.99子类中再次将其声明为Video Games。在其他子类中,它们是不同的...我应该注意,并且它是受到保护的全部原因。所以4.99不是常数。

但请注意,在计算成本和总成本时,它会返回正确的美元金额。所以它被正确使用但我尝试使用" cout<< shipcost<< ENDL;"在类Package中,它无法输出除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;
    }
};

2 个答案:

答案 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() {};
    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 );

我还不确定我是不是得到了它。在我看来,设置shipcost的值而不是声明一个新值,将允许每个派生类访问它们自己的值:

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

答案 1 :(得分:-1)

你没有问一个问题。但是,因为你声明:

  

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

以及

  

“我尝试在cout << shipcost << endl;内使用class Package”,除了0.00之外,它无法输出任何内容。这就是我的问题“

我必须将您想要的问题解释为:

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

我至少看到一种可能性: 4.99的值位于class Video_Games。 尝试在"cout << shipcost << endl;"中添加class Video_Games,而不是将此语句放在class Package中。 此外,您使用单词"double"两次声明两次发货成本。 尝试在class Video_Games中使用setter函数(删除“double”)