Dynamics allocation

34
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

description

 

Transcript of Dynamics allocation

Page 1: Dynamics allocation

Constructor and Destructor

Arrays, Pointers, References, and the Dynamic AllocationOperators

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

Page 2: Dynamics allocation

Constructor and Destructor

Array Of Object

Example

The syntax for declaring andusing anobject array is exactlythe same as it is for any othertype of array.

class cl {int i;public:void set_i(int j) { i=j; }int get_i() { return i; }};

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;}

H S Rana (UPES) Constructor and Destructor October 3, 2012 2 / 23

Page 3: Dynamics allocation

Constructor and Destructor

Array Of Object

Example

The syntax for declaring andusing anobject array is exactlythe same as it is for any othertype of array.

class cl {int i;public:void set_i(int j) { i=j; }int get_i() { return i; }};

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;}

H S Rana (UPES) Constructor and Destructor October 3, 2012 2 / 23

Page 4: Dynamics allocation

Constructor and Destructor

Array Of Object

Example

Parameterized Constructor canbe used to initialize eachobject in array

class cl {int i;public:cl(int j) { i=j; } // constructorint get_i() { return i; }};

H S Rana (UPES) Constructor and Destructor October 3, 2012 3 / 23

Page 5: Dynamics allocation

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

Page 6: Dynamics allocation

Constructor and Destructor

Array Of Object

Example

If an object’s constructor requires two or more argumentsclass cl {

int h;int i;public:cl(int j, int k) { h=j; i=k; } // constructor with 2 parametersint get_i() {return i;}int get_h() {return h;}};

H S Rana (UPES) Constructor and Destructor October 3, 2012 5 / 23

Page 7: Dynamics allocation

Constructor and Destructor

Array Of Object

Example

int main(){cl ob[3] = {cl(1, 2), // initializecl(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

Page 8: Dynamics allocation

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

Page 9: Dynamics allocation

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

Page 10: Dynamics allocation

Constructor and Destructor

Pointer to Object

We can define pointers to objects. When accessing members of a class given apointer 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 obcout << p->get_i(); // use -> to call get_i()return 0;}

H S Rana (UPES) Constructor and Destructor October 3, 2012 8 / 23

Page 11: Dynamics allocation

Constructor and Destructor

Pointer to Object

when a pointer is incremented, it points to the next element of its type. Forexample, 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 arrayfor(i=0; i<3; i++) {cout << p->get_i() << "\n";p++; // point to next object }return 0;}H S Rana (UPES) Constructor and Destructor October 3, 2012 9 / 23

Page 12: Dynamics allocation

Constructor and Destructor

The this Pointer

When a member function is called, it is automatically passed an implicit argumentthat is a pointer to the invoking object (that is, the object on which the functionis 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

Page 13: Dynamics allocation

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 Definition

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

Page 14: Dynamics allocation

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 Definition

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

Page 15: Dynamics allocation

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

Page 16: Dynamics allocation

Constructor and Destructor

Dynamics Allocation of Memory

Memory Allocation operators

C++ provides two dynamic allocation operators: new and delete. Theseoperators are used to allocate and free memory at run time.The new operatorallocates memory and returns a pointer to the start of it. The delete operatorfrees 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 tomemory that is large enough to hold an item of type type.

H S Rana (UPES) Constructor and Destructor October 3, 2012 13 / 23

Page 17: Dynamics allocation

Constructor and Destructor

Dynamics Allocation of Memory

Memory Allocation operators

C++ provides two dynamic allocation operators: new and delete. Theseoperators are used to allocate and free memory at run time.The new operatorallocates memory and returns a pointer to the start of it. The delete operatorfrees 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 tomemory that is large enough to hold an item of type type.

H S Rana (UPES) Constructor and Destructor October 3, 2012 13 / 23

Page 18: Dynamics allocation

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 Failure\n";return 1;}

*p = 100;cout << "At " << p << " ";cout << "is the value " << *p << "\n";delete p;return 0;}

H S Rana (UPES) Constructor and Destructor October 3, 2012 14 / 23

Page 19: Dynamics allocation

Constructor and Destructor

Initializing Allocated Memory

We can initialize allocated memory to some known value by putting an initializerafter 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

Page 20: Dynamics allocation

Constructor and Destructor

Initializing Allocated Memory

We can initialize allocated memory to some known value by putting an initializerafter 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

Page 21: Dynamics allocation

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 Failure\n";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

Page 22: Dynamics allocation

Constructor and Destructor

Allocating Arrays

We can allocate arrays using new operator.

General form

p_var = new array_type [size];

size specifies 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

Page 23: Dynamics allocation

Constructor and Destructor

Allocating Arrays

We can allocate arrays using new operator.

General form

p_var = new array_type [size];

size specifies 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

Page 24: Dynamics allocation

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 Failure\n";return 1;}

for(i=0; i<10; i++ )p[i] = i;for(i=0; i<10; i++)cout << p[i] << " ";delete [] p; // release the arrayreturn 0;

}

H S Rana (UPES) Constructor and Destructor October 3, 2012 18 / 23

Page 25: Dynamics allocation

Constructor and Destructor

Allocating Objects

We can allocate object using new operator.When we do this, an object is createdand 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

Page 26: Dynamics allocation

Constructor and Destructor

Allocating Objects

We can allocate object using new operator.When we do this, an object is createdand 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

Page 27: Dynamics allocation

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 Failure\n";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

Page 28: Dynamics allocation

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 Failure\n";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

Page 29: Dynamics allocation

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

Page 30: Dynamics allocation

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

Page 31: Dynamics allocation

Constructor and Destructor

Allocating Objects

Example

int main(){balance *p;char s[80];double n;// this version uses an initializertry {p = new balance (12387.87, "Rahul Kumar");} catch (bad_alloc xa) {cout << "Allocation Failure\n";return 1;}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 22 / 23

Page 32: Dynamics allocation

Constructor and Destructor

Allocating Objects

Example

int main(){balance *p;char s[80];double n;// this version uses an initializertry {p = new balance (12387.87, "Rahul Kumar");} catch (bad_alloc xa) {cout << "Allocation Failure\n";return 1;}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 22 / 23

Page 33: Dynamics allocation

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 qualifier const tell the compiler that the function should not modify theargument. Compiler will generate the error when this condition is violated.

H S Rana (UPES) Constructor and Destructor October 3, 2012 23 / 23

Page 34: Dynamics allocation

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 qualifier const tell the compiler that the function should not modify theargument. Compiler will generate the error when this condition is violated.

H S Rana (UPES) Constructor and Destructor October 3, 2012 23 / 23