1 / 7

CSE 333 – Section 4

CSE 333 – Section 4. References, const and classes. HW2. Index the contents of files Search through documents containing specified words Feels good when you complete it. This or that?. Consider the following code: Pointers : References: int i ; int i ;

morse
Download Presentation

CSE 333 – Section 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 333 – Section 4 References, const and classes

  2. HW2 • Index the contents of files • Search through documents containing specified words • Feels good when you complete it

  3. This or that? • Consider the following code: Pointers: References: inti; inti; int *pi = &i; int &ri= i; In both cases, The difference lies in how they are used in expressions: *pi = 4; ri = 4;

  4. Pointers and References • Once a reference is created, it cannot be later made to reference another object. • Compare to pointers, which are often reassigned. • References cannot be null, whereas pointers can. • References can never be uninitialized. It is also impossible to reinitialize a reference.

  5. C++ const declaration • As a declaration specifier, const is a type specifier that makes objects unmodifiable. constint m = 255; • Reference to constant integer: int n = 100; constint&ri = n; //ri becomes read only

  6. When to use? • Function parameter types and return types and functions that declare overloaded operators. • Pointers: may point to many different objects during its lifetime. Pointer arithmetic (++ or --) enables moving from one address to another. (Arrays, for e.g.) • References: can refer to only one object during its lifetime. • Style Guide Tip: • use const reference parameters to pass input • use pointers to pass output parameters • input parameters first, then output parameters last

  7. C++ Classes /* Note: This code is unfinished! Beware! */ class Point { public: Point(constint x, constint y); // constructor intget_x() { return x_; } // inline member function intget_y() { return y_; } // inline member function double distance(constPoint &p); // member function void setLocation(constint x, constint y); //member function private: intx_; // data member inty_; // data member }; // class Point

More Related