cs60 lecture 15 ---------------------------- Today ---------------------------- - C++! - C/C++ diffs - Classes C++ ---------------------------- - developed in early 80s by stroustrep to bring object oriented programming to C programmers - IMPORTANT: C++ is a superset of C, all C programs are C++ programs, but converse is not true - that being said, everything we know about C directly translates to C++ - C++ main difference is complex encapsulation data and functions, core of object oriented progamming - class - other differences include - comments :) - bool type - how IO is handled - how memory is managed - references (pointers sort of) C++ Comments ------------ - // woo! C++ types ---------- - all the same native types in C++, plut bool which is one byte that can be set to 'true' or 'false' which are just '1' or '0' - operators all the same, type conversion still the same - casting can look slightly different, but old way works int someint; double somedoub; somedoub = static_cast(someint); C++ IO ---------- - in C, we used stdio.h which defined printf/scanf/etc - default type was a FILE * - in C++ we have IO streams, which are classes - iostream.h - cout << << "string" << endl; - cin >> >> ; - can put multiple types in the cout line, types are detected and formatted properly - if you want to output one type as another, use a cast - printf/scanf still work! must include stdio.h C++ Memory Management ---------------------- - same functionality as C but has different names - malloc == new - free == delete - form slightly different, types automatically detected - char *foo; - foo = new char[32]; - delete foo; - you can still use malloc/free but don't use them both in the same program C++ References ---------------------- - new way to reference variables, looks better but is same as pointer int i; int &ref = i; 'ref' and 'i' are the exact same thing! ref++ == i++ ref = 10 == i = 10; int *p; p = &ref; - Why is this useful? we can pass them to functions int i=0; cout << i << endl; foo(i); cout << i << endl; void foo(int &ref) { ref = 10; } - once ref is initialized it can never change C++ Classes ---------------------- - class is what makes C++ 'object oriented' - in OO programming model, data and functionality combined to form objects - class is like a super struct - contains data types - contains functions - idea is to not only encapsulate data, but all operations on that data that a program would ever need to perform - we'll use a stack datatype as an example - simple stack consists of an array and a 'top' index - define a class just like a struct using class { }; - inside class declaration we can encapsulate data by declaring variables class stack { public: int top; int data[32]; int size; }; - this is exactly the same as a struct, accessed the same way: declare one use . for accessing data, -> for pointers to classes - often times, we actually would like to 'hide' some data from the programmer. These are called 'ADT' or abstract data types. - int top; - int data[32]; class stack { private: int top; int data[32]; public: int size; }; - now, we cannot access top or data from main, so now what? - declare some member functions void push(int); int pop(); - we can call these member functions from main just like we access data stack mystack; mystack.push(10); mystack.push(20); cout << mystack.pop() << " " << mystack.pop() << endl; - format of member functions void stack::push(int in) { top++; size++; data[top] = in; } int stack::pop() { int ret; ret = data[top]; top--; size--; return(ret); } - picture of instance of class. stack foo, bar; foo top, data[32], size bar top, data[32], size - when call foo.push(10), code is operating on foo's data - member functions can be private as well, then can only be called from other member functions - we have a problem. the stack has some private variables, but they are uninitialized when we declare the class! - some special member functions: - constructor - desctructor - copy constructor Constructor ----------------- - constructor is called every time a class is created, whether statically declared or created with 'new' - every class should have one that takes no arguments stack(); stack::stack() { cout << "constructor running" << endl; top=-1; size=0; bzero(data, 32); } Copy Constructor ------------------ - copy costructor is called whenever a class is passed to a function - 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]; } } Destructor ------------------ - is invoked whenever we leave scope where class declared or explictly call 'delete' - how we implement 'garbage collection' in C++ ~stack(); stack::~stack() { cout << "I AM THE DESTRUCTOR" << endl; // nothing to do but if we had some dynamic vars we would free // them here }