0

I'm currently studying c++ and I'm stuck with this lab work given, I have five fruits and each has different traits. I'm not sure how I could apply them for each fruits. should I just split them into different classes?

 void Fruit::sale()
{
    stocks -= units;
    income += price;
    cout << "Stocks = " << stocks << endl;
    cout << "Income = " << char(156) << income << endl;

I was thinking of using arrays as a solution but I've never come across arrays with classes and I'm still new with classes, inheritance and polymorphism. Thank you for the help in advance.

JiRiv
  • 3
  • 3
  • If these traits can be entirely represented in variables, different price, different name, etc... don't use inheritance. Somewhat related: https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle . – user4581301 May 31 '18 at 04:54
  • Unrelated. None of those setters are really setters. You can, and probably should, should do all of that work inside the `Fruit` constructor. – user4581301 May 31 '18 at 04:55
  • What do you mean by each fruit has different traits? Could you give an example of some of your fruits, and how their traits differ (and maybe any similar ones)? If they have no similar traits, and they're all just different, then you don't need inheritance. – Tas May 31 '18 at 05:00
  • for example, an apple has a weight of 0.69 kg, the cost of its purchase from the supplier is (weight * £0.90) and the price its being sold for is (purchase value * 3) while a banana has a weight of 0.55kg with a purchase price of (weight * £0.75) and selling price of (purchase value * 5) – JiRiv May 31 '18 at 05:10

1 Answers1

2

You have different fruits, but the fruits all share things in common: they have a weight, a purchase price and a retail price.

All 3 of these are different for each fruit, but the formula for purchase price and retail price is the same.

purchase = weight * fruitPurchaseMultiplier;
retail = purchase * fruitRetailMultipler;

You can have a class that handles this:

#include <cstdint>
class Fruit
{
public:
    Fruit(double w, double weightMultiplyForPurchase, uint32_t priceMultiplyForRetail)
        : weight{w}
    {
        purchasePrice = weight * weightMultiplyForPurchase;
        retailPrice = purchasePrice * priceMultiplyForRetail;
    }

private:
    double weight;
    double purchasePrice;
    double retailPrice;
};

Here we have a Fruit which has the 3 traits mentioned, and you can see in its constructor it takes a weight, a purchase modifier and a retail modifier. You could just as easily take the ACTUAL value, but since it appears common for each fruit, you can let your constructor do the hard work for you.

Constructing fruits is now done like this:

int main()
{
    Fruit apple{ 0.69, 0.9, 3 };
    Fruit banana{ 0.55, 0.75, 5 };
}

Here you can see we have an apple which has a weight of 0.69, the modifier for its purchase price is 0.9 and the sell price is 3x. Again, you could have just supplied the actual values which might be fine for these 2 fruits, but if you have 30 fruits that won't be fun to do.

You don't need inheritance or polymorphism for this problem.

Tas
  • 7,023
  • 3
  • 36
  • 51
  • thank you! what if I was to add random generated quantities for each of them? and when I do purchase or sell them my stock and income gets affected. do I just create a stock and income function inside the class – JiRiv May 31 '18 at 10:56
  • You know, it's also common to sell by bundle or count, instead of weight. – Deduplicator May 31 '18 at 13:45