Answer to Question #186801 in C++ for ayush singh

Question #186801

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-28T15:49:33-0400
#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;


}

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