1
+ // source: https://leimao.github.io/blog/CPP-Virtual-Table/
2
+ class Drug
3
+ {
4
+ public:
5
+ Drug (int property) : m_property{property} {};
6
+ virtual ~Drug () = default ;
7
+ virtual void foo () = 0;
8
+ virtual void baz () = 0;
9
+ int m_property;
10
+ };
11
+
12
+ class Fruit
13
+ {
14
+ public:
15
+ Fruit (double size, int id, char country)
16
+ : m_size{size}, m_id{id}, m_country{country} {};
17
+ virtual ~Fruit () = default ;
18
+ virtual void foo () = 0;
19
+ virtual void bar (){};
20
+ double m_size;
21
+ int m_id;
22
+ char m_country;
23
+ };
24
+
25
+ class Apple : public Fruit
26
+ {
27
+ public:
28
+ Apple (double size, int id, char country, int color)
29
+ : Fruit{size, id, country}, m_color{color} {};
30
+ ~Apple () = default ;
31
+ void foo () override {}
32
+ void get_color () const {}
33
+ virtual void apple_foo () {}
34
+ int m_color;
35
+ };
36
+
37
+ class Orange : public Fruit , public Drug
38
+ {
39
+ public:
40
+ Orange (double size, int id, char country, double weight)
41
+ : Fruit{size, id, country}, Drug{4 }, m_weight{weight} {};
42
+ ~Orange () = default ;
43
+ // Overriding Fruit virtual functions.
44
+ void foo () override {}
45
+ void bar () override {}
46
+ // Overriding Drug virtual functions.
47
+ void baz () override {}
48
+ // Orange non-virtual functions.
49
+ void get_weight () const {}
50
+ // Orange virtual functions.
51
+ virtual void orange_bar () {}
52
+ double m_weight;
53
+ };
54
+
55
+ int main ()
56
+ {
57
+ double const apple_size{1.0 };
58
+ int const apple_id{1 };
59
+ char const apple_country{' c' };
60
+ int const apple_color{2 };
61
+ double const orange_size{0.8 };
62
+ int const orange_id{2 };
63
+ char const orange_country{' n' };
64
+ double const orange_weight{0.6 };
65
+ Apple apple{apple_size, apple_id, apple_country, apple_color};
66
+ Orange orange{orange_size, orange_id, orange_country, orange_weight};
67
+ }
0 commit comments