Answer to Question #109640 in C++ for Shubham shashikant dashpute

Question #109640
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.
1
Expert's answer
2020-04-15T01:54:17-0400

#include <iostream>

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