Task 1:(Inheritance and Polymorphism)
Make 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 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;
class Fruit {
static int fruitsAmount;
public:
Fruit(){
++fruitsAmount;
}
~Fruit(){
--fruitsAmount;
}
static int amount(){
return fruitsAmount;
}
};
class Apples: public Fruit{
static int applesAmount;
public:
Apples(){
++applesAmount;
}
~Apples(){
--applesAmount;
}
static int amount(){
return applesAmount;
}
};
class Mangoes: public Fruit{
static int mangoesAmount;
public:
Mangoes(){
++mangoesAmount;
}
~Mangoes(){
--mangoesAmount;
}
static int amount(){
return mangoesAmount;
}
};
int Fruit::fruitsAmount;
int Apples::applesAmount;
int Mangoes::mangoesAmount;
int main()
{
Apples a1, a2, a3;
Mangoes m1,m2;
cout<<"Apples: "<<Apples::amount()<<endl;
cout<<"Mangoes: "<<Mangoes::amount()<<endl;
cout<<"Fruits: "<<Fruit::amount()<<endl;
return 0;
}
Comments
Leave a comment