C++:第二个 child class 无法从 parent class 继承属性

C++ : second child class is unable to inherit the properties from the parent class

Write a c++ program using inheritance to display the count of apples and mangoes in a basket of fruits.

Make three classes fruit, apple and mango. Fruit as the base class and apple and mango as child classes.

The fruit class should contain all the variables and two functions to input values and calculate the total number of fruits in the basket.

The child classes apple and mango should each contain a function that prints the number of apples/mangoes in the basket

我想出了以下代码作为答案:

#include <iostream>

using namespace std;

class fruit
{
public:
    int apples = 0, mangoes = 0;
    int total_fruits = 0;
    void input_fruits()
    {
        cout << "Enter the number of apples : ";
        cin >> apples;

        cout << "Enter the number of mangoes : ";
        cin >> mangoes;
    }

    void calculate_total()
    {
        total_fruits = apples + mangoes;
        cout << "The total fruits in the basket are : " << total_fruits << endl;
    }
};

class apple : public fruit
{
public:
    void show_apples()
    {
        cout << "The number of apples in the basket is : " << apples << endl;
    }
};

class mango : public fruit
{
public:
    void show_mangoes()
    {
        cout << "The number of mangoes in the basket is : " << mangoes << endl;
    }
};

int main()
{
    apple a1;
    mango m1;
    a1.input_fruits();
    a1.show_apples();
    m1.show_mangoes();
    a1.calculate_total();
    return 0;
}

这是输出,其中芒果数不起作用:

当调用 show_mangoes() 函数时,它返回先前声明的 mangoes 值,即 0。如果我在声明时删除值,函数将返回指针值。

我无法弄清楚 mango class 无法从 fruit class 访问数据的原因。

但我想出了两种 hacky 方法 来获取输出。

  1. 在全局范围内声明变量:
#include <iostream>

using namespace std;

int apples = 0, mangoes = 0;
int total_fruits = 0;

class fruit
{
public:
    void input_fruits()
    {
        cout << "Enter the number of apples : ";
        cin >> apples;

        cout << "Enter the number of mangoes : ";
        cin >> mangoes;
    }

    void calculate_total()
    {
        total_fruits = apples + mangoes;
        cout << "The total fruits in the basket are : " << total_fruits << endl;
    }
};

class apple : public fruit
{
public:
    void show_apples()
    {
        cout << "The number of apples in the basket is : " << apples << endl;
    }
};

class mango : public fruit
{
public:
    void show_mangoes()
    {
        cout << "The number of mangoes in the basket is : " << mangoes << endl;
    }
};

int main()
{
    apple a1;
    mango m1;
    a1.input_fruits();
    a1.show_apples();
    m1.show_mangoes();
    a1.calculate_total();
    return 0;
}

输出:

  1. 继承苹果class而不是芒果:
#include <iostream>

using namespace std;

class fruit
{
public:
    int apples = 0, mangoes = 0;
    int total_fruits = 0;
    void input_fruits()
    {
        cout << "Enter the number of apples : ";
        cin >> apples;

        cout << "Enter the number of mangoes : ";
        cin >> mangoes;
    }

    void calculate_total()
    {
        total_fruits = apples + mangoes;
        cout << "The total fruits in the basket are : " << total_fruits << endl;
    }
};

class apple : public fruit
{
public:
    void show_apples()
    {
        cout << "The number of apples in the basket is : " << apples << endl;
    }
};

class mango : public apple
{
public:
    void show_mangoes()
    {
        cout << "The number of mangoes in the basket is : " << mangoes << endl;
    }
};

int main()
{

    mango m1;
    m1.input_fruits();
    m1.show_apples();
    m1.show_mangoes();
    m1.calculate_total();
    return 0;
}

输出:

能否请您解释一下为什么芒果 class 在第一版代码中无法从 parent class 访问变量。对于更好的方法的任何建议也表示赞赏。

When show_mangoes() function is called it is returning the previously declared value of the mangoes i.e. 0

问题 是您从未对 m1 使用 m1.input_fruits(),而对 a1 您确实使用了 a1.input_fruits()。并且由于您从未在 m1 上调用 input_fruits 它的数据成员 mango 仍然具有来自 in-class 初始化程序[=的值(0) 31=].

所以要解决这个问题,只需在 m1 上调用 input_fruits(),它将给出所需的输出(即用户输入的值),如下所示。

Working demo

int main()
{
    apple a1;
    mango m1;
    a1.input_fruits();
    a1.show_apples();
    
    m1.input_fruits(); //ADDED THIS which was missing before in your example 1
    m1.show_mangoes();
    a1.calculate_total();
    
}