CS60 Lecture 16 --------------------- Announce ---------------------- Today --------------------- - Review of C++ diffs - Review of Classes (chap 6, 7) - Special Member Functions - Book Conventions - Special Member Types - Operator Overloading (chap 8) Review ---------------------- - C++ is superset of C - few syntax difference/additions - bool type - cout/cin - references - memory management new and free Classes --------------------------- class stack { private: int top, size, data[32]; public: int getsize(); int pop(); void push(int); }; So What Do We Have Here? ------------------------ - goal is to create reusable code, write once, use forever - in C we made libraries with special types and functions - put prototypes/type defs in headers - put functions in separate .c files - made libraries - in C++ we create OBJECTS using classes - put class defs in headers - put class functions in separacte .cpp files - make libraries - difference? - objects are cleaner abstractions - programmers can use safely - other programmers can use because all data/functionality is encapsulated in the class - safe becuase we hide sensitive variables from the programmer - programmer can assume object is bugfree - general practice is to hide ALL variables and provide member functions to observe and change them. - observation member functions are called 'accessors' - member functions that change stuff are called 'mutators' ex: int get_size(); void clear_stack(); Special Member Functions ------------------------------ - talked about constructor - talked about destructor - can have multiple constructors - only one destructor Copy Constructor ------------------ - copy costructor is called whenever a class is passed to a function - you get a default copy constructor, but prblems like struct/pointer prob - if you plan on using pointers or arrays in your classes, plan on writing copy constructors stack(stack &); stack::stack(stack &in) { int i; top = in.top; size = in.size; for (i=0; i < 32; i++) { data[i] = in.data[i]; } } Book Conventiosn ------------------- - Savitch likes C++, uses many features of the language so we need to cover this stuff - in C++, easy to shoot yourself in the foot! understand what it is you are seeing/doing or problems will arise. - Chapter 1 savitch shows some 'magic code'! what the?!?! Inline Functions -------------------------- - not very elegant,but sometimes nice for the programmer public: stack() {top=-1; size=0; bzero(data, 32*sizeof(int));}; - compiler places code in place of function call, little faster - if function is large, waste of memory - convention is to inline if function is small (one or two lines) Special Member Types -------------------------- - static variables and member functions - we know that private member data is accessible by all instances of objects of a certain type. - sometimes we would like to have an INSTANCE of a private member available to all instance of a certain type. this way, all types can 'share' some variables at runtime - static int foo; - if one instance of class sets foo to 10, all instances of class see foo as 10 - static member functions - do not access any private variables accept static private variables, - thought of like normal functions that happen to live in an object - declared in class - private: static int numstacks; public: static void set_numstacks(int); - invoked from main stack::set_numstacks(0); Const Member Types --------------------------------- - mostly used for efficiency and error checking - const variables cannot be modified once set - set upon declaration, or as a parameter to function int func(const stack instack) { // do stuff cout << instack.size() << endl; instack.clear_stack(); } - now in this function, the 'instack' variable cannot be changed - what happens if we try to call a member function? the function may or may not change the contents of 'instack', the compiler doesn't know. - so it will give us an error on both functions - UNLESS, we declare our functions as being const functions public: void size() const; - accessors should usually be declared as consts ------------------------------------------------------------------------- - Now we can make objects and understand what we see in other's code! - C has 'objects' sort of, with structs - C++ has many special abilities that can be applied to classes beyond data/function abstraction Operator Overloading ----------------------- - in C and C++ we have operators for basic types - no operators worked in C on user defined types, accept for = - but even = was dangerous to use when structs contained pointers ex: class dice { private: int val; int max; public: dice() {max=6; val=1;}; dice(int inmax) {max=inmax; val=1;}; void add(dice a, dice b); void roll(); int getval(); }; void dice::add(dice a, dice b) { val = a.val + b.val; max = a.max; } main() { dice a, b, c; c = a.add(b); } - a little combersome and awkward ... overload the + operator so we can use like built in types! - C++ allows us to 'overload' operators for objects - operators are really just functions, we can define special functions that are invoked when operators on classes are used! public: const dice operator +(const &dice); const dice dice::operator +(const &dice b) { dice *ret ret = new dice; ret->val = val + b.val; ret->max = max; return(ret); } - invoked using the + operator dice a, b, c; a + b - function called on instance of dice 'a', passed 'b' as parameter, should return a new allocated dice c = a + b same as c = a.operator+(b); - ALL operators can be overloaded: *, -, /, % - Boolean comparison operators: <, >, == - should return bool bool operator ==(const complex &b); - Unary operators - takes nothing as parameter // negation const complex operator -(); //++ and -- complex operator ++(); complex operator ++(int); // 'int' is ignored, just tells compiler postfix as opposed to prefix - even indexing operators! [] - more later - assignment operator '=' - binary operator, through of as a = b just like a + b - returns value of 'a' after assignment - operator is called on instance 'a' with 'b' as parameter