0% found this document useful (0 votes)
9 views5 pages

Constructor Destructor

Uploaded by

renukathakre80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Constructor Destructor

Uploaded by

renukathakre80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Constructor & Destructor

CONSTRUCTOR
 A constructor is a 'special' member function whose task is to initialize the objects of
its class.
 It is special because its name is the same as the class name.
 The constructor is invoked whenever an object of its associated class is created.
 It is called constructor because it constructs the values of data members
of the class.
A constructor is declared and defined as follows:
#include<iostream.h>
class sample
{
private:
int a,b;
public:
sample()
{
a=10,b=20;
}
void display()
{
cout<<a<<b;
}
};
void main()
{
sample s1;
s1.display();
}
When you create the constructor like in the above program, then object will be initialized
automatically. For example declaration
sample s1; //object s1 of class sample created
the above statement not only creates the object s1 of type sample but also initializes its
data members a to 10 and b to 20. There is no need to write any statement to invoke the
constructor function. If a 'normal' member function is defined for initialization, we would
need to invoke this function for each of the objects separately. This would be very
inconvenient, if there are a large number of objects.
A constructor that accepts no parameters is called the default constructor. The default
constructor for class A is A (). If no such constructor is defined, then the compiler
supplies a default constructor. Therefore a statement such as
A a;
invokes the default constructor of the compiler to create the object a.
The constructor functions have some special characteristics. These are:
• They should be declared in the public section.
• They arc invoked automatically when the objects are created.
• They do not have return types, not even void.
 They cannot return values.
• They cannot be inherited.
• They can have default arguments.
• Constructors cannot be virtual.
• We cannot refer to their addresses.
• An object with a constructor cannot be used as a member of a union.
• They make 'implicit calls' to the operators new and delete when memory allocation is
required.

1|Page
Constructor & Destructor

Parameterized Constructors
Sometimes it is necessary to initialize the various data elements of different objects with
different values. This objective is achieved by passing arguments to the constructor function
when the objects are created. The constructors that can take arguments are called
parameterized constructors.
In parameterized constructor, we must pass the initial values as arguments to the
constructor function when an object is declared. This can be done in two ways.
• By calling the constructor explicitly.
• By calling the constructor implicitly.

#include <iostream.h>
class sample
{
int a,b;
public:
sample(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<a<<b<<endl;
}
};
void main()
{
sample s1(0,100); //constructor called implicitly
sample s2=sample(20,110); //constructor called explicitly
cout<<”OBJECT1”;
s1.display();
cout<<”OBJECT2”;
s2.display();
}
The above program shows the following output
OBJECT1
0 100
OBJECT2
20,110
Implicit call
The statement given below is the implicit call of the constructor
sample s1(0,100); //implicit call
In implicit call values are assigned directly when object is created. This statement creates
an sample object s1 and passes the values 0 and 100 to it.

 This method, sometimes called the shorthand method.


 It is easy to implement.
 It is used very often.
Explicit call
The following example shows the explicit call
sample s2=sample(20,110); //explicit call
This statement creates an sample object s2 and passes the values 20 and 110 to it.

2|Page
Constructor & Destructor

Copy Constructor
A copy constructor is used to declare and initialize an object from another object. For
example, the statement
sample s2(s1);
would define the object s2 and at the same time initialize it to the values of s1. Another
form of this statement is
sample s2=s1;
The process of initializing through a copy constructor is known as copy initialization.
The statement s2=s1 will not invoke the copy constructor. However, if s1 and s2 are
objects, this statement is legal and simply assigns the values of s1 to s2, member-by-
member. This is the task of the overloaded assignment operator(=).
A copy constructor takes a reference to an object of the same class as itself as an argument.
#include<iostream.h>
class sample
{
int id;
public:
sample(int a) //parameterized constructor
{
id=a;
}
sample(sample &x) //copy constructor
{
id=x.id;
}
void display()
{
cout<<id;
}
};
void main()
{
sample a(100); // parameterized constructor is called here
sample b(a); // copy constructor is called here
a.display();
b.display();
}

As shown in the above program the instruction sample a(100) calls the parameterized
constructor which sets the value of id for object a is set to 100. When copy constructor is
called i.e. sample b(a); the value of id of a is copied to the id of b. This example is also you
can use for constructor overloading also.

Dynamic constructor
The constructors can also be used to allocate memory while creating objects. This will
enable system to allocate the right amount of memory for each object when the objects are
not of the same size, thus resulting in the saving of memory. Allocation of memory to
objects at the time of their construction is known as dynamic construction of objects. The
memory is allocated with the help of the new operator.

3|Page
Constructor & Destructor

#include<iostream.h>
#include<string.h>
class sample
{
char *name;
public:
sample(char *s)
{
l=strlen(s);
name=new char[l+1]; //one additional space for null
strcpy(name,s);
}
void display()
{
cout<<strlen(name);
}
};
void main()
{
sample s(“ccit”);
s.display();
}

Here in output the length of the string will be displayed. One parameterized constructor is
used here to pass the string as argument runtime. The new operator is use to allocate the
memory to name. one is added in length because we’ve to allocate the memory space to
null character explicitly.

DESTRUCTOR
 A destructor is used to destroy the objects that have been created by a constructor.
 The destructor is a member function whose name is the same as the class name but
is preceded by a tilde.
 For example, the destructor for the class sample can be defined as shown below:
~sample()
{
}
 A destructor never takes any argument.
 It does not return any value.
 It will be invoked implicitly by the compiler upon exit from the program.
 Whenever new is used to allocate memory in the constructors, we should use
delete to free that memory.
Example
#include<iostream.h>
class sample
{
public:
sample()
{
cout<<”object created”;
}
~sample()
{

4|Page
Constructor & Destructor

cout<<”object destroyed”;
}
};
void main()
{
sample s;
}

OUTPUT
object created
object destroyed

5|Page

You might also like