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.(using c++ language)
#include <iostream>
using namespace std;
class Fruit
{
public:
Fruit(){};
Fruit(int n)
{
quantity = n;
}
void setQuantity(int n)
{
quantity = n;
}
virtual void display()
{
cout << "There is fruit in the basket: " << quantity;
}
int getQuantity()
{
return quantity;
}
protected:
int quantity{ 0 };
};
class Apples :public Fruit
{
public:
Apples() {};
Apples(int n)
{
quantity =n;
}
void display()override
{
Fruit::display();
cout << " apples" << endl;
}
};
class Mangoes :public Fruit
{
public:
Mangoes() {};
Mangoes(int n)
{
quantity = n;
}
void display()override
{
Fruit::display();
cout << " mangoes" << endl;
}
};
int main()
{
Apples test1(10);
Mangoes test2(15);
test1.display();
test2.display();
cout << "The total quantity of fruits in the basket: " << test1.getQuantity() + test2.getQuantity() << endl;
return 0;
}
Comments
Leave a comment