12

Types of Constructors in Object Oriented Language

Harsh Gupta - Tech Writer
Constructors are the special member function with the same name as its class. Constructor is used to initialize the objects of that class type with some initial value. Advantages of constructor is that it is invoked (called) automatically as soon as the object of that class is created. This is also known as automatic initialization of objects.

Characteristics of constructor:

Constructor should be declared in public section.

No return type can be specified for constructors. Not even void type can be specified for return type in constructor. So constructor can't return values.

Constructors are invoked automatically when objects are created.

Constructor function can have default arguments.

Other member function can be called from constructor.

They can't be inherited.

Example:

Refer to the image for example.

Here, as soon as the object S1 is declared, its variables m, n is initialized to the value zero.

Parameterized constructor:-

A constructor can also take arguments. A constructor having some argument in it is called parameterized constructor. They allow us to initialize the various data element of different objects with different values.

We have to 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

< Classname > = (value, value,...........);

· By calling the constructor implicitly

(value, value, ..............);

Copy constructor

As the name depicts, copy constructor copies value of one object to a different object inside a same class.

Syntax:

Sample (sample &);

The argument to a copy constructor will always pass by reference method if we pass argument by value method compiler will show error 'out of memory'. The reason is that when an argument is passed by value, a copy of it is constructed. Hence, copy constructor call itself again and again to copy the argument and computer will display message out of memory.

Example:

Integer (integer &k)

{

N=k.n;

}

And this constructor will be called as:

Integer int3 (int1);

This will copy the value of object int1 to int3.

Or this can be called by this: integer int3;

Int3=int1; // calling copy constructor

Default constructor

The constructor that accepts no parameter is called default constructor. If a class has no precise constructor defined then the compiler will provide a default constructor.

Dynamic constructor

We can also use constructor to allocate memory while creating objects. This will enable the system to allocate the right amount of memory for each object, thus resulting in the saving of memory. Allocation of memory to object at the time of their construction is known as dynamic construction of objects.

Example:

Refer to the image for example.

This will result in dynamic memory allocation.

Published by Harsh Gupta - Tech Writer

I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com  View profile

  • Constructor should be declared in public section.
  • Constructors are invoked automatically when objects are created.
  • Other member function can be called from constructor.
A constructor can also take arguments. A constructor having some argument in it is called parameterized constructor. They allow us to initialize the various data element of different objects with different values.

To comment, please sign in to your Yahoo! account, or sign up for a new account.