Objects, classes, data and function members, constructors/destructors. OOP in C++ and Java, objects vs closures, classes vs datatypes, OO pitfalls.
Readings: Scott ch 3.3.4-3.3.5, 3.7, Stroustrup ch. 8-9 (optional), Arnold ch. 13 (optional).
Styles of OOLs (object oriented languages)
Class-based
In a class-based OOP language, each object is an instance of a class.
Prototype-based
In a prototype-based OOP language, each object is a clone of another object, possibly, with modifications and/or additions.
Example:
var original = {a: 'A', b: 'B'};
var clone = owl.util.clone(original);
// clone.a == 'A'
// clone.b == 'B'
clone.a = 'Apple';
// clone.a == 'Apple'
// original.a == 'A' // unchanged
original.b = 'Banana';
// clone.b == 'Banana' // change shows through
clone.c = 'Car';
// original.c is undefined
original.a = 'Blah';
// clone.a == 'Apple' // clone's new val hides original
delete clone.a;
// clone.a == 'Blah' // original value visible again
// repeating "delete clone.a" won 't delete orig.value
class ColoredPoint : public Point {
Color color;
public:
ColoredPoint (double x ,double y, Color c)
: Point (x ,y), color ( c ) {}
ColoredPoint (Color c) : Point (0.0, 0.0), color (c) { }
virtual Color getColor () {return color;}
virtual void display () { ... } // now in color!
};
Dynamic despatching by vtable
vtable is used to determine which class' method to invoke.
virtual method means: "use the subclass version" (including all descendant subclasses). Virtual methods are placed in the vtable.