1 / 24

Lecture 21

Lecture 21. CIS 208 Wednesday, April 20, 2005. Constructors. Initialization functions for classes Automatically called when object is created. May be overloaded. Constructors. No return types. Name of constructor must be same as class. implemented just like any other function. .

wendi
Download Presentation

Lecture 21

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. Lecture 21 CIS 208 Wednesday, April 20, 2005

  2. Constructors • Initialization functions for classes • Automatically called when object is created. • May be overloaded.

  3. Constructors • No return types. • Name of constructor must be same as class. • implemented just like any other function.

  4. Constructors • global variables: called before main() • local: called when declared. • Not necessary.

  5. constructors class X { int a; public : X(int j) {a =j;} int geta() {return a;} }; int main() { X ob = 99; X ob(99); cout << ob.geta(); return 0; }

  6. single parameter constructors • X ob = 99; == X ob(99); • only works on constructors with one parameter.

  7. Deconstructors • complement of constructors • Automatically called when object is destroyed. • global: after program termination • local: after code segment

  8. Deconstructors • class foo; • constructor == foo(…); • deconstructor == ~foo(); • no parameters.

  9. why use deconstructors? • Deal with last minute issues. • unallocated any memory. • Close a file.

  10. Passing Objects • Usual call-by-value vs. call-by-reference • CBV: separate copy of object made • Exact copy, all value identical • regular constructor is not called • Deconstructor is called.

  11. pointers to objects • Work just like C pointers. • use -> to access class functions/fields.

  12. pointers class cl{ int j; public ; cl(int k) { j = k;} int getJ() { return j;} }; int main() { cl ob(88), *p; p = &ob; cout << p ->getJ(); return 0; }

  13. passing pointers • Call by reference. • No constructor or deconstructor when creating pointers. .

  14. dynamic memory allocation • 2 new commands. • new • delete • a lot like malloc() & free()

  15. new • returns a pointer to the new allocated memory space • the pointer is of the correct type. • casting isn’t necessary

  16. delete • destroys memory pointed to by pointer. • Just like free() • unlike free(), delete calls deconstructor, and you can try to delete unallocated pointers.

  17. new int main() { int *p,*q; p = new int; *p = 100; q = new int(5); cout<< “At “ << p << “ “; cout << “is the value “ << *p << “\n”; delete p; return ;

  18. initializing allocation memory • p_var = new var_type(initial data); • cl *p = new cl(99); • creates new cl object, sends 99 to constructor. delete p;

  19. NULL • if using NULL, need to #include <cstdlib>

  20. recursive constructors class foo{ foo *child; public : foo(); ~foo(); };

  21. continued foo:: foo() { child = new foo; } foo::~foo() { delete child; }

  22. bad allocation • new doesn’t return NULL if it fails. • It actually throws an exception. • We’ll talk about exception handling Friday.

  23. making new return NULL; • use nothrow operator. p_var= new(nothrow) type; • return NULL on failure • must #include <new> for this.

More Related