language: C++11 (gcc-4.7.2)
date: 118 days 7 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
 
template <class ParticularFruit>
struct FruitBase
{
    FruitBase(int num) : m_inventory(num) {}
 
        float netProfit() const
        {
                auto && particular_fruit = static_cast<const ParticularFruit &>(*this);
                return particular_fruit.totalSellPrice() - particular_fruit.totalCostPrice();
        }
 
protected:
        int m_inventory;
};
 
struct Apple : FruitBase<Apple>
{
        Apple(int num, float unitCostPrice, float unitSellPrice) : 
                FruitBase(num),
                m_unitCostPrice(unitCostPrice),
                m_unitSellPrice(unitSellPrice) {}
 
        float totalCostPrice() const { return m_inventory * m_unitCostPrice; }
        float totalSellPrice() const { return m_inventory * m_unitSellPrice; }
 
private:
        float m_unitCostPrice;
        float m_unitSellPrice;
};
 
struct Orange : FruitBase<Orange>
{
        Orange(int num, float unitCostPrice, float unitSellPrice) : 
                FruitBase(num),
                m_unitCostPrice(unitCostPrice),
                m_unitSellPrice(unitSellPrice) {}
 
        float totalCostPrice() const { return m_inventory * m_unitCostPrice; }
        float totalSellPrice() const { return m_inventory * m_unitSellPrice; }
 
private:
        float m_unitCostPrice;
        float m_unitSellPrice;
};
 
template <typename Fruit>
void print_net_profit(const Fruit & fruit)
{
        std::cout << "Net profit at current prices = $ " << fruit.netProfit() << std::endl;
}
 
int main()
{
        Apple apple(100, 4.00, 4.25);
        Orange orange(50, 7.00, 7.25);
 
        print_net_profit(apple);
        print_net_profit(orange);
}