Added package with the documentation and the examples
[lwc.git] / doc / 2.1.abstract-template-class
blob2793c5d2fec1fca3355df7aade0679615be39c8f
2 Abstract template macro classes
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5   Abstract classes are an alternative way to deal with multiple inheritance
6 and more specifically with cases of virtual inheritance and "diamond"
7 hierarchies.  These are extreme OOP features used rarely anyway (and even when
8 used only a couple of classes will have virtual inheritance in a multi-thousand
9 line program).  So we suppose you know and really need virtual inheritance.
10 Lets see the classic example implemented with virtual inheritance first and
11 abstract classes later:
13         class helloworld_base {
14                 virtual void phello () = 0;
15                 virtual void pworld () = 0;
16            public:
17                 void greet ()
18                 { phello (); printf (" "); pworld (); printf ("!\n"); }
19         };
21         class hello : helloworld_base {
22                 void phello ()  { printf ("hello"); }
23         };
24         class HELLO : helloworld_base {
25                 void phello ()  { printf ("HELLO"); }
26         };
27         class world : helloworld_base {
28                 void pworld ()  { printf ("world"); }
29         };
30         class WORLD : helloworld_base {
31                 void pworld ()  { printf ("WORLD"); }
32         };
34         // instances
35         class hw : hello, world {};
36         class Hw : HELLO, world {};
37         class hW : hello, WORLD {};
38         class HW : HELLO, WORLD {};
40         int main ()
41         {
42                 hw a;
43                 Hw b;
44                 hW c;
45                 HW d;
47                 a.greet ();
48                 b.greet ();
49                 c.greet ();
50                 d.greet ();
51         }
53   This program shows a typical case of complex polumorphism.
54 Here is the same program with abstract classes.  An abstract class
55 is declared with the syntax "template class".  This does not
56 conflict with the other templates which are the multi-line macros.
58         class helloworld_base {
59                 virtual void phello () = 0;
60                 virtual void pworld () = 0;
61            public:
62                 void greet ()
63                 { phello (); printf (" "); pworld (); printf ("!\n"); }
64         };
66         template class hello {
67                 void phello ()  { printf ("hello"); }
68         };
69         template class HELLO {
70                 void phello ()  { printf ("HELLO"); }
71         };
72         template class world {
73                 void pworld ()  { printf ("world"); }
74         };
75         template class WORLD {
76                 void pworld ()  { printf ("WORLD"); }
77         };
79         // instances
80         class hw : helloworld_base, hello, world {};
81         class Hw : helloworld_base, HELLO, world {};
82         class hW : helloworld_base, hello, WORLD {};
83         class HW : helloworld_base, HELLO, WORLD {};
85   Abstract template classes are more like macros.  When a derrived class has an
86 abstract class parent, all the declarations of the abstract class will be
87 included in the derrived class.  The abstract class itself is not a real
88 type and it is not possible to declare objects of this type.  It can only be
89 used in inheritance.
91 Pros and Cons:  OOP is designed in C++ with the goal of maximum code
92 re-use (and rightly so).  Using abstract classes has the oposite effect.
93 In the above examples, in the first version with the virtual inheritance only
94 four functions are defined.  The version with the abstract classes has eight
95 functions (but only four writen by the programmer ==> macros!).  On the other
96 hand accessing the members of the helloworld_base object is much faster.
97 Plus the final objects are smaller.
99   So it depends on what you want.  Something that requires virtual inheritance
100 can be implemented with abstract classes instead.
102 Abstract classes can have their own hierarchy.  For example:
104         template class XX {
105                 int f ()        { return x++; }
106         };
108         template class YY : XX {
109                 float values [1024];
110                 void reset ()
111                 { int i; for (i=0;i<1024;i++) values [i] = 0; }
112         };
114         class A : YY {
115                 int x;
116         };
118 In this example, class A is the same as if it was declared:
120         class A {
121                 int f ()        { return x++; }
122                 float values [1024];
123                 void reset ()
124                 { int i; for (i=0;i<1024;i++) values [i] = 0; }
125                 int x;
126         }
128 which now clearly shows that abstract classes are exactly like macros.
130 Definitions of abstract template class member functions can be outside
131 the class.  Also, the abstract classe's base type is replaced with the
132 current instantiation type:
134         template class XX {
135                 XX foo (XX);
136         };
138         XX XX.foo (XX x)
139         {
140                 XX x2;
141                 return x2;
142         }
144         class A : XX {};