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;
18 { phello (); printf (" "); pworld (); printf ("!\n"); }
21 class hello : helloworld_base {
22 void phello () { printf ("hello"); }
24 class HELLO : helloworld_base {
25 void phello () { printf ("HELLO"); }
27 class world : helloworld_base {
28 void pworld () { printf ("world"); }
30 class WORLD : helloworld_base {
31 void pworld () { printf ("WORLD"); }
35 class hw : hello, world {};
36 class Hw : HELLO, world {};
37 class hW : hello, WORLD {};
38 class HW : HELLO, WORLD {};
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;
63 { phello (); printf (" "); pworld (); printf ("!\n"); }
66 template class hello {
67 void phello () { printf ("hello"); }
69 template class HELLO {
70 void phello () { printf ("HELLO"); }
72 template class world {
73 void pworld () { printf ("world"); }
75 template class WORLD {
76 void pworld () { printf ("WORLD"); }
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
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:
105 int f () { return x++; }
108 template class YY : XX {
111 { int i; for (i=0;i<1024;i++) values [i] = 0; }
118 In this example, class A is the same as if it was declared:
121 int f () { return x++; }
124 { int i; for (i=0;i<1024;i++) values [i] = 0; }
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: