Core Class Concepts for C++ ---------------------------------- In a struct, members are *public* by default In a class, members are *private* by default Instance variable is the actual object (big memory box), not a reference like in Java. CONSTRUCTOR IS ALWAYS CALLED. To refer to the implicit argument of a member function we use this, BUT, in C++ "this" is a pointer, use *this to get actual object. To make a member of a class shared, not instance, we say "static". Defaults (get for free, not always worthwhile): - default constructor (no params) - primitive data members init to 0 - class data members init w/their default constructors - can override and write your own - copy constructor (const Classtype & param) - shallow copy - literal assignment between primitive data members - uses copy constructors for any class data members - override to get a deep copy - if you have an array of data - if you have any dynamically allocated data, collections, etc. - otherwise have two references to same collection - = operator - memberwise assignment - could be shallow copy - usually override to get deep copy - == operator - memberwise == test - could be too shallow - usually override to get deep equality test - destructor (~ClassName) - free the data members of an object when it's lifetime is over - shallow freedom - need to override for deep freedom (dynamic mem allocated) - calls destructors for any data members that are objects Class composition (object of one class is a member of another class) - class data members are constructed in order of declaration and before the class constructor is called - destructor calls destructors of class members in reverse order Dynamic memory allocation: { int *iray = new int[size]; delete [] iray; // frees the array elements int *iptr = new int; // ?? *iptr = 20; // put 20 in it delete iptr; // frees the one integer it pointed to } // iray gets freed automatically