CS201-39 - Introduction to Programming Lecture 39 Copy...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 39 Copy Constructor Pointers Review 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 Float Pointer to a Character Pointer to Objects References & const Dynamic Memory Allocation Native Operator new delete Dynamic Memory Allocation int *p ; p = new int ; delete p ; Dynamic Memory Allocation int *p ; p = new int [ 10 ] ; delete [ ] p ; class Matrix { private : int * m ; int row , col ; public : Matrix ( int rows , int cols ) { m = new int [ rows * cols ] ; } } ; Example delete [ ] m ; Assignment int i = 0 ; //Initialization int i ; i = 0 ; //Assignment Matrix m1 , m2 ; …… m2 = m1 ; //Assignment Statement Member to Member Assignment m2 = m1 int *m of m1 0xefffdad0 int *m of m2 0xefffdad0 Pointing to the same region in memory mx int *m of m1 0xefffdad0 int *m of m2 0xefffdad0 Pointing to the same region in memory mx Copy Constructor Call by value Shallow Copy Deep Copy Matrix ( Matrix & ) ; class String { Example char * c ; public : void copy ( char * s ) ; String ( char * data ) ; void print ( ) ; // etc. Example String s1 ( “test1” ) ; String s2 = s1 ; s1.copy ( “this is a test” ) ; s2.print ( ) ; int i ; i = 10 ; i = i ; Matrix m2 ( m1 ) ; Matrix m2 = m1 ; Rules For dynamic memory allocation 1. Define copy constructor 2. Write assignment operator 3. Provide destructor 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 ...
View Full Document

This note was uploaded on 01/12/2010 for the course CS CS 201 taught by Professor Dr.naveedmalik during the Spring '09 term at Virtual University of Pakistan.

Ask a homework question - tutors are online