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