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

plan
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

C++: Tour 2 Plan

  • Review of variable types
  • Functions

But first…

  • UML tidbits

– Use cases – please include documentation – Associations vs. Attributes – Seq Diagrams in Logical View

Projects

  • Any questions?
  • Design due March 28th

– Online docs of shapewin library on Web – Can include shapewin classes in UML

  • Partners…anyone doesn’t have one?

Finally…

  • Special thanks to Prof Swammi for taking

my lecture on Thursday

  • Any questions?

Variables in C++

  • Variable types

– Basic – Pointer – Reference

slide-2
SLIDE 2

Variables in C++

  • Basic variable

– Memory associated with a variable with size based on the type of the variable. – Variable declarations are “executable” statements

  • Memory is allocated when declaration is made

– No need to use new when instantiating objects

  • f a class.

Variables in C++

Basic variable

int foo; float f = 7.0; Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”);

Variables in C++

  • Assignment = copy not reference

– In Java

  • Student joe = new Student(“Geigel” , “Joe”,

“GCCIS”, “CS”);

  • Student fred = joe;

Joe Geigel joe fred

Variables in C++

  • Assignment = copy not reference

– In C++

  • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”);
  • Student fred = joe;

Joe Geigel joe fred Joe Geigel

Variables in C++

  • Basic variable

– Accessing class members

  • Uses the . Syntax used in Java
  • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”);
  • joe.getGrades();

Variables in C++

  • Pointer Variables

– Stores the memory address of an object. – new returns a pointer to an object and allocates memory for it on the heap (free store). – Can have pointers to basic data types. – C++ has no garbage collection! – NULL pointer takes value 0.

slide-3
SLIDE 3

Variables in C++

Pointer variable

int *foo; float *f = 7.0; // Invalid float *g = 0; // okay float *h = 0x12345; // actually illegal!! Student *joe = new Student (“Geigel” , “Joe”, “GCCIS”, “CS”);

Variables in C++

  • Pointer Variables

– Dereference operator * – If ptr is a pointer

  • i.e A variable whose contents is a memory address

– then *ptr refers to the object or data item that is pointed to by ptr

  • Can be interpreted as:

– The data item or object at ptr – The object or data item pointed to by ptr

Variables in C++

Pointer variable

float *f = 7.0; // Invalid however float *f = new float; (*f) = 7.0; Student *joe = new Student (“Geigel” , “Joe”, “GCCIS”, “CS”); Student *fred = joe;

Variables in C++

  • Address of operator

– You can always get the address of any variable

  • r object by using the address of operator &.
  • float f = 7.0;
  • float *fptr = &f;

– Use with caution!!

  • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”);
  • Student *joeptr = &joe;

Variables in C++

  • Assignment = copy not reference

– For Pointer Variables as well

  • Student *joe = new Student(“Geigel” , “Joe”,

“GCCIS”, “CS”);

  • Student *fred = joe;

Joe Geigel

0x345ABC2

joe

0x345ABC2

fred 0x345ABC2

Variables in C++

  • Pointer variable

– Accessing class members

  • Uses the -> Syntax
  • Student *joe = new Student (“Geigel” , “Joe”,

“GCCIS”, “CS”);

  • joe->getGrades();
  • Which is the same as
  • (*joe).getGrades();
slide-4
SLIDE 4

Variables in C++

  • Reference Variables

– Alias for an already existing object – Usually used to pass function arguments by reference. – Cannot change what they point to

Variables in C++

  • Reference Variables
  • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”);
  • Student &fred = joe;

Joe Geigel joe fred

Variables in C++

  • Reference variable

– Accessing class members

  • Uses the . Syntax
  • Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”);
  • joe.getGrades();
  • Student &fred (joe);
  • fred.getGrades();

Variables in C++ -- Examples

Student joe (“Geigel” , “Joe”, “GCCIS”, “CS”); 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;

Variables in C++

  • Questions?

static

  • static class members like in Java:

– 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

  • r functions
slide-5
SLIDE 5

static

  • In header file:

class Tribble { public: Tribble(); ~Tribble(); public: static void report(); static unsigned long count; };

static

  • In source file

void Tribble::report() { ... } unsigned long Tribble::count = 0;

static

  • Local variables can also be declared as

static.

void foo () { static int a = 0; … a = a + 1; }

static

  • Questions?

Functions

  • Declaration vs definitions

– Declaration is the function signature

  • Required before function can be used
  • Often included in a header file
  • Can exist in multiple files
  • char *strcpy (char * to, char* from);

– Definition

  • Actual code
  • Must defined once and only once

Functions

  • void and void *

– void – indicates that a function does not return a value – void * -- function returns a “generic” pointer

  • Must be typecast to correct pointer type
  • Use with caution
slide-6
SLIDE 6

Functions

  • In C++ function arguments are pass by

value:

aClass i(7); foo (i); void foo (aClass a) { a.moo = 12; }

Functions – Pass by value

  • Get around this by changing the variable

type of the arguments

aClass i(7); foo (&i); void foo (aClass *a) { a->moo = 12; }

Functions – Pass by value

  • Cleaner means is to use reference variables:

aClass i(7); foo (i); void foo (aClass &a) { a.moo = 12; }

const

  • Any variable declared as const cannot be

changed

– Like final in Java. – If the variable is a pointer then the object / data item pointed to cannot be changed.

  • const aClass *a (new aClass(7))
  • a->moo = 7; // error

Functions – Pass by value

  • Yet another variable type:

– Constant pointers

  • Once defined, the data/object pointed to by the

pointer cannot be changed.

  • const aClass *a (new aClass(7))
  • a->moo = 7; // error

Functions – Pass by value

  • Works for function arguments as well.

aClass i(7); foo (&i); void foo (const aClass *a) { a->moo = 12; // error }

slide-7
SLIDE 7

Functions – Pass by value

  • Furthermore

const aClass *i (new aClass(7)); foo (i); // error void foo (aClass *a) { a->moo = 13; }

const

  • A class method can also be declared as

const

– This means that the method will not change the

  • bject.

class aClass {

public printValues () const; public changeValues(); }

const

  • Const methods

void foo (const aClass *a) { a->printValues(); // okay a->changeValues(); // error }

Functions

  • Question?