Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Constructors AND Destructors

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

CONSTRUCTORS

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

IMPLICIT CALL OF CONSTRUCTOR EXPLICIT CALL OF CONSTRUCTOR


FEATURES OF CONSTRUCTORS

 Constructor has the same name as class.

 Constructor does not have any return type not even void.

 Constructor can be declared only in the public section of the class.

 Constructor is called automatically when the object of the class is


created.
TYPES OF CONSTRUCTOR

1. Default Constructor: A constructor that accepts no parameter is called the Default


Constructor.

2. Parameterized Constructor: A constructor that accepts parameters for its invocation is


known as parameterized Constructor, also called as Regular Constructor.

3. Copy Constructor: A copy constructor is a special constructor in the C++ programming


language used to create a new object as a copy of an existing object.
DEFAULT CONSTRUCTOR
Default constructor takes no argument.
It is used to initialize an object of a class with legal initial values.
Example :
class student
{
private:
int rollno;
float marks;
public:
student( ) // Default Constructor
{ rollno=0; marks=0.0; }
void display();
void enter();
};
void main()
{
student s1; // default constructor called
}
PARAMETRISED CONSTRUCTOR
 Parametrized constructor accepts
class student
arguments.
{
 It initializes the data members of private: void display();
the object with the arguments int rollno; void enter();
passed to it. char name[20]; };
 When the object s1 is created, float marks;
default constructor is called and public: void main()
when the objects s2 is created, student( ) {
parametrized constructor is called // Default Constructor student s1;
and this is implicit call of { rollno=0; marks=0.0; } student s2(1, “RAMESH”,
student(int roll, char nam[20], 350);
parametrized constructor.
float mar) student s3=student(2,
 When the object s3 is created, // Parametrized Constructor ”KAJAL”, 400);
again the parametrized { rollno=roll; }
constructor is called but this strcpy(name, nam);
explicit call of parametrized marks =mar;
constructor. }
COPY CONSTRUCTOR
 Copy constructor accepts an object as an argument.
 It creates a new object and initializes its data members with data members of the object passed
to it as an argument.

Syntax : class_name( class_name & object_name)


{

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:

class student student( student & s) // Copy


{ Constructor
private: { rollno= s.rollno;
int rollno; strcpy(name, s.name); Explanation:
char name[20]; marks =s.marks;
float marks; } As we are passing the
public: } object s6 by value so
student( ) // Default Constructor void job( student stu) when the function call
{ rollno=0; marks=0.0; } { } job is invoked a copy of
stu is created and values
student(int roll, char nam[20], void main( )
of object s6 are copied to
float mar) //Parametrized {
object stu.
{ rollno=roll; student s6(6, “KAPIL”, 245);
strcpy(name, nam); job(s6);
marks =mar; } }
COPY CONSTRUCTOR INVOCATION Contd…..
Case 2: When a function returns an object : When an object is returned by a function the copy
constructor is invoked.
Explanation:
class student student( student & s) // Copy
Constructor  When the function job is
{
private: { rollno= s.rollno; invoked it creates a
int rollno; strcpy(name, s.name); temporary object t1 and
char name[20]; marks =s.marks; then returns it.
float marks; }  At that time the copy
public: } constructor is invoked to
student( ) // Default student job( ) create a copy of the object
{ rollno=0; marks=0.0; } { student t1(12, “AJAY”, 443);
t1 returned by job() and its
student(int roll, char nam[20], return t1; }
value is assigned to s7.
float mar) // Parametrised
void main( )  The copy constructor
{ rollno=roll;
strcpy(name, nam); { creates a temporary object
marks =mar; student s6(6, “KAPIL”, 245); to hold the return value of
} student s7=job(); a function returning an
} object.
COPY CONSTRUCTOR INVOCATION Contd…..
Case 3: Copy constructor is invoked when the value of one object is copied to another object at the
time of creating an object. In all the other cases copy constructor is not invoked and value of one
object is copied to another object simply data member by data member. Example :

class student student( student & s) // Copy EXPLANATION:


{ Constructor
private: { rollno= s.rollno; Statement1: default
int rollno; strcpy(name, s.name); constructor is called
char name[20]; marks =s.marks; Statement2: parametrized
float marks; } } constructor is called
public: void main() Statement 3: copy
student( ) // Default Constructor { constructor is called
{ rollno=0; marks=0.0; } student s1; //statement1 statement4: Parametrized
student s2( 1, “RAJESH”, 450); // constructor is called
student(int roll, char nam[20], float statemet2 statement5: Copy
mar) // Parametrized Constructor student s3(s2); //statement3 constructor is called
{ rollno=roll; student s4(2, “MOHIT”,320); statement6: Copy
strcpy(name, nam); //statement4 constructor is not called
marks =mar; Student s5=s4; //statement5
} s1=s5; } // statement6
CONSTRUCTOR OVERLOADING
C++ allows more than one constructors to be defined in a single class.
Defining more than one constructors in a single class provided they all differ in either type or
number of argument is called constructor overloading.
 In the class student, there are 4
class student student(int roll, char nam[20]) // constructors:
{ Parametrized Constructor 1 default constructor,
private: { rollno=roll; 2 parametrized constructors
int rollno; strcpy(name, nam); and 1 copy constructor.
char name[20]; marks =100;
float marks; }  Defining more than one
public: constructor in a single class is
student( ) // Default Constructor student( student & s) // Copy called constructor overloading.
{ rollno=0; marks=0.0; } Constructor
student(int roll, char nam[20], { rollno= s.rollno;  We can define as many
float mar) // Parametrized Constructor strcpy(name, s.name); parametrized constructors as
{ rollno=roll; marks =s.marks; we want in a single class
strcpy(name, nam); } provided they all differ in either
marks =mar; } number or type of arguments.
}
DESTRUCTOR
 A destructor is a member function whose
name is the same as the class name and Example :
is preceded by tilde(“~”). class student
 It is automatically by the compiler when { private:
an object goes out of scope. int rollno;
 Destructors are usually used to deallocate float marks;
memory and do other cleanup for a class public:
object and its class members when the student( ) //Constructor
object is destroyed. { rollno=0; marks=0.0; }
 Note: In this program first the object s1 is ~student() //Destructor
created. At that time constructor is called. { }
When the program goes over the object };
s1 will also go out of scope. At that time void main()
destructor is called. { student s1;}
FEATURES OF DESTRUCTOR
It has the same name as that of the class and preceded by tilde sign(~).

It is automatically by the compiler when an object goes out of scope.

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.

Destructor is always declared in the public section of the class.

We can have only one destructor in a class.


POINTS TO REMEMBER
 Constructors and destructors do not have return type, not even void nor can they
return values.

 The compiler automatically calls constructors when defining class objects and
calls destructors when class objects go out of scope.

 Derived classes do not inherit constructors or destructors from their base


classes, but they do call the constructor and destructor of base classes.

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

You might also like