//.............................................................................. // // This file is part of the Jancy toolkit. // // Jancy is distributed under the MIT license. // For details see accompanying license.txt file, // the public copy of which is also available at: // http://tibbo.com/downloads/archive/jancy/license.txt // //.............................................................................. // Multiple inheritance is the unfairly abandoned tool, which allows for the // most natural way of implementation sharing. // Jancy uses the simple multiple inheritance model: multiple instances of // shared bases (if any). The infamous virtual multiple inheritance model of // C++ is not (and will not be) supported. //.............................................................................. class I1 { // virtual methods are declared using traditional keywords 'virtual', // 'abstract' and 'override' abstract foo (); } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . class C1: I1 { construct (int a) { printf ("C1.construct (%d)\n", a); } override foo () { printf ("C1.foo ()\n"); } } //.............................................................................. class I2 { abstract bar (); abstract baz ( int x, int y ); } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . class C2: I2 { construct (int a) { printf ("C2.construct (%d)\n", a); } override bar () { printf ("C2.bar ()\n"); } override baz ( int x, int y ) { printf ("C2.baz (%d, %d)\n", x, y); } } //.............................................................................. struct Point { int m_x; int m_y; } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // structs can be inherited from other structs or unions struct Point3D: Point { int m_z; } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // unions, however, cannot // union Illegal: Point // <-- error // { // } //.............................................................................. // it's OK to inherit a class from multiple bases including structs and unions class C3: C1, C2, Point { construct (int a); override baz ( int x, int y ); } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C3.construct (int a) { // if a base type requires construction this must be done in the beginning // of the derived type constructor // keywords 'basetype', 'basetype1'..'basetype9' could be used to reference // base types ('basetype' is equivalent to 'basetype1') basetype1.construct (a * 10); // traditional reference by name works too C2.construct (a * 100); printf ("C3.construct (%d)\n", a); } // it's OK to use storage specifier in an out-of-class method implementation // (it must match the original one, of course) override C3.baz ( int x, int y ) { printf ("C3.baz (%d, %d)\n", x, y); m_x = x; m_y = y; } //.............................................................................. // entry point int main () { printf ("main ()\n"); C3 c (7); c.foo (); c.bar (); c.baz (20, 30); printf ("c = { %d, %d }\n", c.m_x, c.m_y); return 0; } //..............................................................................