Next time: Learning Computing with Robots, ch. 10. Extensible Languages: Languages that can be extended for some particular application area or purpose (e.g., scientific computing, OS writing, graphics, game programming, robots, etc.). You have a base language and an extension mechanism. Anyone can make extensions to the language. This means the language can grow and evolve and adapt to new application areas. C++ is an example of an extensible language. The base language is very close to the C language, and it provides basic data types (int, bool, double, char, ...) and control structures (if, while, for, switch, ...) and basic operations (=, +, -, *, /, %, ==, !=, <, >, <=, >=, ++, --, ...). The extension mechanism allows you to define new things that look as though they are part of the language: function definition. type definition (enum, struct, class, ...). Class is really the most important extension mechanism. Many of standard libraries (iostream, cmath, vector, string, list, ...) are just classes that someone else has defined. Another extension mechanism is operator overloading: gives you a limited ability to define new operators in the language. It's limited because you can only add new definitions to operator symbols that are already built into the core language (=, +, -, *, /, %, ==, !=, <, >, <=, >=, ++, --, <<, >>, ...). Note: C++ permits both function overloading and operator overloading. The compiler has to be able to figure from the types and number of arguments which definition is meant. You can't have ambiguous programs. Do sensible overloading. For example all meanings of "+" should in some sense be addition. If you give operators unintuitive meanings it won't confuse the compiler but will confuse humans (including you). When you overload an operator, the new operator might be a member function of a class (if it needs access to the private member variables or member functions) or it could be a standalone (non-member) function, i.e., not in the class. If "+" has been overloaded as a non-member function, then "a+b" works just like a function call "+ (a, b)". In this case the overloading declaration (say, for complex addition) would be: Complex operator + (const Complex a, const Complex b); If "+" has been overloaded as a member function, then "a+b" works like the function call "a.+(b)", that is, the first argument is passed implicitly. When you write a = b = c = 0; This is interpreted as: a = (b = (c = 0)); Step by step: assign 0 to c, then return the reference to c. Then assign the contents of c to b, and return the ref. to b. Then assign the contents of b to a, and return the ref. to a. But (a = b) = 0; doesn't do what you expect. Because a=b assigns b to a, but then returns the ref. to a, which is then set to 0. Making the reference returned by "=" be constant, prevents this, because then you can't assign to it. I.e., in the above example, you cannot assign to (a = b). Overloading the insert (<<) and extract (>>) operators: You have seen that << and >> are defined for all the built-in types (ints, doubles, chars, etc.), but it would be nice to define them for user-defined types. Complex z (3, 4); cout << z << endl; If you write this the C++ compiler will look for a definition of << defined for Complex. You need to understand how chained inserts are interpreted. The answer is, they associate to the left and return their left argument. cout << "Ans = " << N << endl; This is interpreted as though I wrote: (((cout << "Ans = ") << N) << endl); The first << takes cout and a string, prints the string and return its left argument, which is cout. The second << does cout << N, which prints the number N (inserts it in the stream cout) and returns cout. The third << does cout << endl, which inserts an end of line in the stream cout. Inheritance and Subclasses: We think of classes as sets or categories of related objects. Just as in everyday life it's useful to classify things hierarchically, so also in OOP it's useful. We have subclasses of animal, such as bird, fish, land animal, and subclasses of land animal, such as dog, cat, horse, ..., and subclasses of dog, such as collie, beagle, ..., and individuals (objects) belonging to the class dog, such as Fido and Rover. The idea is that a subclass has all (or most) of the properties of the superclass, but may have additional properties. This is also very useful in OOP. Suppose you were implementing a video game. Your highest class of objects might be: displayObjects: position on screen show it hide it control size ... movingObject (subclass of displayObject) direction speed set its direction and speed rotate ... projectile (subclass of movingObject) collision detection explode arrow (subclass of projectile) animateObject (subclass of movingObject) limb motion die life meter (vitality) leggedObject (subclass of animateObject) control motion of legs (gait) person (subclass of leggedObject) name appearance personality