Question #188366

Create a class named Fruit with a 

data member to calculate the number 

of fruits in a basket. Create two other 

class named Apples and Mangoes 

derived from class Fruit to calculate the 

number of apples and mangoes in the 

basket. Print the number of fruits of 

each type and the total number of fruits 

in the basket.


Expert's answer

#include <iostream>
using namespace std;
static int mangoes = 0, apples = 0, fruits = 0;
class Fruit{
    public:
    Fruit(){
        fruits++;
    }
    void display(){
        cout<<apples<<" apples\n";
        cout<<mangoes<<" mangoes\n";
        cout<<fruits<<" fruits\n";
    }
};
class Apples:public Fruit{
    public:
    Apples(): Fruit(){
        apples++;
    }
};
class Mangoes: public Fruit{
    public:
    Mangoes(): Fruit(){
        mangoes++;
    }
};
int main(){
    Apples i, j, k, l;
    Mangoes m, n, o, p;
    p.display();
    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS