plan
play

Plan Review of variable types C++: Tour 2 Functions But first - PDF document

Plan Review of variable types C++: Tour 2 Functions But first Projects UML tidbits Any questions? Use cases please include documentation Associations vs. Attributes Design due March 28 th Seq Diagrams


  1. Plan • Review of variable types C++: Tour 2 • Functions But first… Projects • UML tidbits • Any questions? – Use cases – please include documentation – Associations vs. Attributes • Design due March 28 th – Seq Diagrams in Logical View – Online docs of shapewin library on Web – Can include shapewin classes in UML • Partners…anyone doesn’t have one? Finally… Variables in C++ • Special thanks to Prof Swammi for taking • Variable types my lecture on Thursday – Basic – Pointer – Reference • Any questions?

  2. Variables in C++ Variables in C++ • Basic variable Basic variable – Memory associated with a variable with size int foo; based on the type of the variable. float f = 7.0; – Variable declarations are “executable” statements Student joe (“Geigel” , “Joe”, “GCCIS”, • Memory is allocated when declaration is made “CS”); – No need to use new when instantiating objects of a class. Variables in C++ Variables in C++ • Assignment = copy not reference • Assignment = copy not reference – In Java – In C++ • Student joe = new Student(“Geigel” , “Joe”, • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); “GCCIS”, “CS”); • Student fred = joe; • Student fred = joe; joe Joe joe Joe Geigel Geigel fred fred Joe Geigel Variables in C++ Variables in C++ • Basic variable • Pointer Variables – Accessing class members – Stores the memory address of an object. • Uses the . Syntax used in Java – new returns a pointer to an object and allocates memory for it on the heap (free store). • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); – Can have pointers to basic data types. • joe.getGrades(); – C++ has no garbage collection! – NULL pointer takes value 0.

  3. Variables in C++ Variables in C++ • Pointer Variables Pointer variable – Dereference operator * – If ptr is a pointer int *foo; float *f = 7.0; // Invalid • i.e A variable whose contents is a memory address – then *ptr refers to the object or data item that float *g = 0; // okay is pointed to by ptr float *h = 0x12345; // actually illegal!! • Can be interpreted as: – The data item or object at ptr Student *joe = new Student (“Geigel” , – The object or data item pointed to by ptr “Joe”, “GCCIS”, “CS”); Variables in C++ Variables in C++ Pointer variable • Address of operator – You can always get the address of any variable float *f = 7.0; // Invalid however or object by using the address of operator &. float *f = new float; • float f = 7.0; • float *fptr = &f; (*f) = 7.0; – Use with caution!! Student *joe = new Student (“Geigel” , • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); “Joe”, “GCCIS”, “CS”); • Student *joeptr = &joe; Student *fred = joe; Variables in C++ Variables in C++ • Pointer variable • Assignment = copy not reference – Accessing class members – For Pointer Variables as well • Uses the -> Syntax • Student *joe = new Student(“Geigel” , “Joe”, “GCCIS”, “CS”); • Student *fred = joe; • Student *joe = new Student (“Geigel” , “Joe”, “GCCIS”, “CS”); • joe->getGrades(); 0x345ABC2 joe 0x345ABC2 Joe • Which is the same as Geigel fred 0x345ABC2 • (*joe).getGrades();

  4. Variables in C++ Variables in C++ • Reference Variables • Reference Variables • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); – Alias for an already existing object • Student &fred = joe; – Usually used to pass function arguments by reference. – Cannot change what they point to joe Joe Geigel fred Variables in C++ Variables in C++ -- Examples Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); • Reference variable Student *joe_ptr (new Student (“Schmoe” , “Joe”, “GCCIS”, “CS”); – Accessing class members Student &joe_ref (joe); • Uses the . Syntax // okay joe = *joe_ptr; • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); *joe_ptr = joe; • joe.getGrades(); joe_ref = *joe_ptr; // changes joe • Student &fred (joe); joe = joe_ref; • fred.getGrades(); // dangerous joe_ptr = &joe; joe_ptr = &joe_ref; Variables in C++ static • static class members like in Java: • Questions? – the member has no knowledge of any particular instance of the class – data member: there is only one copy shared by all – member function: cannot access non-static data or functions

  5. static static • In header file: • In source file class Tribble { void Tribble::report() public: { ... } Tribble(); unsigned long Tribble::count = 0; ~Tribble(); public: static void report(); static unsigned long count; }; static static • Local variables can also be declared as • Questions? static . void foo () { static int a = 0; … a = a + 1; } Functions Functions • Declaration vs definitions • void and void * – Declaration is the function signature – void – indicates that a function does not return • Required before function can be used a value • Often included in a header file • Can exist in multiple files – void * -- function returns a “generic” pointer • Must be typecast to correct pointer type • char *strcpy (char * to, char* from); • Use with caution – Definition • Actual code • Must defined once and only once

  6. Functions Functions – Pass by value • In C++ function arguments are pass by • Get around this by changing the variable value: type of the arguments void foo (aClass a) void foo (aClass *a) aClass i(7); aClass i(7); { { foo (i); foo (&i); a.moo = 12; a->moo = 12; } } Functions – Pass by value const • Any variable declared as const cannot be • Cleaner means is to use reference variables: changed – Like final in Java. void foo (aClass &a) aClass i(7); – If the variable is a pointer then the object / data { item pointed to cannot be changed. foo (i); • const aClass *a (new aClass(7)) a.moo = 12; • a->moo = 7; // error } Functions – Pass by value Functions – Pass by value • Yet another variable type: • Works for function arguments as well. – Constant pointers • Once defined, the data/object pointed to by the pointer cannot be changed. void foo (const aClass *a) aClass i(7); { foo (&i); • const aClass *a (new aClass(7)) a->moo = 12; // error • a->moo = 7; // error }

  7. Functions – Pass by value const • A class method can also be declared as • Furthermore const – This means that the method will not change the object. void foo (aClass *a) const aClass *i (new aClass(7)); { class aClass { a->moo = 13; public printValues () const; foo (i); // error } public changeValues(); } const Functions • Const methods • Question? void foo (const aClass *a) { a->printValues(); // okay a->changeValues(); // error }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend