C++

Notes by Tim on C++. See also: Werner Jank's report on Saber C.

C++ compiler is generally called CC (not cc)

The same as C or objective-C:

Comments are C or objective-C style.

Include file name conventions are the same as C (no #import)

Overload functions

overload pow; // Tells compiler that overloading is not an error int pow(int, int) To overload an operator "+" for example, define a function called "operator +" sdf

Class specification:

To declare a class: class myclass { int myvariable1; int myvariable2; public: void myoperation(char *) void mymethod(int, int); }; To declare a subclass (why "public"?): class myclass : public superclass { ... }; To declare an instance of class myclass: myclass myobject; To call a method on a class: myobject.mymethod(x,y); To define a method for a class: void myclass::mymethod(int a, int b) { myvariable1 = myvariable2; // Instance variables this->myvariable1 = this->myvariable2; // Equivalent superclass::othermethod(2); // cf [super ...] return *this; // Really? see p25 } Within the definition, "this" is equivalent to objective-C''s "self". I can't understand whether it is a pointer to the object or the object itself from the above example.

Note you have, in a class method, no access to the superclass's private variables! However, you can DECLARE that your private class variables will be visible to SPECIFIC methods which are "friends"

friend otherclass othermethod(int, int); If you want to declare a method which is not defined in your (dummy) class, but MUST be declared in any subclass, declare it "virtual".

Constructors

A consructor is a method which initialises an instance. A constructor has the same name as its class. Several can exist with different arguments, or no arguments. class myclass { int myvariable1; int myvariable2; public: myclass(char *) myclass(int, int); }; A constructor can be called in a declaration staement:- int x; myclass myobject(1,2) A constructor method for a subclass must give the parameters for the constructor method for the superclass after ":". myclass::myclass(int a, int b) : (a+b) { /* code here could be void */ } is the equivalent of Objective-C: - newFoo: (int)a bar:(int)b { self = [super newSum:a+b]; /* code here could be void */ return self; } It seems that objective-C can only distinguish different methods by name, not by type of parameter, whilst C++ can distinguish by either, except that the name of a constructor is limited to the name of the class, so constructors can ONLY be distinguished by the parameter typing.

Inline expansion

The keyword "inline" forces inline expansion of very small functions.

References

The prefix "&" means "reference" rather like Algol68 "ref". int &r=i; means r refers to i, so r++ has the effect i++. Allows values to be passed by reference? Other uses?

Stream.h

cout and cin are standard input and output streams. They take input and output operators.