Constructors AND Destructors
Constructors AND Destructors
Constructors AND Destructors
AND
DESTRUCTORS
CONSTRUCTORS
A member function with the same as its class is called Constructor and it is used to
initialize the object of that class with a legal initial value.
Example :
class student
{
private:
int rollno;
float marks;
public:
student( ) //Constructor
{ rollno=0; marks=0.0; }
};
CONSTRUCTOR CALLING
A constructor can be called in two ways: implicitly and explicitly.
class student
class student
{
{
private:
private:
int rollno;
int rollno;
float marks;
float marks;
public:
public:
student( ) //Constructor
student( ) //Constructor
{ rollno=0; marks=0.0; }
{ rollno=0; marks=0.0; }
};
};
void main()
void main()
{
{
student s1 = student(); //explicit call
student s1; //implicit call
}
}
Constructor does not have any return type not even void.
Note: It is necessary to pass an object by reference in copy constructor because if do not pass the
object by reference then a copy constructor will be invoked to make the copy of the object.
In this process copy constructor will be invoked again and it will be a recursive process ie this
process will repeat again and again endlessly resulting in running short of memory. Ultimately our
system will hang.
COPY CONSTRUCTOR EXAMPLE
Example : class student student( student & s) // Copy
{ Constructor
Statement1: default private: { rollno= s.rollno;
int rollno; strcpy(name, s.name);
constructor is called.
char name[20]; marks =s.marks;
float marks; }
Statement2: parametrized public: }
constructor is called. student( ) void main()
// Default Constructor {
Statement 3: copy { rollno=0; marks=0.0; } student s1; //statement1
constructor is called. student(int roll, char nam[20], student s2( 1, “RAJESH”, 450); //
float mar) statemet2
Statement4: Parametrized // Parametrized Constructor student s3(s2); //statement3
constructor is called. { rollno=roll; student s4(2, “MOHIT”,320);
strcpy(name, nam); //statement4
marks =mar; } student s5=s4; //statement5
statement5: Copy }
constructor is called.
COPY CONSTRUCTOR INVOCATION
Case 1: When an object is passed by value to a function: The pass by value method requires a
copy of the passed argument to be created for the function to operate upon. Thus to create the copy
of the passed object, copy constructor is invoked. Example:
Destructors are usually used to deallocate memory and do other cleanup for
a class object and its class members when the object goes out of scope.
The compiler automatically calls constructors when defining class objects and
calls destructors when class objects go out of scope.
The constructor of the base class is called first and then the constructor of the
child class/es.
The destructor for a child class object is called before destructors for base
class/es are called.
If we do not declare our own constructor and destructor in the class, then they
are automatically defined by the compiler.
POINTS TO REMEMBER Contd……
I f we declare our own constructor of any one type then it is necessary to define constructors
of other types also otherwise we will not be able to create an object whose constructor
definition is not given in the class.
C++ compiler uses the concept of stack in memory allocation and deallocation. Thus
compiler creates the memory in sequential order and delete the memory in reverse order.
x2 x1
Consider the class student declared in the previous slides. If the main
program is as given below then allocation and deallocation of x3
Deallocation
memory to objects will take place as given in the figure::
x3
Allocation
void main()
{ x2
student x1, x2, x3; x1
}
EXAMPLE EXPLAINED
class student student( student & s) // Copy
{ Constructor
private: { rollno= s.rollno;
strcpy(name, s.name); OUTPUT
int rollno;
char name[20]; marks =s.marks;
cout<<“copy constructor Default constructor called
float marks; Parametrized constructor
public: called\n”;
} called
student( ) // Default Constructor Copy constructor called
{ rollno=0; marks=0.0; ~student() //Destructor
{ cout<<“Destructor Parametrized constructor
cout<<“Default constructor called\n”; } called
called\n”; }
} Copy constructor called
student(int roll, char nam[20], float mar) Destructor called
// Parametrized Constructor void main()
{ Destructor called
{ rollno=roll; Destructor called
strcpy(name, nam); student s1;
student s2( 1, “RAJESH”, 450); Destructor called
marks =mar; Destructor called
cout<<“Parametrized constructor student s3(s2);
called\n”; student s4(2, “MOHIT”,320);
} student s5=s4; }