Upcoming SlideShare
×

# Dynamics allocation

665 views

Published on

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
665
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
20
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Dynamics allocation

1. 1. Constructor and Destructor Arrays, Pointers, References, and the Dynamic Allocation Operators H S Rana Center For information Technology UPES Dehradun, India October 3, 2012 H S Rana (UPES) Constructor and Destructor October 3, 2012 1 / 23
2. 2. Constructor and Destructor Array Of Object Example The syntax for declaring and using anobject array is exactly the same as it is for any other type of array. class cl { int i; public: void set_i(int j) { i=j; } int get_i() { return i; } }; H S Rana (UPES) Constructor and Destructor October 3, 2012 2 / 23
3. 3. Constructor and Destructor Array Of Object Example The syntax for declaring and using anobject array is exactly the same as it is for any other type of array. class cl { int i; public: void set_i(int j) { i=j; } int get_i() { return i; } }; H S Rana (UPES) int main() { cl ob[3]; int i; for(i=0; i<3; i++) ob[i].set_i(i+1); for(i=0; i<3; i++) cout << ob[i].get_i()<< "n"; return 0; } Constructor and Destructor October 3, 2012 2 / 23
4. 4. Constructor and Destructor Array Of Object Example Parameterized Constructor can be used to initialize each object in array class cl { int i; public: cl(int j) { i=j; } // constructor int get_i() { return i; } }; H S Rana (UPES) Constructor and Destructor October 3, 2012 3 / 23
5. 5. Constructor and Destructor Array Of Object Example int main() { cl ob[3] = {1, 2, 3}; // initializers //Equal to cl ob[3] = { cl(1), cl(2), cl(3) }; int i; for(i=0; i<3; i++) cout << ob[i].get_i() << "n"; return 0; } H S Rana (UPES) Constructor and Destructor October 3, 2012 4 / 23
6. 6. Constructor and Destructor Array Of Object Example If an object’s constructor requires two or more arguments class cl { int h; int i; public: cl(int j, int k) { h=j; i=k; } // constructor with 2 paramet int get_i() {return i;} int get_h() {return h;} }; H S Rana (UPES) Constructor and Destructor October 3, 2012 5 / 23
7. 7. Constructor and Destructor Array Of Object Example int main() { cl ob[3] = { cl(1, 2), // initialize cl(3, 4), cl(5, 6) }; int i; for(i=0; i<3; i++) { cout << ob[i].get_h(); cout << ", "; cout << ob[i].get_i() << "n"; } return 0; } H S Rana (UPES) Constructor and Destructor October 3, 2012 6 / 23
8. 8. Constructor and Destructor Creating Initialized vs. Uninitialized Arrays If you want to create uninitialized array Example class cl { int i; public: cl(int j) { i=j; } int get_i() { return i; } }; H S Rana (UPES) Constructor and Destructor October 3, 2012 7 / 23
9. 9. Constructor and Destructor Creating Initialized vs. Uninitialized Arrays If you want to create uninitialized array Example class cl { int i; public: cl(int j) { i=j; } int get_i() { return i; } }; main function main() { cl a[9]; // error, } H S Rana (UPES) Constructor and Destructor October 3, 2012 7 / 23
10. 10. Constructor and Destructor Pointer to Object We can deﬁne pointers to objects. When accessing members of a class given a pointer to an object, use the arrow operator instead of the dot operator. Example class cl { int i; public: cl(int j) { i=j; } int get_i() { return i; } }; int main() { cl ob(88), *p; p = &ob; // get address of ob cout << p->get_i(); // use -> to call get_i() return 0; } H S Rana (UPES) Constructor and Destructor October 3, 2012 8 / 23
11. 11. Constructor and Destructor Pointer to Object when a pointer is incremented, it points to the next element of its type. For example, an integer pointer will point to the next integer. Example class cl { int i; public: cl() { i=0; } cl(int j) { i=j; } int get_i() { return i;}}; int main(){ cl ob[3] = {1, 2, 3} cl *p; int i; p = ob; // get start of array for(i=0; i<3; i++) { cout << p->get_i() << "n"; p++; // point to next object } H S Rana (UPES) 0;} Constructor and Destructor return October 3, 2012 9 / 23
12. 12. Constructor and Destructor The this Pointer When a member function is called, it is automatically passed an implicit argument that is a pointer to the invoking object (that is, the object on which the function is called). This pointer is called this. Example class pwr { double b; int e; double val; public: pwr(double base, int exp); double get_pwr() { return val; } }; H S Rana (UPES) Constructor and Destructor October 3, 2012 10 / 23
13. 13. Constructor and Destructor The this Pointer Example pwr::pwr(double base, int exp) {b = base; e = exp; val = 1; if(exp==0) return; for( ; exp>0; exp--) val = val * b;} H S Rana (UPES) Constructor and Destructor October 3, 2012 11 / 23
14. 14. Constructor and Destructor The this Pointer Example pwr::pwr(double base, int exp) {b = base; e = exp; val = 1; if(exp==0) return; for( ; exp>0; exp--) val = val * b;} Another Deﬁnition pwr::pwr(double base, int exp) { this->b = base; this->e = exp; this->val = 1; if(exp==0) return; for( ; exp>0; exp--) this->val = this->val * this->b; } H S Rana (UPES) Constructor and Destructor October 3, 2012 11 / 23
15. 15. Constructor and Destructor The this Pointer main function int main() { pwr x(4.0, 2), y(2.5, 1), z(5.7, 0); cout << x.get_pwr() << " "; cout << y.get_pwr() << " "; cout << z.get_pwr() << "n"; return 0; } H S Rana (UPES) Constructor and Destructor October 3, 2012 12 / 23
16. 16. Constructor and Destructor Dynamics Allocation of Memory Memory Allocation operators C++ provides two dynamic allocation operators: new and delete. These operators are used to allocate and free memory at run time.The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new H S Rana (UPES) Constructor and Destructor October 3, 2012 13 / 23
17. 17. Constructor and Destructor Dynamics Allocation of Memory Memory Allocation operators C++ provides two dynamic allocation operators: new and delete. These operators are used to allocate and free memory at run time.The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new General form The general forms of new and delete are : p_var = new type; delete p_var; Here, p_var is a pointer variable that receives a pointer to memory that is large enough to hold an item of type type. H S Rana (UPES) Constructor and Destructor October 3, 2012 13 / 23
18. 18. Constructor and Destructor Dynamics Allocation of Memory Example #include <iostream> #include <new> using namespace std; int main() { int *p; try { p = new int; // allocate space for an int } catch (bad_alloc xa) { cout << "Allocation Failuren"; return 1; } *p = 100; cout << "At " << p << " "; cout << "is the value " << *p << "n"; delete p; return (UPES) H S Rana 0; Constructor and Destructor October 3, 2012 14 / 23
19. 19. Constructor and Destructor Initializing Allocated Memory We can initialize allocated memory to some known value by putting an initializer after the type name in the new statement. H S Rana (UPES) Constructor and Destructor October 3, 2012 15 / 23
20. 20. Constructor and Destructor Initializing Allocated Memory We can initialize allocated memory to some known value by putting an initializer after the type name in the new statement. General form p_var = new var_type (initializer); H S Rana (UPES) Constructor and Destructor October 3, 2012 15 / 23
21. 21. Constructor and Destructor Initializing Allocated Memory Example #include <iostream> #include <new> using namespace std; int main() { int *p; try { p = new int (87); // initialize to 87 } catch (bad_alloc xa) { cout << "Allocation Failuren"; return 1; } cout << "At " << p << " "; cout << "is the value " << *p << "n"; delete p; return 0; } H S Rana (UPES) Constructor and Destructor October 3, 2012 16 / 23
22. 22. Constructor and Destructor Allocating Arrays We can allocate arrays using new operator. H S Rana (UPES) Constructor and Destructor October 3, 2012 17 / 23
23. 23. Constructor and Destructor Allocating Arrays We can allocate arrays using new operator. General form p_var = new array_type [size]; size speciﬁes the number of elements in the array. To free an array, use delete delete [ ] p_var; H S Rana (UPES) Constructor and Destructor October 3, 2012 17 / 23
24. 24. Constructor and Destructor Allocating Arrays Example #include <iostream> #include <new> using namespace std; int main() { int *p, i; try { p = new int [10]; // allocate 10 integer array } catch (bad_alloc xa) { cout << "Allocation Failuren"; return 1; } for(i=0; i<10; i++ ) p[i] = i; for(i=0; i<10; i++) cout << p[i] << " "; delete (UPES) p; // releaseConstructor array the and Destructor H S Rana [] October 3, 2012 18 / 23
25. 25. Constructor and Destructor Allocating Objects We can allocate object using new operator.When we do this, an object is created and a pointer is returned to it. H S Rana (UPES) Constructor and Destructor October 3, 2012 19 / 23
26. 26. Constructor and Destructor Allocating Objects We can allocate object using new operator.When we do this, an object is created and a pointer is returned to it. Example #include <iostream> #include <new> #include <cstring> using namespace std; class balance { double cur_bal; char name[80]; public: void set(double n, char *s) { cur_bal = n; strcpy(name, s);} void get_bal(double &n, char *s) { n = cur_bal; strcpy(s, name);}}; H S Rana (UPES) Constructor and Destructor October 3, 2012 19 / 23
27. 27. Constructor and Destructor Allocating Objects H S Rana (UPES) Constructor and Destructor October 3, 2012 20 / 23
28. 28. Constructor and Destructor Allocating Objects Example int main(){ balance *p; char s[80]; double n; try { p = new balance; } catch (bad_alloc xa) { cout << "Allocation Failuren"; return 1;} p->set(12387.87, "Rahul Kumar"); p->get_bal(n, s); cout << s << "’s balance is: " << n; cout << "n"; delete p; return 0;} H S Rana (UPES) Constructor and Destructor October 3, 2012 20 / 23
29. 29. Constructor and Destructor Allocating Objects dynamically allocated objects may have constructors and destructors H S Rana (UPES) Constructor and Destructor October 3, 2012 21 / 23
30. 30. Constructor and Destructor Allocating Objects dynamically allocated objects may have constructors and destructors Example class balance { double cur_bal; char name[80]; public: balance(double n, char *s) { cur_bal = n; strcpy(name, s); } ~balance() { cout << "Destructing "; cout << name << "n"; } void get_bal(double &n, char *s) { n = cur_bal; strcpy(s, name);} H S Rana (UPES) Constructor and Destructor }; October 3, 2012 21 / 23
31. 31. Constructor and Destructor Allocating Objects H S Rana (UPES) Constructor and Destructor October 3, 2012 22 / 23
32. 32. Constructor and Destructor Allocating Objects Example int main() { balance *p; char s[80]; double n; // this version uses an initializer try { p = new balance (12387.87, "Rahul Kumar"); } catch (bad_alloc xa) { cout << "Allocation Failuren"; return 1; } p->get_bal(n, s); cout << s << "’s balance is: " << n; cout << "n"; delete p; returnH S0; (UPES) Rana Constructor and Destructor October 3, 2012 22 / 23
33. 33. Constructor and Destructor Const Argument An argument of a function can be declared as const. H S Rana (UPES) Constructor and Destructor October 3, 2012 23 / 23
34. 34. Constructor and Destructor Const Argument An argument of a function can be declared as const. Example int strlrn(const char *p) int length(const String &s) The qualiﬁer const tell the compiler that the function should not modify the argument. Compiler will generate the error when this condition is violated. H S Rana (UPES) Constructor and Destructor October 3, 2012 23 / 23