Constructors and Destructors

3,778 views
3,578 views

Published on

Constructors, Destructors, call in parameterized Constructor, Multiple constructor in a class, Explicit/implicit call, Copy constructor, Dynamic Constructors and call in parameterized Constructor

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,778
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
368
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Constructors and Destructors

  1. 1. CONSTRUCTORSANDDESTRUCTORSHarinder SinghSukhpal SinghThapar University, PatialaBatch: 2011-2013
  2. 2. WHAT IS A CONSTRUCTOR ? In c++ , a constructor is a ‘special’ memberfunction whose task is to initialize the objectsof its class. It is special because its name is the same asthe class name. It is called constructor because it constructsthe value of data members of the class. The constructor is invoked whenever an objectof its associated class is created.ssgill©tupatiala
  3. 3. CLASS WITH A CONSTRUCTORClass integer {int m, n;public:integer(void); // constructor declared};Integer : : integer (void) // constructor defined{m=0; n=0;}Object declartion:Integer int1; // object int1 createdssgill©tupatiala
  4. 4. CHARACTERISTICS OF THE CONSTRUCTOR They should be declared in public section. They are invoked automatically when the objects arecreated. They dont have return types, cannot return values. A constructor that accepts no parameters is calleddefault constructor. The default constructor for classA is A:: A() . The statement A a; invokes the defaultconstructor of the compiler to create the object a.ssgill©tupatiala
  5. 5. PARAMETERIZED CONSTRUCTORThe constructor that can take arguments.Class integer {int m, n;public:integer(int x, int y); //parameterized constructor};Integer : : integer (int x, int y){m=x; n=y;}Object declaration statement “integer int1 ; “ do not workhere.ssgill©tupatiala
  6. 6. CALL IN PARAMETERIZED CONSTRUCTOR We must pass the initial values as arguments tothe constructor function when object is created.This can be done in two ways: By calling the constructor explicitly.integer int1 = integer(0, 100); By calling the constructor implicitly.integer int1(0, 100);This method is also called shorthand method.ssgill©tupatiala
  7. 7. EXPLICIT/IMPLICIT CALLint main(){integer int1(0, 100); //implicit callinteger int2 = integer(25, 75); //explicit callcout<<“object 1:”int1.function1();cout<<“object 2:”int2.function1();return 0;}Outputobject1: m=0 and n= 100object2: m=25 and n= 75ssgill©tupatiala
  8. 8. MULTIPLE CONSTRUCTOR IN A CLASSClass integer{int m, n;public:integer() //default constructor{m=0; n=0;}integer(int a, int b) //parameterized constructor{m=a; n=b;}integer(integer & i) //copy constructor{m= i.m; n= i.n;}};ssgill©tupatiala
  9. 9. CONSTRUCTORS WITH DEFAULT ARGUMENTScomplex(float real , float imag=0){} complex c1(5.0); complex c2(2.0,3.0); Difference b/w A::A() and A::A(int a=0).ssgill©tupatiala
  10. 10. DYNAMIC INITIALIZATION OF OBJECTSProvide various initialization formats usingoverloaded constructors. Fixed_deposit() { } Fixed_deposit(long int p, int y ,float r=0.12); Fixed_deposit( long int p, int y, int r);ssgill©tupatiala
  11. 11. COPY CONSTRUCTOR It is used to declare and initialize an objectfrom another object.e.g• code() { }• code( int a) {id = a;}• code( code &x){ id = x.id; } //copy constructor. And various calls for them are:• code A(100); // object A is Created and initialized.• code B(A); // copy constructor called.• code C = A; // copy constructor called again.• code D; // object D is created , not initializedD=A; // copy constructor is not called.ssgill©tupatiala
  12. 12. DYNAMIC CONSTRUCTORS It is used to allocate memory while creatingobjects.• Enable the system to allocate right amount ofmemory for each object when objects are not ofsame size.• Thus resulting in saving of memory.ssgill©tupatiala
  13. 13. DYNAMIC CONSTRUCTORSclass String{char *name;int length;public:String (){ length = 0;name = new char[length+1];}String (char *s){ length = strlen(s);name = new char[length+1];strcpy(name, s);}};main(){Char *first = “ME_SE_CR” ;String name1( first);String name2(“ Rupinder”);...}ssgill©tupatiala
  14. 14. DESTRUCTORS ~ integer() { } As new is used to allocate memory indynamic constructors, here we use delete tofree that memory. Never take any argument nor does it return anyvalue. It will be invoked implicitly by compiler uponexit from program or a block or a function.ssgill©tupatiala
  15. 15. DESTRUCTORSInt count = 0;class test1{public :~test1(){count ++;cout <<“n no. of object created “<< count ;}~test1 (){ cout <<“n no. of object destroyed”<< count ;Count - - ;}};main(){cout << “n Enter Main n”;test1 t1, t2, t3, t4;{cout <<“n Entered in block1” ;test1 t5;}{cout <<“n Entered in block2” ;test1 t6;}cout << “n Re-Enter Main n”;return 0;}ssgill©tupatiala
  16. 16. OUT PUT Enter MainNo. of object created 1No. of object created 2No. of object created 3No. of object created 4 Enter block 1No. of object created 5No. of object destroyed 5 Enter block 2No. of object created 5No. of object destroyed 5 Re - Enter MainNo. of object destroyed 4No. of object destroyed 3No. of object destroyed 2No. of object destroyed 1ssgill©tupatiala
  17. 17. THANK YOUssgill©tupatiala

×