• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifdef DO_VIRTUAL_INHERITANCE
2 #define VIRTUAL virtual
3 #else
4 #define VIRTUAL
5 #endif
6 
7 #include <stdio.h>
8 
9 class Base
10 {
11 public:
Base(int val)12     Base(int val) : m_base_val (val) {}
~Base()13     virtual ~Base() {}
14 
15     virtual void
forcast(int input)16     forcast(int input) {
17         int future_val = m_base_val + input * 1;
18         printf("Forcasting %d\n", future_val);
19     }
20 
21 protected:
22     int m_base_val;
23 };
24 
25 class DerivedA : public VIRTUAL Base
26 {
27 public:
DerivedA(int val)28     DerivedA(int val) : Base(val*2), m_a_val(val) {
29         printf("DerivedA::ctor()->\n");
30         printf("m_base_val=%d\n", m_base_val);
31         printf("m_a_val=%d\n", m_a_val);
32     }
~DerivedA()33     virtual ~DerivedA() {}
34 
35 private:
36     int m_a_val;
37 };
38 
39 class DerivedB : public VIRTUAL Base
40 {
41 public:
DerivedB(int val)42     DerivedB(int val) : Base(val), m_b_val(val*3) {
43         printf("DerivedB::ctor()->\n");
44         printf("m_base_val=%d\n", m_base_val);
45         printf("m_b_val=%d\n", m_b_val);
46     }
~DerivedB()47     virtual ~DerivedB() {}
48 
49     virtual void
forcast(int input)50     forcast(int input) {
51         int future_val = m_b_val + input * 2;
52         printf("Forcasting %d\n", future_val);
53     }
54 
55 private:
56     int m_b_val;
57 };
58 
59 int
main(int argc,char ** argv)60 main(int argc, char **argv)
61 {
62 	DerivedA* dA = new DerivedA(10);
63 	DerivedB* dB = new DerivedB(12);
64 	Base *array[2] = {dA, dB};
65     Base *teller = NULL;
66     for (int i = 0; i < 2; ++i) {
67         teller = array[i];
68         teller->forcast(i); // Set breakpoint here.
69     }
70 
71     return 0;
72 }
73