cs 376b introduction to computer vision 01 / 23 / 2008 instructor: michael eckmann

20
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

Upload: debra-griffith

Post on 05-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

CS 376bIntroduction to Computer Vision

01 / 23 / 2008

Instructor: Michael Eckmann

Page 2: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Today’s Topics• More C++

– similarities to Java, operators– arrays– objects in C++ vs. Java references– pointers, references– struct, class– pass by reference

Page 3: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• Like Java,

– C++ has for, while and do-while loops,

– C++ has if's and else's

– C++ has switch statements with cases

– C++ has same arithmetic operators

– C++ has same relational operators

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 4: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• >> and << when used on ints do a shift.

• int x = 10;

• x = x << 2; // shift left by 2 bits multiplies by 4

• cout << x << endl; // will be 40

• >> is a shift right (divides by 2 * right operand)

• padding high bits with 0's?

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 5: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• More types

– array• similar to Java

– int nums[30]; // array of 30 ints, index starts at 0– int ages[] = {21, 18, 35, 52, 65};– can't put brackets before the name (but in Java can)

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 6: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• More types

– objects• a simple declaration in Java creates a reference to a

class– Card a; // a is a Card reference

• a simple declaration in C++ creates an object of that class by calling the default constructor

– string a; // actually calls string's default constructor• string s1(“Hello”); // calls a different constructor• string s2 = string(“Bye”); // also calls a constructor• s1 = s2; // actually makes a COPY

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 7: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• More types

– objects• string s1(“Hello”); // calls a different constructor• string s2 = string(“Bye”); // also calls a constructor• s1 = s2; // actually makes a COPY• s2 = “So long”; // doesn't affect s1

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 8: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• More types

– objects/references

– In Java we can have:

Card a; // a reference to a Card (does NOT call Card's default constructor)

Card b = new Card(2, 2);

a = b; // a and b now both refer to the same object

b.setRank(5); // the ONE object has rank 5 and both a and b refer to it

– This is the typical behaviour in Java (except for Strings where = actually makes a “copy” of the data not an alias.)

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 9: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• More types

– objects/references

– In C++ we can have:

Card &a; // a reference to Card (does NOT call Card's default constructor)

Card b = new Card(2, 2);

a = b; // a and b now both refer to the same object

b.setRank(5); // the ONE object has rank 5 and both a and b refer to it

– This is the typical behaviour in Java (except for Strings where = actually makes a “copy” of the data not an alias.)

– see example programsMichael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 10: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• a struct is a record in C++ and in C e.g.

struct PhoneNumber

{int areaCode; // first 3 digits, e.g. 518

int exchange; // next 3 digits e.g. 580

int last4; // last 4 digits e.g. 5294

}

• if you know structs from C, they are different in C++. In C++ they are essentially the same as classes except that by default a struct's members are public and by default a class's members are private.

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 11: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• classes in C++

– can have multiple inheritance

– typically divided into header and implementation (usually in different files)

– class definitions end with a semicolon

– divided into sections public: private: protected:

– inline functions - code in the definition, for speed (one liners)

– header (interface) file vs. implementation file

– prototypes of functions and classes must appear before use

– extern (says variable is declared elsewhere and nothing more is allocated)

– let's look at the Card.h and Card.cpp files

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 12: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• classes in C++

– destructors (class name preceded by ~, automatically called when object goes out of scope, usually put code in there for deallocating memory for data members if necessary)

– if you don't create them C++ automatically provides:

• default constructor (no parameters)

• a copy constructor– provides shallow copy access– if you need deep copy, write your own

• a destructor– may need your own to release allocated memory

– I just wanted to make you aware of these things now, we'll see more details later after understanding explicit memory alloc/dealloc.

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 13: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• pointers in C++

– a pointer is a variable that holds a memory address

• the size of a pointer is the size of a memory address for your system

– the memory address stored in a pointer is typically but not always an address in the heap

– a pointer can refer to different places during a program

– a pointer can point to data structures that change in size or whose size is not known at compile-time

– NULL == 0 is the value of a pointer that does not reference a memory location (doesn't point to a memory address)

– a pointer can be explicitly dereferenced to refer to the data at which the pointer points (use the * operator for dereference)

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 14: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• pointers in C++

– a pointer to a class can refer to members of the class by using the pointer operator ->

• Card *c = new Card(3,3); // note use of new

• c->setRank(5); // same as (*c).setRank(5);

• // let's see this in code...

– a pointer can be subscripted if it is pointing to an array

– can do pointer arithmetic (again useful if pointing to an array), e.g. adding 1 to a pointer adds the size of the type to which the pointer points

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 15: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• pointers in C++

– from Budd's book:

int i = 7;

int j = 11;

int *p = &i; // & is address of, point now points to I

// p holds the address of i

// i holds the value 7

*p = *p + j; // p is being dereferenced, i now has value 18

p = &j; // p now holds the address of j

// let's put this in a program and print the values...Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 16: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• const is used to make something constant (like Java final)

• e.g.

• const int x = 10; // like Java's final int x = 10;

• when used with pointers though be careful which is being declared constant (the pointer or the thing being pointed to)

• from Budd's book p.33:

– int i = 7;

– const int *p = &i; // pointer to a constant

– int * const q = &i; // constant pointer

– const int * const r = &i; // a const pointer to a const

– // let's put this in a program

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 17: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• void *

– can be used to hold any type of pointer

– think of Object references in Java

– double d = 3.5;

– double *dp = &d;

– void *p = dp; // make p point to d (the thing dp points to)

– double dp2;

– dp2 = *((double *)p); // cast p to be a double pointer and deref

– // can't simply do: dp2 = *p;

– // let's put this in code

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 18: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• function pointers

– double fdiv(int i, int j)

– {

• return i / (double) j;

– }

– double (*fptr) (int, int); // fptr is a pointer to a function whose

– // arguments are two ints and has a return type of double

– // can assign to fptr the address of a function that has those

– // properties

– fptr = &fdiv;

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 19: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• function pointers

– useful when a function can be generically written to take a pointer to a function which it then calls internally

– see qsort example in Budd p. 34

Michael Eckmann - Skidmore College - CS 376b - Spring 2008

Page 20: CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann

C++• references used as parameters (for pass by reference)

– use & in parameter

– no need to do anything else

– void passByRef(int &i)

– {

• i++;

– }

– int x = 7;

– passByRef(x);

– // x is now 8

– // write program with Card functions (pass by ref and val)

Michael Eckmann - Skidmore College - CS 376b - Spring 2008