Answer to Question #186593 in C++ for Ravi kumar pandey

Question #186593

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)


1
Expert's answer
2021-04-28T11:29:46-0400
 #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;
}

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