Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructors and Destructors

Similar presentations


Presentation on theme: "Constructors and Destructors"— Presentation transcript:

1 Constructors and Destructors

2 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

3 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 A1 ; } //end of Amain.cpp

4 The statements are transformed as follows
A A1 ; //memory allocated for the object A1.A(); as A(&A1);// 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

5 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 A1.A(); // not legal C++ code If we define constructor, the compiler does not define, but embeds implicit calls to the constructor

6 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

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

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

9 #include “Distance.h” #include<iostream.h> void main() { Distance d1; //constructor called cout<<d1.getFeet()<<””<<d1.getInches()<<endl; } //end of DistTest.cpp Output 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

10 The class ‘String’ will have two private data members – (i) a character pointer 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 * cStr; long unsigned int len; public : String(); // prototype of the constructor // rest of the class String }; // end of String.h

11 Suppose s1 is the object of class String
101 27 cStr 101 3 a b c \0 len s1 Suppose s1 is the object of class String We want to implement the following two conditions (i)cStr 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, cStr should initially set to NULL and len to zero

12 #include “String.h” String :: String() // definition of the constructor { cStr = 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

13 Distance :: Distance ( )
{ iFeet =0; fInches = 0.0 ; } Distance :: Distance ( int p, float q) { iFeet =p; setInches(q) ; } //function definitions void main() //object created in stack { Distance d1(1, 1.1); //parameterized constructor called cout<<d1.getFeet()<<“ ”<<d1.getInches(); } Output:

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

15 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 d1; // ambiguity error Parameterized constructor for class ‘String’ String s1(“abc”); //constructor would handle these char * cPtr=“abc” ; String s1(cPtr) ; //statements char cArr=“abc” ; String s1(Arr) ;

16


Download ppt "Constructors and Destructors"

Similar presentations


Ads by Google