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

Slides:



Advertisements
Similar presentations
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CS 376b Introduction to Computer Vision 01 / 22 / 2008 Instructor: Michael Eckmann.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers and References
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Java Programming Language
Pointers and References
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
SPL – PS3 C++ Classes.
Presentation transcript:

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

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

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

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

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

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

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

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 programs Michael Eckmann - Skidmore College - CS 376b - Spring 2008

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 } 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

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

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

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

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

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

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

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

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

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

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