Before We Begin • CS4 Newsgroup C++: Tour Pt 1 Plan for today Data Types • Basic data types • Data types – int , short, long, unsigned • Variables / Pointers – bool • Operations – char – float • Statements – double – void (sort of) – more on void later • Not classes! • No corresponding classes like in Java Data types Data Types • Strings • The class – No basic string type in language but… – Like in Java, classes have – C style strings • Data members • In C strings are arrays of char with the string • Methods terminated by a null character – Constructor h e l l o \0 • called when a new instance is created – Destructor – C++ standard template library does include a string class. • Called when an instance is destroyed
Variables in C++ Variables in C++ • Variable types • Basic variable – Basic – Memory associated with a variable with size based on the type of the variable. – Pointer – Variable declarations are “executable” – Reference statements • Memory is allocated when declaration is made – No need to use new when instantiating objects of a class. Variables in C++ Variables in C++ Basic variable • Assignment = copy not reference – In Java int foo; • Student joe = new Student(“Geigel” , “Joe”, “GCCIS”, “CS”); float f = 7.0; • Student fred = joe; Student joe (“Geigel” , “Joe”, “GCCIS”, joe Joe “CS”); Geigel fred Variables in C++ Variables in C++ • Assignment = copy not reference • Basic variable – In C++ – Accessing class members • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); • Uses the . Syntax used in Java • Student fred = joe; • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); joe Joe • joe.getGrades(); Geigel fred Joe Geigel
Variables in C++ Variables in C++ • Pointer Variables Pointer variable – Stores the memory address of an object. int *foo; – new returns a pointer to an object and allocates float *f = 7.0; // Invalid memory for it on the heap (free store). float *g = 0; // okay – Can have pointers to basic data types. float *h = 0x12345; // actually illegal!! – C++ has no garbage collection! – NULL pointer takes value 0. Student *joe = new Student (“Geigel” , “Joe”, “GCCIS”, “CS”); Variables in C++ Variables in C++ • Pointer Variables Pointer variable – Dereference operator * – If ptr is a pointer float *f = 7.0; // Invalid however • i.e A variable whose contents is a memory address float *f = new float; – then *ptr refers to the object or data item that (*f) = 7.0; is pointed to by ptr Student *joe = new Student (“Geigel” , • Can be interpreted as: “Joe”, “GCCIS”, “CS”); – The data item or object at ptr Student *fred = joe; – The object or data item pointed to by ptr Variables in C++ Variables in C++ • Address of operator • Assignment = copy not reference – You can always get the address of any variable – For Pointer Variables as well or object by using the address of operator &. • Student *joe = new Student(“Geigel” , “Joe”, “GCCIS”, “CS”); • float f = 7.0; • Student *fred = joe; • float *fptr = &f; – Use with caution!! • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); 0x345ABC2 joe 0x345ABC2 Joe • Student *joeptr = &joe; Geigel fred 0x345ABC2
Variables in C++ Variables in C++ • Pointer variable • Reference Variables – Accessing class members – Alias for an already existing object • Uses the -> Syntax – Usually used to pass function arguments by reference. • Student *joe = new Student (“Geigel” , “Joe”, “GCCIS”, “CS”); – Cannot change what they point to • joe->getGrades(); • Which is the same as • (*joe).getGrades(); Variables in C++ Variables in C++ • Reference Variables • Reference variable • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); – Accessing class members • Student &fred = joe; • Uses the . Syntax • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); • joe.getGrades(); joe Joe • Student &fred (joe); • fred.getGrades(); Geigel fred Variables in C++ -- Examples Variables in C++ Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); • Questions? Student *joe_ptr (new Student (“Schmoe” , “Joe”, “GCCIS”, “CS”); Student &joe_ref (joe); // okay joe = *joe_ptr; *joe_ptr = joe; joe_ref = *joe_ptr; // changes joe joe = joe_ref; // dangerous joe_ptr = &joe; joe_ptr = &joe_ref;
Operations Operations • Numeric • Logical – Usual arithmetic – <. >, <=, >=, == • + , - , * , /, % (mod) • == is logical equals to – Operate and assign – &&, ||, ! • +=, -=, *=, /=, %= – Increment, Decrement • ++, -- Operations Operations • Bitwise operations • sizeof – Can operate on the bits of an integral type – Will return the size of a data item or object • Basic logical operations • sizeof (char) = 1 – &, |, ^ • sizeof (bool) = 1 • Bit shifting – <<, >> » int a = 0x1111 » a >> 4; Operations Statements • if (condition) statement • Questions? • if (condition) statement else statement • switch (condition) statement • while (statement) statement • do statement while (expression) • for ( ; ; ) statement – Statements can be nested blocks of code • I.e { … }
Statements Statements • Logical conditions • Logical conditions – int a; – False if condition evaluates to 0 – if (a) { ... } // same as – True if condition evaluates to a non-zero value. – if (a != 0) { ... } – Can be interpreted as condition != 0 – int *b; – if (b) { ... } // same as – if (b != 0) { ... } Statements Statements • Logical conditions • Logical conditions – Assignments and declarations can be made in a – Are short cuircuited logical condition – The following is valid: – if ( ( a < b) && ( c > d)) { • if ( double d = somefunction (a)) { … } …} – A most common mistake • if ( a = b ) { … } // is not the same • If (a > b), (c > d) will not get tested. as • if ( a == b) { … } Statements In memory of… • Exiting a loop • Edsger Wybe Dijkstra • 1930 – 2002 – break – exit the loop – continue – perform next iteration of a loop • GOTOs Conidered – goto – go anywhere Harmful
Statements Summary • Part 1 of our tour of C++ • Finally, my favorite C++ statement – Data types – var = (condition) ? statement1 : statement2 – Variable / Pointers – Same as: – Operators • if (condition) – Statement var = expression1 else var = expression2 • Tomorrow: – Functions, arrays, basic I/O • max = (a <=b) ? a : b; • Questions?
Recommend
More recommend