Constructors and Destructors The constructor gets called automatically

  • Slides: 16
Download presentation
Constructors and Destructors

Constructors and Destructors

 • The constructor gets called automatically for each object that has just got

• The constructor gets called automatically for each object that has just got created • It appears as member function of each class, whether it is defined or not • It has the same name as that of the class • It may or may not take parameters, does not return any thing and not even void • The prototype is - <class name> (<param list>) • It guarantees the initialization of the data members of the class • Domain constraints on the values of data members can also be implemented via constructors

 • The compiler embeds a call to the constructor for each object when

• The compiler embeds a call to the constructor for each object when it is created class A { int x; public : void setx (const int = 0); int getx ; }; // end of A. h # include”A. h” void main() { A A 1 ; } //end of Amain. cpp

The statements are transformed as follows A A 1 ; //memory allocated for the

The statements are transformed as follows A A 1 ; //memory allocated for the object A 1. A(); as A(&A 1); // constructor called implicitly Similarly, the constructor is called for each object that is created dynamically in the heap by the new operator A * Aptr; Aptr = new A; as 2 statements Aptr = new A; Aptr -> A(); as A(Aptr) ; //constructor called implicitly by the compiler • Constructors do not allocate memory for objects but are the member functions that are called after memory has been allocated for the object • The compiler prototypes and defines constructor for us with out any statements in the definition • •

class A { …. . public: A(); // prototype inserted by compiler …. };

class A { …. . public: A(); // prototype inserted by compiler …. }; A : : A() { } //empty defn. inserted by compiler • The compiler defines the constructor in order to resolve the call to the constructor that it compulsorily places for the object being created • Explicit call to the constructor for an existing object is forbidden A 1. A(); // not legal C++ code • If we define constructor, the compiler does not define, but embeds implicit calls to the constructor

 • Being a non-static member, constructor takes the ‘this’ pointer as a leading

• Being a non-static member, constructor takes the ‘this’ pointer as a leading formal argument • The address of the invoking object is passed as a leading parameter to the constructor call. Hence members of the invoking object can be accessed from with in the definition of the constructor class A { int x; public : A() ; void setx (const int = 0); int getx ; }; // end of A. h

# include”A. h” #include<iostream. h> A : : A() { cout << “constructor of

# include”A. h” #include<iostream. h> A : : A() { cout << “constructor of class A calledn”; } // definitions of rest of functions of class A // end of A. cpp # include”A. h” void main() { A A 1 ; cout << “ End of Program n “; } //end of Amain. cpp Output : constructor of class A called

 • For the class Distance, we would like to set the values of

• For the class Distance, we would like to set the values of ‘i. Feet’ and ‘f. Inches’ to 0 and 0. 0 class Distance { public: Distance (); //our own constuctor //rest of class Distance }; //end of Distance. h #include “Distance. h” Distance : : Distance ( ) { i. Feet =0; f. Inches = 0. 0 ; } //definitions of rest of functions of class Distance // end of Distance. cpp

 • • #include “Distance. h” #include<iostream. h> void main() { Distance d 1;

• • #include “Distance. h” #include<iostream. h> void main() { Distance d 1; //constructor called cout<<d 1. get. Feet()<<””<<d 1. get. Inches()<<endl; } //end of Dist. Test. cpp Output 0 0. 0 There is a guaranteed initialization of data members of the objects of the class Distance The constructor that does not take any arguments – default / zero-argument constructor We define a class instead of character arrays

 • The class ‘String’ will have two private data members – (i) a

• The class ‘String’ will have two private data members – (i) a character pointing at dynamically allocated block of memory that contains the actual character array (ii) a long unsigned integer containing the length of this array class String { char * c. Str; long unsigned int len; public : String(); // prototype of the constructor // rest of the class String }; // end of String. h

c. Str 27 101 len 3 101 a b c � s 1 •

c. Str 27 101 len 3 101 a b c s 1 • Suppose s 1 is the object of class String • We want to implement the following two conditions (i)c. Str should point either to the dynamically allocated block of memory exclusively allocated for it or NULL (ii) There should be no memory leaks • When the object of the class is created, c. Str should initially set to NULL and len to zero

#include “String. h” String : : String() // definition of the constructor { c.

#include “String. h” String : : String() // definition of the constructor { c. Str = NULL ; len = 0 ; } // definitions of rest of functions of class String // end of String. cpp • Parameterized constructors • Constructors take arguments and can, therefore be overloaded public : Distance(); //part of Distance. h Distance(int, float); // parameterized constructor

Distance : : Distance ( ) { i. Feet =0; f. Inches = 0.

Distance : : Distance ( ) { i. Feet =0; f. Inches = 0. 0 ; } Distance : : Distance ( int p, float q) { i. Feet =p; set. Inches(q) ; } //function definitions void main() //object created in stack { Distance d 1(1, 1. 1); //parameterized constructor called cout<<d 1. get. Feet()<<“ ”<<d 1. get. Inches(); }

void main() //object created in heap { Distance * d. Ptr; d. Ptr =

void main() //object created in heap { Distance * d. Ptr; d. Ptr = new Distance d 1(1, 1. 1); //parameterized constructor called cout<<d. Ptr->get. Feet()<<“ ”<<d 1 ->get. Inches(); } Output: 1 1. 1 • If only the parameterized constructor is provided with out zero-argument one, the compiler will not provide the default constructor. On compiling Distance d 1; // ERROR: no matching constructor • Hence zero-argument constructor is a must

 • The formal arguments of the parameterized constructor can be assigned default values

• The formal arguments of the parameterized constructor can be assigned default values • But, in that case zero-argument constructor should not be provided, because an ambiguity error will arise when we attempt to create an object with out passing any values for the constructor • Distance ( int =0, float = 0. 0 ) ; // default values • If we write, Distance d 1; // ambiguity error • Parameterized constructor for class ‘String’ • String s 1(“abc”); //constructor would handle these • char * c. Ptr=“abc” ; String s 1(c. Ptr) ; //statements • char c. Arr=“abc” ; String s 1(Arr) ;