fork download
  1. #include <iostream>
  2.  
  3. class Fruit{
  4. public:
  5. virtual float totalCostPrice() const = 0;
  6. virtual float totalSellPrice() const = 0;
  7. virtual float netProfit() const = 0;
  8. };
  9.  
  10. class Fruit_PI : virtual public Fruit {
  11. protected:
  12. int m_inventory;
  13. public:
  14. Fruit_PI( int num) : m_inventory(num) {}
  15.  
  16. virtual float netProfit() const { return totalSellPrice() - totalCostPrice(); }
  17. };
  18.  
  19. class Apple : virtual public Fruit, private Fruit_PI {
  20. private:
  21. float m_unitCostPrice;
  22. float m_unitSellPrice;
  23. public:
  24. Apple(int num, float unitCostPrice, float unitSellPrice) :
  25. Fruit_PI(num),
  26. m_unitCostPrice(unitCostPrice),
  27. m_unitSellPrice(unitSellPrice) {}
  28.  
  29. virtual float totalCostPrice() const { return m_inventory * m_unitCostPrice; }
  30.  
  31. virtual float totalSellPrice() const { return m_inventory * m_unitSellPrice;}
  32. };
  33.  
  34. class Orange : virtual public Fruit, private Fruit_PI {
  35. private:
  36. float m_unitCostPrice;
  37. float m_unitSellPrice;
  38. public:
  39. Orange(int num, float unitCostPrice, float unitSellPrice) :
  40. Fruit_PI(num),
  41. m_unitCostPrice(unitCostPrice),
  42. m_unitSellPrice(unitSellPrice) {}
  43.  
  44. virtual float totalCostPrice() const { return m_inventory * m_unitCostPrice; }
  45.  
  46. virtual float totalSellPrice() const { return m_inventory * m_unitSellPrice;}
  47. };
  48.  
  49. void print_net_profit(const Fruit & fruit)
  50. {
  51. std::cout << "Net profit at current prices = $ " << fruit.netProfit() << std::endl;
  52. }
  53.  
  54. int main()
  55. {
  56. Apple apple(100, 4.00, 4.25);
  57. Orange orange(50, 7.00, 7.25);
  58.  
  59. print_net_profit(apple);
  60. print_net_profit(orange);
  61. }
  62.  
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
Net profit at current prices = $ 25
Net profit at current prices = $ 12.5