Wednesday 4 January 2017

Constructor & Destructor

Share & Comment
CONSTRUCTOR

C++ provides a special member function called the constructor which enables an object to initialize itself when it is created. This is known as automatic initialization of objects. A constructor name is same as the class name. it is called constructor because it constructs the values of data members of the class.

A constructor is declared and defined as follows:

//class with a constructor

Class integer

{
 int m, n;
public:
integer(void)
 ………..
};

integer :: integer(void)
{
m=0;
n=0;
}

When a class contains a constructor like the one defined above, it is guaranteed that an object created by the class will be initialized automatically. For example, the declaration

   integer int1;

not only creates the object int1 of type integer but also initializes its data members m ans n to 0.

The constructor functions have some special characteristics:

  • They are invoked automatically when the objects are created.
  • We cannot refer to their address.
  • An object with a constructor cannot be used as a member of a union.
  • Like other C++ function, they can have defaults arguments.
  • They should be declared in the public section.
  • They do not have return types, not even void therefore, and they cannot return values.
  • They cannot be inherited, tough a derived class can call the base class constructor.
  • They can implicit calls to the operators new and delete when memory allocation is required.


TYPES OF CONSTRUCTORS

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

Parameterized Constructor: The constructor that can take argument are called parameterized constructor.

Copy Constructor: A copy constructor takes a reference to an object of the same class as itself as an argument.

Dynamic Constructor: The constructor can also be used to allocate memory while creating objects. This will enable the 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.

  

DESTRUCTOR

A destructor as the name implies is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor is a member function whose name is same as the name of class but it is preceded by a tilde. For example the constructor for the class integer can be defined as shown below:

  ~integer()
{
   ………
}

A destructor never returns any argument nor does it return any value. It will be invoked implicitly by the compiler upon exit from the program to clean up storage that is no longer accessible. It is good practice to declare destructors in a program since it releases memory space for future use. 



Tags:

Written by

Hy, i am a Code Geek ! And like to eduate others.

0 comments:

Post a Comment

 
Copyright © C++ Programming | Designed by Templateism.com