1 / 7

Review

Review. Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space for a pointer to an int Pointer and pointee are different Space for pointee must be allocated before pointer dereferencing is sensible

luke
Download Presentation

Review

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. Review • Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space for a pointer to an int • Pointer and pointee are different • Space for pointee must be allocated before pointer dereferencing is sensible • Assignment to a pointer copies address, not contents CSE 250

  2. Where am I? • The & operator yields the address of a variable Examples: int x = 12; int * px; px = &x; // assigns to px the address of x CSE 250

  3. “Clean up after yourself!” • C++ does not have automatic garbage collection like Java does • It is the programmer’s responsibility to return to free space any memory reserved using the new operator. • Memory allocated using new must be freed using delete. CSE 250

  4. delete operator int * px; px = new int(3); … do some work until finished with the space px points to… delete px; CSE 250

  5. Memory areas CSE 250

  6. Classes • Syntax to define classes in C++ is similar but not identical to that in Java. • The class is declared independently of its implementation. CSE 250

  7. Class declaration class Point { public: Point(int,int); int getX(); int getY(); void setX(int); void setY(int); private: int x, y; }; CSE 250

More Related