1 / 31

Introduction to Programming

Introduction to Programming. Lecture 39. Copy Constructor. Review. Pointers References Memory Allocation. Pointers. A pointer is a special type of variable that contain a memory address. Void Pointer Pointer to an Integer Pointer to a Character Pointer to a Float

Download Presentation

Introduction to Programming

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. Introduction to Programming Lecture 39

  2. Copy Constructor

  3. Review • Pointers • References • Memory Allocation

  4. Pointers A pointer is a special type of variable that contain a memory address

  5. Void Pointer • Pointer to an Integer • Pointer to a Character • Pointer to a Float • Pointer to Objects

  6. References

  7. &

  8. const

  9. Dynamic Memory Allocation

  10. Native Operator • new • delete

  11. Dynamic Memory Allocation int *p ; p = new int ; delete p ;

  12. Dynamic Memory Allocation int *p ; p = new int [ 10 ] ; delete [ ] p ;

  13. Example class Matrix { private : int * m ; int row , col ; public : Matrix ( int rows , int cols ) { m = new int [ rows * cols ] ; } } ;

  14. delete [ ] m ;

  15. Assignment

  16. int i = 0 ; //Initialization int i ; i = 0 ; //Assignment

  17. Matrix m1 , m2 ; …… m2 = m1 ; //Assignment Statement

  18. Member to Member Assignment

  19. m2 = m1

  20. Pointing to the same region in memory int *m of m1 0xefffdad0 mx int *m of m2 0xefffdad0

  21. Pointing to the same region in memory int *m of m1 0xefffdad0 mx int *m of m2 0xefffdad0

  22. Copy Constructor

  23. Call by value

  24. Deep Copy Shallow Copy

  25. Matrix ( Matrix & ) ;

  26. Example class String { char * c ; public : void copy ( char * s ) ; String ( char * data ) ; void print ( ) ; // etc. } ;

  27. Example String s1 ( “test1” ) ; String s2 = s1 ; s1.copy ( “this is a test” ) ; s2.print ( ) ;

  28. int i ; i = 10 ; i = i ;

  29. Matrix m2 ( m1 ) ; Matrix m2 = m1 ;

  30. Rules For dynamic memory allocation • Define copy constructor • Write assignment operator • Provide destructor

  31. What have we covered today • Review of pointers • Dynamic memory allocation • new • Delete • For memory allocation in classes we must provide • Constructor • Copy constructor • Assignment operator • destructor

More Related