Home > Back-end >  C : second child class is unable to inherit the properties from the parent class
C : second child class is unable to inherit the properties from the parent class

Time:05-19

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

I have come up with the following code as the answer :

#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;
}

Here is the output, where the mangoes count doesn't work:

image

When the show_mangoes() function is called, it is returning the previously declared value of the mangoes i.e. 0. If I remove the value at declaration, the function is returning the pointer value.

I couldn't figure out the reason of why the mango class is not able to access the data from the fruit class.

But I was able to come up with two hacky ways of getting the output.

  1. Declaring the variables in global scope:
#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;
}

Output:

output for global variables

  1. Inheriting apple class instead of mango:
#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;
}

Ouput :

inheriting from apple class

Can you please explain why the mango class was unable to access the variable from the parent class in the first version of the code. Any suggestions for better approach are also appreciated.

CodePudding user response:

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

The problem is that you never used m1.input_fruits() for m1 while for a1 you did use a1.input_fruits(). And since you never called input_fruits on m1 its data member mango still has the value(0) from the in-class initializer.

So to solve this just call input_fruits() on m1 and it will give the desired output(which is the value entered by the user), as shown below.

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();
    
}
  • Related