Answer to Question #184022 in C++ for Muhammad Adan

Question #184022

Make a class named Fruit and Vegetables with a data member to calculate the number of fruits and Vegetables in a basket. Create four other class named Apples, Mangoes , carrot and Potato to calculate the number of Fruits and Vegetables in the basket. Print the number of fruits and Vegetables of each type and the total number of fruits in the basket.


1
Expert's answer
2021-04-21T12:42:28-0400
#include <iostream>
using namespace std;
static int mangoes = 0, apples = 0, potatoes = 0, carrots = 0;
class Fruit_and_Vegetables{
    public:
    Fruit_and_Vegetables(){}
    void display(){
        cout<<apples<<" apples\n";
        cout<<mangoes<<" mangoes\n";
        cout<<carrots<<" carrots\n";
        cout<<potatoes<<" potatoes\n";
        cout<<mangoes + apples<<" fruits\n";
        cout<<carrots + potatoes<<" vegetables\n";
    }
};
class Apples{
    public:
    Apples(){
        apples++;
    }
};
class Mangoes{
    public:
    Mangoes(){
        mangoes++;
    }
};
class Carrots{
    public:
    Carrots(){
        carrots++;
    }
};
class Potato{
    public:
    Potato(){
        potatoes++;
    }
};
int main(){
    Potato a, b, c, d;
    Carrots e, f, g, h;
    Apples i, j, k, l;
    Mangoes m, n, o, p;
    Fruit_and_Vegetables basket;
    basket.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