Consider a class Fruit that contains o A data member named as Fruit Name of string type o A data member named as Quantity of type int o A data member named price-per-kg of type double o A member function displayprice(Fruit) that shows the price of the instrument while evaluating the name of the instrument object that it accepts as a reference parameter Derive two classes named Mango and Watermelon from the class Fruit that contain o A parameterized constructor to initialize data members that they inherit from the class Fruit
#include <iostream>
#include <string>
using namespace std;
class Fruit{
string Name;
int Quantity;
double price_per_kg;
public:
Fruit(){}
Fruit(string name, int quantity, double price):
Name(name),
Quantity(quantity),
price_per_kg(price)
{}
void displayprice(Fruit& f){
cout<<price_per_kg<<endl;
cout<<"Name of passed Fruit: "<<f.Name<<endl;
}
};
class Mango: public Fruit{
public:
Mango(int quantity, double price): Fruit("Mango", quantity, price){}
};
class Watermelon: public Fruit{
public:
Watermelon(int quantity, double price): Fruit("Watermelon", quantity, price){}
};
Comments
Leave a comment