Answer to Question #186613 in C++ for Pronoy

Question #186613

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-04-28T23:28:48-0400
#include <bits/stdc++.h>


using namespace std;






class Fruit


{


public:


  static int fruitCounter;


  Fruit()


  {


    fruitCounter++;


  }


  ~Fruit()


  {


    fruitCounter--;


  }


};






class Apples: public Fruit


{


public:


  static int applesCounter;


  Apples():Fruit()


  {


    applesCounter++;


  }


  ~Apples()


  {


    applesCounter--;


  }






};






class Mangoes: public Fruit


{


public:


  static int mangoesCounter;


  Mangoes():Fruit()


  {


    mangoesCounter++;


  }


  ~Mangoes()


  {


    mangoesCounter--;


  }


};






int Fruit::fruitCounter = 0;


int Apples::applesCounter = 0;


int Mangoes::mangoesCounter = 0;






int main()


{


  Apples firstApple,secondApple,thirdApple;


  Mangoes firstMango,secondMango,thirdMango,fourthMango;


  cout << "Total number of fruits: " << Fruit::fruitCounter << endl;


  cout << "Number of apples: " << Apples::applesCounter << endl;


  cout << "Number of mangoes: " << Mangoes::mangoesCounter << endl;


  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