Answer to Question #188366 in C++ for Gaurav Kumar

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.


1
Expert's answer
2021-05-03T01:19:54-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS