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.
#include <iostream>
using namespace std;
//define class fruit
class Fruit{
public:
static int count_fruits;
Fruit()
{
count_fruits++;
}
~Fruit()
{
count_fruits--;
}
};
//define apples class
class Apples: public Fruit{
public:
static int count_apples;
Apples():Fruit()
{
count_apples++;
}
~Apples()
{
count_apples--;
}
};
//define mangoes class
class Mangoes: public Fruit{
public:
static int count_mangoes;
Mangoes():Fruit()
{
count_mangoes++;
}
~Mangoes()
{
count_mangoes--;
}
};
int Fruit::count_fruits = 0;
int Apples::count_apples = 0;
int Mangoes::count_mangoes = 0;
int main()
{
Apples a1,a2,a3,a4,a5,a6;
Mangoes m1,m2,m3,m4,m5;
cout << "Total number of fruits: " << Fruit::count_fruits << endl;
cout << "Number of apples: " << Apples::count_apples << endl;
cout << "Number of mangoes: " << Mangoes::count_mangoes << endl;
return 0;
}
Comments
Leave a comment